> Anyone got any non-table driven known good code > with explanation of the bit > shifting operations? I learnt about CRC checking from this article when I needed to write a crc16 routine for Dallas keys (its a 16 page .pdf document) http://pdfserv.maxim-ic.com/arpdf/AppNotes/app27.pdf and heres the code I wrote (CCS c) after reading it (based on Table 5). Trouble is I can't remember how it works anymore and I think I've subsequently seen more efficient code on this list. Still for what its worth... Hope it helps, James Hillman /** Calculate the 16 bit crc **/ /* lo and hi are global variables initialised to 0 at the start */ /* lo is the low byte of the CRC */ /* hi is the high byte of the CRC */ /* a is the new byte of data to be crc'd */ void calc_crc16(byte a) { short c; /* parity bit */ byte p; /* local temp */ c=0; /* initialise parity */ p=0; /* find the "parity" of lo and a */ #asm btfsc lo,7 incf p,f btfsc lo,6 incf p,f btfsc lo,5 incf p,f btfsc lo,4 incf p,f btfsc lo,3 incf p,f btfsc lo,2 incf p,f btfsc lo,1 incf p,f btfsc lo,0 incf p,f btfsc a,7 incf p,f btfsc a,6 incf p,f btfsc a,5 incf p,f btfsc a,4 incf p,f btfsc a,3 incf p,f btfsc a,2 incf p,f btfsc a,1 incf p,f btfsc a,0 incf p,f #endasm if(bit_test(p,0)) c=1; /* set "parity" bit if odd */ /* crc16 routine */ a^=lo; /* see explanation in Dallas appnote 27 */ lo=hi; hi=a; if(c) lo^=0x01; c=shift_right(&a,1,c); if(c) lo^=0x40; c=(bit_test(a,7)); a^=hi; c=shift_right(&a,1,c); hi=a; if(c) lo^=0x80; } -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads