62,612
社区成员
发帖
与我相关
我的任务
分享
void y_crc(unsigned char *buff, unsigned short bufflen, unsigned char *lsb, unsigned char *msb)
{
unsigned short crc = 0xFFFF; // Preload to FFFF
unsigned short tempresults; // Just a temporary results holder
unsigned short bitindex; // 0 - 7
unsigned short byteindex; // The byte pointer into the byte array that holds
// the command to be checked.
unsigned char placeholder; // A place to put the byte while we work on it.
for (byteindex = 0; byteindex < bufflen ; byteindex++) // begin checking after SOH and before CRC bytes
{
placeholder = *(buff + byteindex);
for(bitindex = 0; bitindex <= 7; bitindex++)
{
tempresults = (crc >> 15) ^ (placeholder >> 7); // Shift CRC right 15 bits then do a bitwise XOR
crc <<= 1; // Shift CRC left one bit and store it in CRC
if(tempresults)
{
crc ^= 0x1021; // Standard CCITT Polynomial X16+X12+X5+1
}
placeholder <<= 1;
}
}
*lsb = crc&0x00ff;
*msb = (crc>>8)&0x00ff;
}