网站上的算法大都是查512字节表的,对单片机来讲,太浪费空间了。
根据老古网上资料,修改并测试通过以下算法:
unsigned int crc_ta[16]={
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
};
unsigned int Crc16(unsigned char *ptr, unsigned char len)
{
unsigned int crc;
unsigned char da;
crc=0;
while(len--!=0)
{
da=crc>>12;
crc<<=4;
crc^=crc_ta[da^(*ptr/16)];
da=crc>>12;
crc<<=4;
crc^=crc_ta[da^(*ptr&0x0f)];
ptr++;
}
return(crc);
}
以下是资料和代码:
回复 楼主 的帖子
我推荐一个简单的
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 */
xiexiexxxxxxxxxxxxxxxxxxxxxxx