Farba Research AVR Assembler Example

;                       F:\AVR\CRC8.ALS                29-Apr-00   07:53:22am   
;                       ---------------                                         
;
;Library subroutine CRC8
;Calculates CRC-8 of a string
;runs on any Atmel AVR microcontroller that has SRAM stack
;(result matches Dallas Semiconductor's One-Wire [DOW] CRC8 algorithm)
;(use this subroutine to check the CRC8 on serial numbers from Dallas iButtons)
;
;written by  E. Nicholas Cupery  Farba Research  29 April 2000
;
;on input:
;     X register must point to the string in SRAM
;     rCount register must hold the byte-count of the string  [0 means 256]
;on exit:
;     rCRC8 holds the CRC-8 result
;     all other registers are preserved
;     c-bit is indeterminate
;
CRC8:	push	XH			;save XH
	push	XL			;save XL
	push	rChar			;save rChar
	push	rCount			;save rCount
	push	rBitCount		;save rBitCount
	push	rTemp			;save rTemp

	clr	rCRC8			;start with a zero CRC-8


	;begin loop to do each byte in the string
CRC8BYT:ld	rChar,X+		;fetch next string byte and bump pointer
	ldi	rBitCount,8		;load the bit-counter for this byte

	;begin loop to do each bit in the byte
CRC8BIT:mov	rTemp,rChar		;get a temporary copy of current data
	eor	rTemp,rCRC8		;XOR the data byte with the current CRC
	lsr	rCRC8			;position to the new CRC
	lsr	rChar			;position to next bit of this byte	
	lsr	rTemp			;get low bit of old result into c-bit
	brcc	CRC8NA			;br if low bit was clear (no adjustment)
	ldi	rTemp,$8C		;magical value needed for CRC-8s
	eor	rCRC8,rTemp		;fold in the magic CRC8 value
CRC8NA:	dec	rBitCount		;count the previous bit done
	brne	CRC8BIT			;br if not done all bits in this byte
	;end loop to do each bit in the byte

	dec	rCount			;count this byte done
	brne	CRC8BYT			;br if not done all bytes in the string
	;end loop to do each byte in the string


        pop	rTemp			;restore rTemp
        pop	rBitCount		;restore rBitCount
	pop	rCount			;restore rCount
	pop	rChar			;restore rChar
	pop	XL			;restore XL
	pop	XH			;restore XH
	ret				;return to caller
;
;END OF SUBROUTINE CRC8

Farba Products WORK