EXPERT Standard Series User Manual
227
CRC Check
Considering the need to improve speed, CRC-16 is usually realized by adopting
the form of table, and the following is the C language source code to realize CRC-16,
note that high-low bytes have been exchanged in the final result, which means the
result is the CRC checksum to be sent.
unsigned short CRC16 ( unsigned char
*msg, unsigned char length)
/* The function returns the CRC as a
unsigned short type */
{
unsigned char uchCRCHi = 0xFF ; /* high byte of CRC initialized */
unsigned char uchCRCLo =
0xFF ;
/* low byte of CRC initialized */
unsigned uIndex ;
/* index into CRC lookup table */
while (length--)
/* pass through message buffer */
{
uIndex = uchCRCLo ^
*msg++ ;
/* calculate the CRC */
uchCRCLo = uchCRCHi ^
(crcvalue[uIndex] >>8);
uchCRCHi
=crcvalue[uIndex]&0xff;
}
return (uchCRCHi |
uchCRCLo<<8) ;
}
/* Table of CRC values */
const unsigned int crcvalue[ ] = {
0x0000,0xC1C0,0x81C1,0x4001,0x01C3,0xC003,0x8002,0x41C2,0x01C6,0xC0
06,0x8007,0x41C7,
0x0005,0xC1C5,0x81C4,0x4004,0x01CC,0xC00C,0x800D,0x41CD,0x000F,0xC
1CF,0x81CE,0x400E,