The following code will read a single DS18B20 sensor ( non parasite powered ), and will check for sensor being present/unplugged during conversion. It also deals with negative temps, and i get around this by adding 50degC to the result, and removing it later as required.
Ps It will also trap sensor errors where there is a pullup but no sensor, as well as no pullup and no sensor ( as they give different results.)
-------------------------------------------------------------------
tempAir VAR WORD ' Air Temp ( ambient )
temp1 Var Word ' Temperature storage
tempx var word
temp2 Var Word ' Temperature storage
dummy VAR BYTE
TmpPos var Byte 'Temp pos or neg
Tmp var Word 'Temp
OWPORT var BYTE 'Pin the sensor is on
NoSens var BIT 'Byte to check for sensors
'-------------------------------------------------------------------------------
' Main loop
loop:
' Get AIR temp
OWPORT = 13 ' Pins 13 maps to C.5 in a 28pin device
GOSUB ReadOW
tempAir = temp1 'now has corrected ( temp + 50deg ) << 4 to beat minus signs
' Get next temp
OWPORT = ??
etc etc
goto loop
end
'-------------------------------------------------------------------------------
' Routine to read a one wire device ( Assumes single unit in full powered mode, not Parasite )
' Checks for sensors ( pulldowns on switched inputs detect if sensor in place )
ReadOW:
temp1 = 0 ' ( Temp + 50 ) * 16
NoSens = 0 ' Sensor check failed bit
TmpPos = 0 ' Sensor check
OWOut OWPORT, 1, [$CC, $44] 'Start conversion process
waitloop:
OWIn OWPORT, 4, [dummy] ' Check for still busy converting
temp1 = temp1 + 1 ' Need next bit to cover probe unplugging after conversion starts
pause 10 ' max conversion time for 12bit temp is 750mSec max
if temp1 > 75 then ' hence if get this far, abort so she doesnt hang
NoSens = 1
goto waitlost
endif
If dummy = 0 Then waitloop ' Not finished converting
OWOut OWPORT, 1, [$CC, $BE] ' Request read of the temperature
OWIn OWPORT, 0, [temp1.LOWBYTE, temp1.HIGHBYTE, TmpPos, Skip 5 ]
if TmpPos = %11111111 then 'If pullup is there but no sensor
NoSens = 1 'TmpPos comes back as all 1s
endif
waitlost:
if NoSens then
temp1 = 9999 'Ie failed
else
' DS18B20 in full 12 bit mode returns temp as a binary value in 1/16th deg C ( 0.0625 )
' We must check MSB of temp1. If it is 1 we have a neg value and this requires
' different processing. We calc this using temp2 = ~temp1 + %1
' To get rid of negatives in later processing, we send temp1 back moved up by 50 degrees
' as such all processing in further code requires no negative nos
if temp1.bit15 = 1 then 'negative
Lcdout "-"
tempx = ~temp1 + %1 ' needed to calc correct display
temp1 = ( 50 << 4 ) - tempx ' add 50 to ensure we always get positive nos
else
tempx = temp1
temp1 = ( 50 << 4 ) + temp1 ' add 50 to keep sweet with neg nos
endif
endif
RETURN
------------------
As a bit of further info, there are probs with the later version DS18B20s which may also be coming into it, which is why they were pulled from the market.
The B6 die units have variable timing probs, and the B7 dies can lose their internal calibrations.
Andrew
Bookmarks