PDA

View Full Version : ds18b20 and CRC check



iw2fvo
- 23rd November 2015, 15:09
Good day to all of you in the forum.
I wrote a picbasic code that allows me to read the temperature from about nine DS18B20 sensors. I just read the temperature from the 18b20 but I do not read the crc and I do not check it for data integrity.
The programs works well and I get valid data from all the connected sensors.
I would like to implement CRC check in my program and I need some help.
> What data should I read from the sensor and how could I compute the CRC and compare it for integrity data check ?
Thanks in advance for any help.
Regards,
Ambrogio

aerostar
- 23rd November 2015, 18:40
Have a look at post 17 on this thread http://www.picbasic.co.uk/forum/showthread.php?t=7533

iw2fvo
- 23rd November 2015, 20:21
OK, thanks.
Is the code attached to post #18 the final one ?
Thanks
Ambrogio

iw2fvo
- 24th November 2015, 13:50
Hi,
I just used the crc part of the code indicated by you: it is working well.
Thanks
regards,
Ambrogio

Scampy
- 27th November 2015, 23:23
I've tried to incorporate the code (modified to suit my variables) in post #17 so that it will display "NC" on an LCD if the sensor is faulty or not present... but it doesn't work.

This is the CRC check subs - the only change is the TempT variable insted of DQ



GetCRC:
for x = 0 to 7 ; Get CRC for each of the 8 bytes
DataByte = TempT[x] ; Assign array pointer using value of x
gosub CalcCRC ; Get CRC value
next x ; Repeat until all bytes are done
if TempT[8] <> CRCCalc then ; Do the CRC values match?
CRCError = 1 ; Set flag indicating a problem with this sensor
else
CRCError = 0 ; Set flag indicating no problem with this sensor
endif
CRCCalc = 0 ; Reset CRC calculation variable
return

;--------------------- CRC Bit Calcuation Method -------------------------------

CalcCRC:
for i = 0 to 7 ; Do for all 8 bits in DataByte
DataBit = CRCCalc.0 ^ DataByte.0 ; XOR bit0 of DataByte and CRC
DataByte = DataByte >> 1 ; Position DataByte for next bit test
if DataBit = 0 then Shift ; If test bit not set, just shift CRCCalc
CRCCalc = CRCCalc ^ $18 ; If set, account for EXOR feedback

shift:
CRCCalc = CRCCalc >> 1 ; Shift right the CRCCalc byte
CRCCalc.7 = DataBit ; CRC bit 0 to bit bucket
next i ; Data bit rotates into CRC bit 7
return


I then have this as the main part of my program loop



gosub GetCRC ; Calculate the CRC for comparison

OWOUT TEMPIN,1,[$CC,$44]
HIGH TEMPIN
PAUSE 1000
OWOUT TEMPIN,1,[$CC,$BE]
OWIN TEMPIN,0,[TempT.LowByte,TEMPT.HighByte]
TempC = (TempT*/1600)
if CRCError = 1 then
LCDout $FE,$80+5,"N/C "
endif
if CRCError = 0 then
LCDout $FE,$80,"Temp ",DEC (TempC/100),".",#TempC dig 1,"C"
endif


With teh DS18B20 connected I get a true temperature displayed on the screen, but If I remove the DS18B20 from the inpt pin the LCD displays 163.7c on the display (presumably it's the maximum value for a word variable and the the formulat applied to convert it to a temperature) rather than "NC"

Any suggestions ?

EDIT:

OK I thought about it a bit more and realised I needed to read the sensor first and then do the check and then apply the if then condition.... but now all I get is "NC" on the screen even when the sensor is connected !

richard
- 28th November 2015, 00:47
OWIN TEMPIN,0,[TempT.LowByte,TEMPT.HighByte]


what about the other 5 data bytes that you need for the crc calc , and the crc actually byte sent by the ds18b20 to check against ?

hint , if you feed the final crc byte sent into the crc routine then if there are no errors the result is always zero