CRC知识点总结-1
美信:crc8
1.按位进行运算
void Cal_Crc8(unsigned char num) { unsigned char i = 0; unsigned char j = 0; unsigned char temp = 0; // 临时存储 unsigned char romb = 0; // rom一位 crc8 = 0; for ( i = 0; i < num; i++) { for (j = 0; j < 8; j++) { romb = (dat[i]>>j) & 1; temp = (crc8 & 1) ^ romb; crc8 = (crc8 & 0xf7) + ((crc8 & 0x08) ^ (temp << 3)); crc8 = (crc8 & 0xef) + ((crc8 & 0x10) ^ (temp << 4)); crc8 = crc8 >> 1; crc8 = (crc8 & 0x7f) + (temp << 7); } } }
2.按字节直接运算
unsigned char cal_table_low_first(unsigned char value) { unsigned char i, crc; crc = value; /* 同样需要计算8次 */ for (i=8; i>0; --i) { if (crc & 0x01) /* 反序异或变成判断最低位是否为1 */ /* 数据变成往右移位了 */ /* 计算的多项式从0x31(0011 0001)变成了0x8C (1000 1100) */ /* 多项式值,原来的最高位变成了最低位,原来的最低位变成最高位,8位数据高低位交换一下位置 */ crc = (crc >> 1) ^ 0x8C; else crc = (crc >> 1); } return crc; }