1-wire CRC code computation
In one wire devices transmitted data are easly to be disrupted. To prevent from reading wrong data are used CRC mechanism. For example, reading the scrachpad memory from DS18B20 digital thermometer is ended by CRC calculated from eight bytes of data from scratchpad memory. Also serial number read sequence provide CRC code calculated from device family ID and 48-bit unique ID number of one wire device. By checking CRC code of read data we can detect wrong transmission and errors in data.
;------------------------------------------------------------------------------
; CRC8 computing functions
; based on Application Note 27 from Dallas Semiconductor
; http://avr-mcu.dxp.pl
; e-mail: radek(at)dxp.pl
; (c) Radoslaw Kwiecien
;------------------------------------------------------------------------------
;------------------------------------------------------------------------------
; Data segment
;------------------------------------------------------------------------------
.dseg
_crc : .byte 1
;------------------------------------------------------------------------------
; Code segment
;------------------------------------------------------------------------------
.cseg
;------------------------------------------------------------------------------
; Update crc value
;------------------------------------------------------------------------------
CRC8Update:
push r16
push r17
push r18
push r16
ldi r17, 8
CRC8L:
lds r18, _crc
eor r16, r18
ror r16
lds r16, _crc
brcc CRC8zero
ldi r18, 0x18
eor r16, r18
CRC8zero:
ror r16
sts _crc, r16
pop r16
lsr r16
push r16
dec r17
brne CRC8L
pop r16
pop r18
pop r17
pop r16
ret
;------------------------------------------------------------------------------
; Clear crc value
;------------------------------------------------------------------------------
CRC8Init:
push r16
ldi r16,0
sts _crc, r16
pop r16
ret
;------------------------------------------------------------------------------
; Copy crc value to r16
;------------------------------------------------------------------------------
GetCRC8:
lds r16, _crc
ret
;------------------------------------------------------------------------------
; End of crc8.asm file
;------------------------------------------------------------------------------ |
Example of use :
rcall CRC8Init ; CRC initialization
lds r16, (SerialNumber + 0) ; load first data byte
rcall CRC8Update ; update CRC (1)
lds r16, (SerialNumber + 1) ; load second data byte
rcall CRC8Update ; update CRC (2)
lds r16, (SerialNumber + 2) ; load third data byte
rcall CRC8Update ; update CRC (3)
lds r16, (SerialNumber + 3) ; load fourth data byte
rcall CRC8Update ; update CRC (4)
lds r16, (SerialNumber + 4) ; load fifth data byte
rcall CRC8Update ; update CRC (5)
lds r16, (SerialNumber + 5) ; load sixth data byte
rcall CRC8Update ; update CRC (6)
lds r16, (SerialNumber + 6) ; load seventh data byte
rcall CRC8Update ; update CRC (7)
rcall GetCRC8 ; read CRC value
rcall LCD_WriteHex8 ; display it on LCD |
How to validate transmission using CRC code
We can chceck CRC code in two ways. Fist way is compute CRC code from all data bytes (without CRC) and compare computed CRC with received CRC. The second way is compute CRC code from all data bytes with received CRC and test it value to zero.
|