8位CRC 数据校验

   传输数据时候需要生成8位 CRC校验码。

http://depa.usst.edu.cn/chenjq/www2/software/crc/CRC_Javascript/CRCcalculation.htm


   以下为我这边确认可以用的校验代码;

其中CRC polynom  为:0x97.

uint8_t crc8_checksum(uint8_t *ptr,uint8_t len)
{
uint8_t i;
uint8_t crc = 0;
ptr++;

while(len--!=0)
{
for(i=0x80;i!=0;i/=2)
{
if((crc&0x80)!=0)
{
crc*=2;
crc^=0x97;
}
else
crc*=2;


if((*ptr&i)!=0)
crc^=0x97;
}
ptr++;
}
return crc;
}


ptr指向要校验的数组首地址,len为数组的长度。返回值crc为校验码。


猜你喜欢

转载自blog.csdn.net/huofeng_2008/article/details/49075341