1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| The following C code represents the way the CRC16 is implemented.
WORD is defined as an unsigned 16bits type.
Make sure to initialize the CRC16 table with InitCRC16(), before you use CalcCRC16().
WORD CRC16Table[256] ;
// initialize the CRC16 table
extern void InitCRC16( void ) {
WORD i, j ;
WORD crc ;
for ( i = 0 ; i < 256 ; i += 1 ) {
for ( crc = i << 8, j = 0 ; j < 8 ; j += 1 )
crc = ( crc << 1 ) ^ ( ( crc & 0x8000 ) ? 0x1021 : 0 ) ;
CRC16Table[ i ] = crc ;
}
}
// calculate the crc of a char array pointed at by p
extern WORD CalcCRC16( unsigned char * p, WORD size ) {
WORD crc = 0xFFFF ;
WORD i ;
for ( i = 0 ; i < size ; i++, p++ ) // for all chars
crc = CRC16Table[ ( ( crc >> 8 ) & 255 ) ] ^ ( crc << 8 ) ^ *p ;
return crc ;
} |