tempr not working


Author Message
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 3321
Posted: 08:57am 02 Jun 2026      

A number of DS18x20 can run from the same OneWire bus by using the unique ROM code each contains.
The codes can be read by copying and pasting the command lines below into TeraTerm.
Only one device may be on the bus while reading the code. Ignore the output numbers in the example

> clear : dim integer q,w,e,r,t,y,u,i,ROM, PinNo=mm.info(pinno GP6)
> OneWire Reset PinNo : OneWire WRITE PinNo,1,1,&h33 : OneWire READ PinNo,2,8,q,w,e,r,t,y,u,i : ? ,q,w,e,r,t,y,u,i
        40      63      68      87      4       225     60      77
> RC$ = chr$(q)+chr$(w)+chr$(e)+chr$(r)+chr$(t)+chr$(y)+chr$(u)+chr$(i) : ROM = str2bin(Uint64,RC$,big) :? ROM
2900111825584602189
>
Its a bit clunky but OneWire doesn't use arrays or strings.

Insert your ROM code numbers and OneWire pin number in the program below. This example is for two but could be modified for more.
' running 2 DS18x20 sensors on the same pin. Get their ROM codes an replace those below
Dim PinNbr = MM.Info(pinno gp6)
Dim integer n, ROMcode(1) = (2909716863767585976, 2900111825584602189)

Dim DStemp0 = DS18x20(PinNbr, ROMcode(0)) 'purge old data and start new conversion
Dim DStemp1 = DS18x20(PinNbr, ROMcode(1)) 'purge old data and start new conversion

Do '  Main Loop
 D = DS18x20(PinNbr, ROMcode(n))
 If D<>1000 Then Print "Device";n; D;"`C",
 If n=1 Then Print
 Pause 1000
 n = Not n
Loop

Function DS18x20(PinNbr As INTEGER, ROM As INTEGER) As FLOAT
' Adapted from DS18x20 test code by TassyJim
 Local INTEGER  done, T1, T2, T3, CpC, Fam, a,b,c,d,e,f,g,h
 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

 If ROM Then
   a=ROM>>56 : b=ROM>>48 And 255 : c=ROM>>40 And 255 : d=ROM>>32 And 255
   e=ROM>>24 And 255 : f=ROM>>16 And 255 : g=ROM>>8 And 255 : h=ROM And 255
   Fam=a  ': Print a,b,c,d,e,f,g,h
   OneWire WRITE PinNbr,1,1,&h55 '        reset then match ROM Code command
   OneWire WRITE PinNbr,0,8,a,b,c,d,e,f,g,h '       send ROM Code
   OneWire WRITE 9,0,1,&hBE  '                      read scratchpad command
   OneWire READ 9, 0, 8, T1,T2,T3,T4,T5,T6,T7,CpC'  get the data
  Else
   OneWire WRITE PinNbr,1,1,&h33 '        read "family" ROM code
   OneWire READ PinNbr,0,1,Fam
 EndIf

' process the data           16=DS1820/DS18S20, 32=DS18S22, 40=DS18B20
 If Fam = 16 Then Value = Str2bin(int16,Chr$(T1)+Chr$(T2))\2 -0.25+(CpC-T3)/CpC
 If Fam = 40 Or Fam = 34 Then Value = Str2bin(int16,Chr$(T1)+Chr$(T2))/16
 DS18x20 = Cint(Value * 100) / 100 'round to 1/100ths

' Start conversion, ready for next read
 If ROM Then
   OneWire WRITE PinNbr,1,1,&h55  'reset then match ROM Code command
   OneWire WRITE PinNbr,0,8,a,b,c,d,e,f,g,h 'send ROM Code
   OneWire WRITE PinNbr,2,1,&h44 'Master issues Convert Temp. command
  Else
   OneWire RESET PinNbr'                      ' reset before command
   OneWire WRITE PinNbr, 8, 2, &hCC, &h44'    ' start conversion
 EndIf
End Function
End


Footnote added 2026-06-02 21:49 by phil99
A sample of the output.
Device 0 24.19`C        Device 1 31.69`C
Device 0 23.38`C        Device 1 31.56`C
Device 0 22.69`C        Device 1 31.44`C
Device 0 22.13`C        Device 1 31.25`C
Device 0 21.63`C        Device 1 31.13`C
Device 0 21.25`C        Device 1 31`C
Device 0 20.88`C        Device 1 30.88`C
Device 0 20.63`C        Device 1 30.75`C
Device 0 20.38`C        Device 1 30.63`C
Device 0 20.19`C        Device 1 30.5`C


Footnote added 2026-06-02 22:01 by phil99
Noticed a copy/paste error tat would affect the fractions of degrees for DS18S20 sensors.
Replace this line:-
OneWire READ 9, 0, 8, T1,T2,T3,T4,T5,T6,T7,CpC'  get the data
with
OneWire READ 9, 0, 8, T1,T2,T3,T3,T3,T3,T3,CpC'  get the data

Most of the T3s are dummys, only the one before CpC gives the fractions.

Footnote added 2026-06-11 12:04 by phil99
Found another copy / paste error, going senile.
So here is the function again with all errors fixed.
  Quote  Function DS18x20(PinNbr As INTEGER, ROM As INTEGER) As FLOAT
