我推荐一个简单的
unsigned int fcs_calc(unsigned char *pr) /* CRC16.c */
{ unsigned int number_of_databytes;
unsigned int calculate_or_check_crc;
unsigned int i, j;
unsigned int fcs;
number_of_databytes =UsbLength;
calculate_or_check_crc = CALC_CRC;
if(calculate_or_check_crc == CALC_CRC)
{ number_of_databytes = UsbLength; /* 数组长 */
}
else /* check CRC */
{ number_of_databytes = UsbLength + 2;
}
fcs = PRESET_VALUE;
for(i = 1; i < number_of_databytes; i++)
{ fcs = fcs ^ ((unsigned int)(pr));
for (j = 0; j < 8; j++)
{ if (fcs & 0x0001)
{ fcs = (fcs >> 1) ^ POLYNOMIAL;
}
else
{ fcs = (fcs >> 1);
}
} /* for j */
} /* for i */
fcs = ~fcs;
return(fcs);
} /* fcs_calc */