This is the code I use for calculating the CRC from the Dallas 18B20 temperature sensor. The only difference I can notice is the locations of the << and >> in the two code. Can you give me an example of how your code is used cause I'm still a little fuzzy on what data does where. I know the CRC value byte is the third byte but not sure what bytes are used to calculate the CRC to compare to this value.
Code:
------------------------ Calculate the CRC from Byte 9 ------------------------
GetCRC:
for x = 0 to 7
databyte = dq[x]
gosub CalcCRC
next x
if dq[8] <> crccalc then
sensorerror = 1
else
sensorerror = 0
endif
crccalc = 0
return
'--------------------- CRC Bit Calcuation Method -------------------------------
CalcCRC:
for i = 0 to 7
databit = crccalc.0 ^ databyte.0
databyte = databyte >> 1
if databit = 0 then Shift
crccalc = crccalc ^ $18
shift:
crccalc = crccalc >> 1
crccalc.7 = databit
next i
return
Code:
'------------------ Read SHT71 Temperature / Humidity Sensor -------------------
ReadSHT71:
gosub Initialize
Sensorcmd = ReadTemp
gosub readsensor
if result => 4000 then
tempC = result - 4000
SignC = 160
else
tempC = 4000 - result
SignC = "-"
endif
gosub ConvertToKelvin
gosub ConvertToFahrenheit
gosub Initialize
Sensorcmd = ReadHumidity
gosub readsensor
'--------------------- Calculate Relative Humidity -----------------------------
w = result * 405
w = div32 100
x = result * result
x = div32 1000
x = x * 28
x = div32 100
RHLinear = w - x - 400
'---------- Calculate Relative Humidity with Temperature Compensation ----------
w = (8 * result + 1000) / 10
if SignC = 160 then
if tempC > 2500 then
x = (tempC - 2500) * w
x = div32 100
RHTempComp = RHLinear + x / 100
else
x = (2500 - temp) * w
x = div32 100
RHTempComp = RHLinear - x / 100
endif
else
x = (2500 + tempC) * w
x = div32 10000
RHTempComp = RHLinear - x
endif
sleep 1
return
'---------------------------- Initialize the Sensor ----------------------------
Initialize:
high DataPin
low clk
for i = 1 to 10
high clk
pause 1
low clk
pause 1
next i
call TransferStart
return
'---------------------------- Get Data from Sensor -----------------------------
ReadSensor:
gosub TransferStart
gosub WaitSensor
shiftout DataPin,clk,1,[Sensorcmd\8] ' Send command byte
input DataPin ' Wait for acknowledge
low clk
while DataPin = 1
wend
pulsout clk,10 ' Send acknowledge
while DataPin = 0
wend
while DataPin = 1 ' Wait for conversion to complete
wend
low clk
shiftin DataPin,clk,0,[result.byte1\8] ' Get the first byte, 8 bits
low DataPin
pulsout clk,10 ' Send acknowledge
shiftin DataPin,clk,0,[result.byte0\8] ' Get the second byte, 8 bits
low DataPin
pulsout clk,10 ' Send acknowledge
shiftin DataPin,clk,0,[crc\8] ' Get third byte, 8 bits, CRC
high DataPin
pulsout clk,10 ' Send acknowledge
input DataPin ' End of Transmission
input clk
return
'---------------------------- Start Transfer -----------------------------------
TransferStart:
high clk
pause 1
low DataPin
pause 1
low clk
pause 1
high clk
pause 1
high DataPin
pause 1
low clk
return
'---------------------------- Wait for Sensor ----------------------------------
WaitSensor:
result = 4096
Loop:
result = result - 1
if DataPin && result.bit11 then Loop
return
'-------------------------- Convert to Kelvin ----------------------------------
ConvertToKelvin:
if signC = "-" then
tempk = 27315 - temp
else
tempk = temp + 27315
endif
return
ConvertToFahrenheit:
if SignC = 160 then
SignF = 160
dummy = TempC * 18
TempF = div32 10
TempF = TempF + 3200
else
TempF = (tempc + 5000) * 900
TempF = DIV32 500
if TempC => 1778 and SignC = "-" then
TempF = TempF - 12200
SignF = "-"
else
TempF = 12200 - TempF
SignF = 160
endif
endif
return
Bookmarks