论坛元老
 
- 积分
- 3722
- 金钱
- 3722
- 注册时间
- 2011-5-23
- 在线时间
- 2013 小时
|
发表于 2017-8-21 15:17:25
|
显示全部楼层
CRC8 有多种poly
常用有两种
[mw_shl_code=c,true]/* crc8 poly = x8+x5+x4+1 = 0x131 */
static uint8_t crc8(const uint8_t *ptr, int len, uint8_t crc)
{
uint8_t i;
while(len--)
{
crc ^= *ptr++;
for(i = 0;i < 8;i++)
{
if(crc & 0x01)
{
/* 0x31(0011 0001) ==> 0x8C(1000 1100) */
crc = (crc >> 1) ^ 0x8C;
}
else
{
crc >>= 1;
}
}
}
return crc;
}[/mw_shl_code]
[mw_shl_code=c,true]/*
crc8 poly = 0x107 (x8+x2+x1+1)
*/
uint8_t crc8(const uint8_t *buf, uint32_t len)
{
uint32_t i, j;
uint8_t CRC = 0, _crc_poly = 0x07;
for (j=0; j<len; j++)
{
CRC ^= buf[j];
for(i = 0; i<8; i++)
{
if(CRC & 0x80)
CRC = (CRC << 1) ^ _crc_poly;
else
CRC <<= 1;
}
//CRC &= 0xff;
}
return CRC;
}
[/mw_shl_code] |
|