Examples of CRC algorithm for uPs


[Document Version: 1.00] [Last Updated: 1/2/97]

1. PIC16C71 crc code in asm

(From Robert Reimiller)

I can't make any guarantees, but this is a bit of code I once used on a PIC16C71. Actually, I have found that a 16 bit checksum formed by one byte being the exclusive-or and the other byte the addition does a pretty good job of finding errors and takes a lot less code.

; Calculate CRC, xtemp has new byte. Returns with w=xtemp
docrc   movlw   8
        movwf   crcbits                ; number of bits to process
        movf    xtemp,w                ; get original byte
        movwf   ctemp2                 ; shifter
dc0     clrf    ctemp1                 ; clear flag
        movf    ctemp2,w               ; get test byte
        xorwf   crcl,w                 ; eor with lsb of crc
        andlw   1                      ; we only care about lsb
        bz      dc2                    ; don't do polynomial
        movlw   0x48
        xorwf   crcl                   ; low order polynomial
        movlw   1
        xorwf   crch                   ; high order polynomial
        bsf     ctemp1,0               ; set lsb of flag
dc2     rrf     ctemp1                 ; move flag bit to carry
        rlf     crcl                   ; into lsb of crcl
        rlf     crch                   ; complete 16 bit crc shift
        rrf     ctemp2                 ; next higher bit
        decfsz  crcbits                ; until done all 8
        goto    dc0
        movf    xtemp,w                ; return with binary value

Please check attribution section for Author of this document! This article was written by filipg@repairfaq.org [mailto]. The most recent version is available on the WWW server http://www.repairfaq.org/filipg/ [Copyright] [Disclaimer]