please dont rip this site

PIC Microcontroller Bit Math Method

CRC 16/32 Bit

By Dr. Imre Bartfai

SX version also available

; What you need is to define the polynomial. E. g. for the CCITT one:

poly0   equ     0x21
poly1   equ     0x10

; Do not forget to initialize <crcval> = <crch:crcl> before the 1st call
; e. g. with 0xFF or with zeroes, as the particular CRC algorithm
; requires.

;This subroutine calculates a 16-bit CRC
;--------
; CrcUpd: update <crcval> using <W>
;
;
        CBLOCK     crcblk
           crch
           crcl
           saved
           oldcch
           i
        ENDC
CrcUpd  movwf   saved           ; j = W
        movlw   d'8'            ; W = 8
        movwf   i               ; i = W
_loop   movfw   crch
        movwf   oldcch          ; temporary save
        clrc                    ; clear carry for rlf
        rlf     crcl            ; crc << 1
        rlf     crch
        movfw   saved           ; the char read
        xorwf   oldcch,F        ; test with old high
        btfss   oldcch,7        ; if bit set, apply mask
        b       _notset         ; otherwise skip
        movlw   poly0
        xorwf   crcl,F
        movlw   poly1
        xorwf   crch,F
_notset clrc                    ; for rlf
        rlf     saved,F         ; next bit of saved
        decfsz  i,F
        b       _loop
        return
;
; End CrcUpd

John Payson replies: Here's a version which should be faster: 38 instructions with no loops. When invoked, it will update crcH and crcL based upon the new input value. Note that crcL is entirely a function of crcH, while crcH is a function of crcH, xor'ed with the 'old' value of crcL. Basically, each bit in crcH will affect certain other bits in the output; each bit in crcL will affect only the corresponding bit in crcH [which, on the next iteration, will affect bits in both crcL and crcH].
; Simple CRC routine
; from John Payson 1998-10-23
   xorwf  crcH
; Compute the LSB first [based upon MSB]
   movlw  0
   btfsc  crcH,0
    xorlw 33
   btfsc  crcH,1
    xorlw 66
   btfsc  crcH,2
    xorlw 132
   btfsc  crcH,3
    xorlw 8
   btfsc  crcH,4
    xorlw 49
   btfsc  crcH,5
    xorlw 98
   btfsc  crcH,6
    xorlw 196
   btfsc  crcH,7
    xorlw 136
; Swap crcL with W
   xorwf  crcL,f
   xorwf  crcL,w
   xorwf  crcL,f
; Next compute the MSB [note W holds old LSB]
   btfsc  crcH,0
    xorlw 16
   btfsc  crcH,1
    xorlw 32
   btfsc  crcH,2
    xorlw 64
   btfsc  crcH,3
    xorlw 129
   btfsc  crcH,4
    xorlw 18
   btfsc  crcH,5
    xorlw 36
   btfsc  crcH,6
    xorlw 72
   btfsc  crcH,7
    xorlw 145
   movwf  crcH

Interested:


file: /Techref/microchip/crc.htm, 3KB, , updated: 2008/10/18 14:58, local time: 2024/11/5 13:12,
TOP NEW HELP FIND: 
3.139.81.254:LOG IN
©2024 PLEASE DON'T RIP! THIS SITE CLOSES OCT 28, 2024 SO LONG AND THANKS FOR ALL THE FISH!

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/techref/microchip/crc.htm"> PIC Microcontroller Bit Math Method - CRC</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?