CRC Subroutines for DS18B20 Temperature probes

AlanT

New Member
Hi All

Here's a subroutine pair that I wrote based on the published CRC algorithm for the Dallas thermometer. Its directly for use with the 8 bytes of the scratchpad read, with the CRC in byte 9. If the last-but-one call of the CRC_byte subroutine is removed then it also works for the address response, which is 7 bytes plus CRC. It's written for the 28X1 PIC. It assumes the 1-wire status read is into registers b1-b9.

I've tested it, and believe it's fully working.

If anyone can suggest a faster version, that would be nice.

Unfortunately I don't have the facility to merge assembler into the Basic.

Cheers

AlanT
;
;
;************************************
;
; CRC Subroutine
;
;************************************
;
; inputs: data bytes in b1-b8, CRC in b9
;
; outputs: result in b10 - zero is a pass
;
; uses: b0,b10,b11,b12
;
CRC:
b10=0 ;seed CRC at 0
;
b12=b1
gosub crc_byte
b12=b2
gosub crc_byte
b12=b3
gosub crc_byte
b12=b4
gosub crc_byte
b12=b5
gosub crc_byte
b12=b6
gosub crc_byte
b12=b7
gosub crc_byte
b12=b8
gosub crc_byte
b12=b9 ;received CRC byte last into shift register
gosub crc_byte
;
return
;
;
;
;********************************************
;
; CRC Byte Subroutine
;
; critical for speed, as this code is run 64 times for each temperature reading
;
;*******************************************
;
; inputs: b12, new data to be shifted into CRC, b10 - current crc
;
; uses: b11 - loop count for this new byte, b0 - scratchpad
;
; outputs: b10, new CRC
;
crc_byte:
;
b11=0 ;loop count for 8 bits

do
;
; exclusive OR the current CRC with the new data byte (into a temporary register b0)
;
b0=b10 xor b12
;
; shift the CRC one bit right, filling MSB with a 0
;
b10=b10 >> 1
;
; test least significant bit of first XOR - put in feedback XOR if 1
;
if bit0 = 1 then
b10=b10 xor %10001100 ;feedback taps at bits 7,3 & 2
endif
;
; shift the input data right 1 bit (don't care what MSB becomes)
;
b12=b12>>1
;
; loop 8 times
;
inc b11
loop until b11>7
;
return
;
;
;
 
Top