#define POLY 0x1021
/**
* Calculating CRC-16 in 'C'
* @para addr, start of data
* @para num, length of data
* @para crc, incoming CRC
*/
uint16_t crc16(unsigned char *addr, int num, uint16_t crc)
{
int i;
; num--) /* Step through bytes in memory */
{
crc = crc ^ (*addr++ << ); /* Fetch byte from memory, XOR into CRC top byte*/
; i < ; i++) /* Prepare to rotate 8 bits */
{
if (crc & 0x8000) /* b15 is set... */
crc = (crc << ) ^ POLY; /* rotate and XOR with polynomic */
else /* b15 is clear... */
crc <<= ; /* just rotate */
} /* Loop for 8 bits */
crc &= 0xFFFF; /* Ensure CRC remains 16-bit value */
} /* Loop until num=0 */
return(crc); /* Return updated CRC */
}
转自http://blog.csdn.net/liyuanbhu/article/details/7882789