'  Adapted from DS18x20 test code by TassyJim
  Local INTEGER  done, T1, T2, T3, CpC, Fam, a,b,c,d,e,f,g,h
 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

  If ROM Then
    a=ROM>>56 : b=ROM>>48 And 255 : c=ROM>>40 And 255 : d=ROM>>32 And 255
    e=ROM>>24 And 255 : f=ROM>>16 And 255 : g=ROM>>8 And 255 : h=ROM And 255
    Fam=a  ': Print a,b,c,d,e,f,g,h
    OneWire WRITE PinNbr,1,1,&h55 '        reset then match ROM Code command
    OneWire WRITE PinNbr,0,8,a,b,c,d,e,f,g,h '       send ROM Code
   Else
    OneWire WRITE PinNbr,1,1,&h33 '        read "family" ROM code
    OneWire READ PinNbr,0,1,Fam
  EndIf
  OneWire WRITE PinNbr,0,1,&hBE  '                      read scratchpad command
  OneWire READ PinNbr, 0, 8, T1,T2,T3,T3,T3,T3,T3,CpC'  get the data

' process the data           16=DS1820/DS18S20, 32=DS18S22, 40=DS18B20
If Fam = 16 Then Value = Str2bin(int16,Chr$(T1)+Chr$(T2))\2 -0.25+(CpC-T3)/CpC
If Fam = 40 Or Fam = 34 Then Value = Str2bin(int16,Chr$(T1)+Chr$(T2))/16
DS18x20 = Cint(Value * 100) / 100 'round to 1/100ths

' Start conversion, ready for next read
If ROM Then
  OneWire WRITE PinNbr,1,1,&h55  'reset then match ROM Code command
  OneWire WRITE PinNbr,0,8,a,b,c,d,e,f,g,h 'send ROM Code
  OneWire WRITE PinNbr,2,1,&h44 'Master issues Convert Temp. command
 Else
'   OneWire RESET PinNbr'                      ' reset before command
  OneWire WRITE PinNbr, 1, 2, &hCC, &h44'    ' start conversion
EndIf
End Function
End


Footnote added 2026-06-11 13:46 by phil99
I just keep making mistakes. This time lucky?
Added a feature. If there is only one device on the bus and an integer variable with a value of 0 is supplied as the ROM Code it will be filled with the device ROM Code. So you can get the ROM Codes of devices without using 1Wire commands.
Eg.
> ? "Temp.";ds18x20(2,code%);"`C", "ROM Code";code% 'only one on the bus
Temp. 21.5`C    ROM Code 2900111825584602189
>
> ? ds18x20(2,2900111825584602189), ds18x20(2,2909716863767585976) 'multiple on the bus
21.5    22.25
>

And the amended function.
Function DS18x20(PinNbr As INTEGER, ROM As INTEGER) As FLOAT
'  Adapted from DS18x20 test code by TassyJim
  Local INTEGER  done, T1, T2, T3, CpC, Fam, a,b,c,d,e,f,g,h
 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

  If ROM Then
    a=ROM>>56 : b=ROM>>48 And 255 : c=ROM>>40 And 255 : d=ROM>>32 And 255
    e=ROM>>24 And 255 : f=ROM>>16 And 255 : g=ROM>>8 And 255 : h=ROM And 255
    Fam=a  ': Print a,b,c,d,e,f,g,h
    OneWire WRITE PinNbr,1,1,&h55 '        reset then match ROM Code command
    OneWire WRITE PinNbr,0,8,a,b,c,d,e,f,g,h 'send ROM Code
    OneWire WRITE PinNbr,0,1,&hBE  '          read scratchpad command
   Else
    OneWire WRITE PinNbr,1,1,&h33 '           read "family" and ROM code
    OneWire READ PinNbr,0,8,Fam,b,c,d,e,f,g,h
    ROM=(Fam<<56)+(b<<48)+(c<<40)+(d<<32)+(e<<24)+(f<<16)+(g<<8)+h
    OneWire WRITE PinNbr,1,2,&hCC,&hBE '      read scratchpad command
  EndIf
  OneWire READ PinNbr, 2, 8, T1,T2,T3,T3,T3,T3,T3,CpC'  get the data

' process the data.     Fam - 16=DS1820/DS18S20, 32=DS18S22, 40=DS18B20
  If Fam = 16 Then Value = Str2bin(int16,Chr$(T1)+Chr$(T2))\2 -0.25+(CpC-T3)/CpC
  If Fam = 40 Or Fam = 34 Then Value = Str2bin(int16,Chr$(T1)+Chr$(T2))/16
  DS18x20 = Cint(Value * 100) / 100 'round to 1/100ths

' Start conversion, ready for next read
  If ROM Then
    OneWire WRITE PinNbr,1,1,&h55 '          reset then match ROM Code command
    OneWire WRITE PinNbr,0,8,a,b,c,d,e,f,g,h 'send ROM Code
    OneWire WRITE PinNbr,2,1,&h44 '          Master issues Convert Temp. command
   Else
    OneWire WRITE PinNbr, 1, 2, &hCC, &h44'   start conversion
  EndIf
End Function
End