tempr not working


Author Message
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 3348
Posted: 04:06am 30 May 2026      

The last byte is COUNT_PER_C (CpC)
TEMPERATURE = TEMP_READ – 0.25 + (COUNT_PER_C – COUNT_REMAIN) / COUNT_PER_C

Function DS18S20(PinNbr As INTEGER) As FLOAT
' derived from DS18x20 test code by TassyJim
  Local INTEGER  T1,T2,T3,CpC

  OneWire RESET PinNbr                       ' reset before command
  OneWire WRITE PinNbr, 8, 2, &hCC, &h44     ' start conversion
  Pause 750                                  ' maximum conversion time
  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

  DS18S20t = Str2bin(int16,Chr$(T1)+Chr$(T2))\2 -0.25 + (CpC-T3)/CpC 'process the data
End Function


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

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

Function DS18S20(PinNbr As INTEGER) As FLOAT
' Adapted from DS18x20 test code by TassyJim
  Local INTEGER  done, T1, T2, T3, CpC
  DS18S20 = 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 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

  Value = Str2bin(int16,Chr$(T1)+Chr$(T2))\2  -0.25 + (CpC-T3)/CpC 'process the data
  DS18S20 = 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


Edit.
Corrected a few typos in code above.
If you are already using it please copy it again.
Edited 2026-05-30 14:13 by phil99