/* CRC-CCITT : */ /* */ /* crc polynomial = 11021H */ #define CRC_INIT 0xFFFF /* all bits are 1, in case of leading zeroes */ /* Complement CRC before sending in case of extra zeroes. */ #define CRC_MAGIC 0x1D0F /* The received CRC must match this number. */ unsigned short crccitt(unsigned short crc,unsigned char data); /* This function is used to generate the crc table. */ static unsigned short crccittx( unsigned short crc, /* old crc value */ unsigned char data /* data to be crc'd */ ) { crc = ((crc << 8) + (crc >> 8)) ^ ((int)data); crc = crc ^ ((crc & 0x00ff) >> 4); crc = crc ^ ((crc & 0x00ff) << 12) ^ ((crc & 0x00ff) << 5); return crc; } /* This crc routine uses a table lookup. */ unsigned short crccitt( unsigned short crc, /* old crc value */ unsigned char data /* data to be crc'd */ ) { static int inited = 0; static unsigned short crctbl[256]; if(!inited){ int i; for (i = 0; i < 256; i++) crctbl[i] = crccittx(0,(unsigned char)i); inited = 1; } crc = ((crc >> 8) ^ data) + (crc << 8); crc = (crc & 0xff00) ^ crctbl[(crc & 0x00ff)]; return crc; }