CRC8算法表有两种形式,第一种是256个字节的表,第二种是高4位16字节,低4位16字节。
根据不同多项式算出对应的表中的数据
第一种方法:多项式( 100110001----0x131)
#include <iostream>
using namespace std;
unsigned char table[256] = {0};
unsigned char CRC8_Table(unsigned char *p, char counter)
{
unsigned char res = 0x00;
for(;counter >0 ; counter--)
{
res = table[res ^ *p];
p++;
}
return res;
}
//反序
void crc_table_create(unsigned char *pdata, unsigned char factor)
{
unsigned char temp;
for(int i = 0;i < 256; i++)
{
pdata[i] = i;
}
for(int i = 0;i < 256; i++)
{
for(int j = 7;j >= 0; j--)
{
temp = pdata[i] & 0x01;//take the last bit
if(temp) //the last bit is 1
{
pdata[i] = pdata[i] >> 1;
pdata[i] ^= factor;
}
else
{
pdata[i] = pdata[i] >> 1;
}
}
}
}
int main(void)
{
int i,j,k = 0;
short temp = 0x00;
int poly_src = 0x31;//1 0011 0001(B) the top 1 is hidden
int poly = 0x8c;// 1000 1100(B)
unsigned short reg = 0x3e;
unsigned char data[2] = {0x02,0x43};
unsigned char res = 0x00;
//start of calculating the CRC8 table
// for(i = 0;i < 256; i++){
// table[i] = i;
// }
// for(i = 0;i < 256; i++){
// for(j = 7;j >= 0; j--){
// temp = table[i] & 0x01;//take the last bit
// if(temp){
// table[i] = table[i] >> 1;
// table[i] ^= poly;
// }
// else{
// table[i] = table[i] >> 1;
// }
// }
// }
//end of calculating the CRC table
crc_table_create(table, poly);
for(i = 0;i < 256; i++)
{
cout<<table[i]<<endl;
}
res = CRC8_Table(data, 2);
printf("res=0x%x\n",res);
while(1);
}
计算出的表格是:
const char CRC8Table[]={
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
};
第二种方法:多项式( 00101111----0x2F)
#include <stdio.h>
unsigned char crc_table_create(unsigned char tableIndex, unsigned char factor)
{
unsigned char i;
unsigned char crc; /* 计算的初始crc值 */
crc = tableIndex^0xFF; /* 每次先与需要计算的数据异或,计算完指向下一数据 */
for (i=8; i>0; --i) /* 下面这段计算过程与计算一个字节crc一样 */
{
if (crc & 0x80)
crc = (crc << 1) ^ factor;
else
crc = (crc << 1);
}
return (crc);
}
int main(void)
{
unsigned char crc_tab[16] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
unsigned char crc_tab1[16] = {0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0};
unsigned char crc_res[16];
unsigned char crc_res1[16];
for(int i=0; i<16; i++)
{
crc_res[i] = crc_table_create(crc_tab[i], 0x2F);
printf("crc_res[%d] = 0x%x\n",i, crc_res[i]);
}
for(int i=0; i<16; i++)
{
crc_res1[i] = crc_table_create(crc_tab1[i], 0x2F);
printf("crc_res1[%d] = 0x%x\n",i, crc_res1[i]);
}
}
算出的表格为:
/* Constant array CRC8 */
static const uint8 LIB_Crc8Table1[16]={
0x42u, 0x6du, 0x1cu, 0x33u, 0xfeu, 0xd1u, 0xa0u, 0x8fu,
0x15u, 0x3au, 0x4bu, 0x64u, 0xa9u, 0x86u, 0xf7u, 0xd8u};
static const uint8 LIB_Crc8Table2[16]={
0x42u, 0xecu, 0x31u, 0x9fu, 0xa4u, 0x0au, 0xd7u, 0x79u,
0xa1u, 0x0fu, 0xd2u, 0x7cu, 0x47u, 0xe9u, 0x34u, 0x9au};
CRC8算法
/**
* @brief This function calculates a CRC8 over the data buffer
* @param LIB_TempInputCrc8_cp[in]: pointer to the input data
* @param LIB_TempLengthCrc8_u16[in]: Length of the input data
* @return Calculated CRC8
* @details Local variables
* Loop over all byte
* Execute CRC algorithm
* Return inverted result
* @ReqKey MOD_LIB-64, MOD_LIB-65
*/
uint8 LIB_Crc8(const uint8 *LIB_TempInputCrc8_cp, const uint16 LIB_TempLengthCrc8_u16)
{
/* Constant array CRC8 */
static const uint8 LIB_Crc8Table1[16]={
0x42u, 0x6du, 0x1cu, 0x33u, 0xfeu, 0xd1u, 0xa0u, 0x8fu,
0x15u, 0x3au, 0x4bu, 0x64u, 0xa9u, 0x86u, 0xf7u, 0xd8u};
static const uint8 LIB_Crc8Table2[16]={
0x42u, 0xecu, 0x31u, 0x9fu, 0xa4u, 0x0au, 0xd7u, 0x79u,
0xa1u, 0x0fu, 0xd2u, 0x7cu, 0x47u, 0xe9u, 0x34u, 0x9au};
/* Local Variables */
uint8 LIB_TempCrc8_u8 = 0xFFu;
uint16 LIB_TempIndexCrc8_u16;
/* Loop over all bytes */
for (LIB_TempIndexCrc8_u16 = 0u; LIB_TempIndexCrc8_u16 < LIB_TempLengthCrc8_u16; LIB_TempIndexCrc8_u16++)
{
/* CRC Algorithm */
LIB_TempCrc8_u8 = LIB_TempInputCrc8_cp[LIB_TempIndexCrc8_u16] ^ LIB_TempCrc8_u8;
LIB_TempCrc8_u8 = (LIB_Crc8Table1 [LIB_TempCrc8_u8 & 0x0Fu]) ^ (LIB_Crc8Table2 [LIB_TempCrc8_u8 >> 4u]);
}
return (LIB_TempCrc8_u8^0xFF);
}