Menu
JAQForum Ver 19.10.27

Forum Index : Microcontroller and PC projects : tempr not working

   Page 4 of 4    
Posted: 05:35am
30 May 2026
Copy link to clipboard
Peter63
Senior Member

Jepp  
 
Posted: 05:47am
30 May 2026
Copy link to clipboard
Bryan1
Guru


Well I decided to setup my 2040 Zero with that ili9488 now it's free and gave this DS1820 code a go, now I was having no luck so went back to the start of this thread and saw Tempr(GP9,500) was the fix to get the DS1820 working.

Now when I run the code.

50
Temp = 680°C
50
Temp = 680°C
50
Temp = 680°C
50
Temp = 680°C
50
Temp = 680°C
50
Temp = 680°C
50
Temp = 680°C
50
Temp = 680°C
50
Temp = 154°C
50
Temp = 154°C
50
Temp = 154°C
50
> ? tempr (Gp9, 500)
19.25
>


Nothing changed in the code just copied it and pasted into MMEdit. Anyway the good thing the DS1820 looks to be working
 
Posted: 06:13am
31 May 2026
Copy link to clipboard
phil99
Guru


Ok, so if you don't know which DS18x20 you have these two functions should work with any, including slow ones.
In the first one you can optionally set the Conversion Time in mS.
Usage:-
Print DS18x20ct(mm.info(pinno GPxx)) ' the default Conversion Time is 750mS
or
Print DS18x20ct(mm.info(pinno GPxx, mS_delay) '1000mS should cover most.

The second doesn't need it as it checks if the conversion is finished before reading. If not finished it returns to the main loop.
After reading it starts a new conversion, whish happens in the background as it returns to the main loop.
Usage:-
Print DS18x20(mm.info(pinno GPxx))

Function DS18x20ct(PinNbr As INTEGER, Delay As INTEGER) As FLOAT
' derived from DS18x20 test code by TassyJim
  Local INTEGER  T1,T2,T3,CpC,F
  OneWire WRITE PinNbr,1,1,&h33 'read "family" ROM code
  OneWire READ PinNbr,0,1,F ':Print F, "16=DS1820/DS18S20, 32=DS18S22, 40=DS18B20",
  OneWire RESET PinNbr                       ' reset before command
  OneWire WRITE PinNbr, 8, 2, &hCC, &h44     ' start conversion, adjust for slow units
  If Delay < 10 then Delay = 750             ' maximum normal conversion time
  Pause Delay                                
  OneWire RESET PinNbr                       ' reset before command
  OneWire WRITE PinNbr, 1, 2, &hCC, &hBE     ' command read data
  OneWire READ PinNbr, 0, 8, T1,T2,T3,T3,T3,T3,T3,CpC  'get the data
  'process the data
  If F=16 Then DS18x20ct = Str2bin(int16,Chr$(T1)+Chr$(T2))\2  -0.25 + (CpC-T3)/CpC
  If F=40 or F=34 Then DS18x20ct = Str2bin(int16,Chr$(T1)+Chr$(T2))/16
End Function


PinNbr = MM.Info(pinno GP7)
Dim DStemp = DS18x20(PinNbr) 'purge old data and start new conversion
Dim integer n

Do    'Main Loop
  t = timer
  Print n;
  DStemp = DS18x20(PinNbr) 'get Temp, if ready
  If DStemp <> 1000 Then
    Print
    Print "Temp =";DStemp;"`C"
  n = 0
  EndIf
  Do : Loop until Timer > 49.9
  inc n,50
Loop

Function DS18x20(PinNbr As INTEGER) As FLOAT
' Adapted from DS18x20 test code by TassyJim
  Local INTEGER  done, T1, T2, T3, CpC, F
  DS18x20 = 1000                           ' standard error code
  OneWire READ PinNbr, 4, 1, done          ' is conversion finished?
  If done = 0 Then Exit Function           ' if not return to program
  'Get the data
  OneWire WRITE PinNbr,1,1,&h33 'read "family" ROM code
  OneWire READ PinNbr,0,1,F ':Print F; " 16=DS1820/DS18S20, 32=DS18S22, 40=DS18B20",
  OneWire RESET PinNbr                     ' reset before command
  OneWire WRITE PinNbr, 1, 2, &hCC, &hBE   ' command read data
  OneWire READ PinNbr, 0, 8, T1,T2,T3,T3,T3,T3,T3,CpC  ' get the data
  ' process the data
  If F=16 Then Value = Str2bin(int16,Chr$(T1)+Chr$(T2))\2  -0.25 + (CpC-T3)/CpC
  If F=40 or F=34 Then Value = Str2bin(int16,Chr$(T1)+Chr$(T2))/16
  DS18x20 = Cint(Value * 10) / 10 'round to 1/10ths

  'Start conversion, ready for next read
  OneWire RESET PinNbr                      ' reset before command
  OneWire WRITE PinNbr, 8, 2, &hCC, &h44    ' start conversion
End Function
End
 
Posted: 07:00am
31 May 2026
Copy link to clipboard
Bryan1
Guru


Thanks for that Phil   just tried it

Temp = 85°C
50
Temp = 85°C
50
Temp = 85°C
50
Temp = 85°C
50
Temp = 85°C
50
Temp = 85°C
50
Temp = 85°C
50
Temp = 85°C
50
Temp = 18.1°C
50
Temp = 18.1°C
50
Temp = 18.1°C
50
Temp = 18.1°C
50
Temp = 18.1°C
50


I did get these sealed can temp sensors about 15-16 years ago so they may be DS1820's  that need the delay for it to work.

Anyway it is time to make a new thread with my project for the temp sensor.

Regards Bryan
 
   Page 4 of 4    


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2026