CRC代码实现

关于crc使用比较透彻且带源码的博文地址:

https://blog.csdn.net/huang_shiyang/article/details/50881305#0-tsina-1-63954-397232819ff9a47a7b7e80a40613cfe1

https://blog.csdn.net/liyuanbhu/article/details/7882789#0-tsina-1-16830-397232819ff9a47a7b7e80a40613cfe1

个人源码均测试通过验证:

废话不多说上源码

#include  "crc.h"

#include "stdio.h"

#include "system.h"

#include "flywheel.h"

#include "string.h"

#include "star_sensor1.h"

unsigned char Crc8_tmp[256] = { 0 };

//crc8 对应生成的crc码表

static unsigned char Crc8_Table[256] = {

0x00,0x07,0x0e,0x09,0x1c,0x1b,0x12,0x15,0x38,0x3f,0x36,0x31,0x24,0x23,0x2a,0x2d,

0x70,0x77,0x7e,0x79,0x6c,0x6b,0x62,0x65,0x48,0x4f,0x46,0x41,0x54,0x53,0x5a,0x5d,

0xe0,0xe7,0xee,0xe9,0xfc,0xfb,0xf2,0xf5,0xd8,0xdf,0xd6,0xd1,0xc4,0xc3,0xca,0xcd,

0x90,0x97,0x9e,0x99,0x8c,0x8b,0x82,0x85,0xa8,0xaf,0xa6,0xa1,0xb4,0xb3,0xba,0xbd,

0xc7,0xc0,0xc9,0xce,0xdb,0xdc,0xd5,0xd2,0xff,0xf8,0xf1,0xf6,0xe3,0xe4,0xed,0xea,

0xb7,0xb0,0xb9,0xbe,0xab,0xac,0xa5,0xa2,0x8f,0x88,0x81,0x86,0x93,0x94,0x9d,0x9a,

0x27,0x20,0x29,0x2e,0x3b,0x3c,0x35,0x32,0x1f,0x18,0x11,0x16,0x03,0x04,0x0d,0x0a,

0x57,0x50,0x59,0x5e,0x4b,0x4c,0x45,0x42,0x6f,0x68,0x61,0x66,0x73,0x74,0x7d,0x7a,

0x89,0x8e,0x87,0x80,0x95,0x92,0x9b,0x9c,0xb1,0xb6,0xbf,0xb8,0xad,0xaa,0xa3,0xa4,

0xf9,0xfe,0xf7,0xf0,0xe5,0xe2,0xeb,0xec,0xc1,0xc6,0xcf,0xc8,0xdd,0xda,0xd3,0xd4,

0x69,0x6e,0x67,0x60,0x75,0x72,0x7b,0x7c,0x51,0x56,0x5f,0x58,0x4d,0x4a,0x43,0x44,

0x19,0x1e,0x17,0x10,0x05,0x02,0x0b,0x0c,0x21,0x26,0x2f,0x28,0x3d,0x3a,0x33,0x34,

0x4e,0x49,0x40,0x47,0x52,0x55,0x5c,0x5b,0x76,0x71,0x78,0x7f,0x6a,0x6d,0x64,0x63,

0x3e,0x39,0x30,0x37,0x22,0x25,0x2c,0x2b,0x06,0x01,0x08,0x0f,0x1a,0x1d,0x14,0x13,

0xae,0xa9,0xa0,0xa7,0xb2,0xb5,0xbc,0xbb,0x96,0x91,0x98,0x9f,0x8a,0x8d,0x84,0x83,

0xde,0xd9,0xd0,0xd7,0xc2,0xc5,0xcc,0xcb,0xe6,0xe1,0xe8,0xef,0xfa,0xfd,0xf4,0xf3,

};

#define CRC_8_WIDTH 8 //crc8 位宽始终为8

//陀螺采用crc8 算法配置(不同设备配置不同)

#define CRC8_FORMULA  0x07 //简记式0x07 (多项式x8+x2+x1+1)

#define CRC8_INITIAL_REMAINDER 0xff //使用的余数初始值0xff

#define CRC8_FINAL_XOR_VALUE 0x00 //结果异或值 0x00

static void Mk_crc8_table_256(unsigned char * crctable_8, unsigned char simple_formula)

{

unsigned int i;

unsigned char crc_8 = 0;

int bit;

for (i = 0; i < 256; i++)

{

crc_8 = i << (CRC_8_WIDTH - 8);

for (bit = 0; bit < 8; bit++)

{

if (crc_8 & (1 << (CRC_8_WIDTH - 1)))

{

crc_8 = (crc_8 << 1) ^ simple_formula;

}

else

{

crc_8 = crc_8 << 1;

}

}

crctable_8[i] = crc_8;

}

}

unsigned char Crc8_calculate(unsigned char * message, unsigned int nBytes, unsigned char remainder_init, unsigned char final_xor_value)

{

unsigned int offset;

unsigned char byte;

unsigned char remainder = remainder_init;

for (offset = 0; offset < nBytes; offset++)

{

byte = (remainder >> (CRC_8_WIDTH - 8)) ^ message[offset];

remainder = Crc8_Table[byte] ^ (remainder << 8);

}

return (remainder ^ final_xor_value);

}

unsigned char Crc8_refcalculate(unsigned char * message, unsigned int nBytes, unsigned char remainder_init, unsigned char final_xor_value)

{

unsigned int offset;

unsigned char byte;

unsigned char remainder = remainder_init;

while (nBytes--)

{

remainder = (remainder >> 8) ^ Crc8_tmp[(remainder & 0xff) ^ *message++];

}

return (remainder ^ final_xor_value);

}

unsigned short Crc16_tmp[256] = { 0 };

//crc16 对应生成的crc码表

static unsigned short Crc16_Table[256] = { 

0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,

0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,

0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,

0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,

0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,

0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,

0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,

0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,

0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,

0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,

0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,

0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,

0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,

0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,

0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,

0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,

0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,

0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,

0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,

0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,

0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,

0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,

0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,

0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,

0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,

0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,

0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,

0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,

0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,

0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,

0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,

0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 };

#define CRC_16_WIDTH 16 //crc16 位宽始终为16

//采用crc16 算法配置(不同设备配置不同) 这里飞轮参数不明,简记式使用0x1189获得表只有前16个字节对应...

#define CRC16_FORMULA   //简记式...

#define CRC16_INITIAL_REMAINDER //使用的余数初始值

#define CRC16_FINAL_XOR_VALUE //结果异或值

static void Mk_crc16_table_256(unsigned short * crctable_16, unsigned short simple_formula)

{

unsigned int i;

unsigned short cRc_16 = 0;

int bit;

for (i = 0; i < 256; i++)

{

cRc_16 = i << (CRC_16_WIDTH - 8);

for (bit = 0; bit < 8; bit++)

{

if (cRc_16 & (1 << (CRC_16_WIDTH - 1)))

{

cRc_16 = (cRc_16 << 1) ^ simple_formula;

}

else

{

cRc_16 = cRc_16 << 1;

}

}

crctable_16[i] = cRc_16;

}

}

unsigned short Crc16_calculate(unsigned char * message, unsigned int nBytes, unsigned short remainder_init, unsigned short final_xor_value)

{

unsigned int offset;

unsigned char byte;

unsigned short remainder = remainder_init;

for (offset = 0; offset < nBytes; offset++)

{

byte = (remainder >> (CRC_16_WIDTH - 8)) ^ message[offset];

remainder = Crc16_Table[byte] ^ (remainder << 8);

}

return (remainder ^ final_xor_value);

}

unsigned short Crc16_refcalculate(unsigned char * message, unsigned int nBytes, unsigned short remainder_init, unsigned short final_xor_value)

{

unsigned int offset;

unsigned char byte;

unsigned short remainder = remainder_init;

while (nBytes--)

{

remainder = (remainder >> 8) ^ Crc16_tmp[(remainder & 0xff) ^ *message++];

}

return (remainder ^ final_xor_value);

}

unsigned int Crc32_Table_Reflected[256] = { 0 };

//crc32 对应生成的crc码表

//static 

unsigned int Crc32_Table[256] = { 

0X00000000L,  0X04c11db7L,  0X09823b6eL,  0X0d4326d9L,  0X130476dcL,  0X17c56b6bL,  0X1a864db2L, 

0X1e475005L,  0X2608edb8L,  0X22c9f00fL,  0X2f8ad6d6L,  0X2b4bcb61L,  0X350c9b64L,  0X31cd86d3L,  

0X3c8ea00aL,  0X384fbdbdL,  0X4c11db70L,  0X48d0c6c7L,  0X4593e01eL,  0X4152fda9L,  0X5f15adacL, 

0X5bd4b01bL,  0X569796c2L,  0X52568b75L,  0X6a1936c8L,  0X6ed82b7fL,  0X639b0da6L,  0X675a1011L,  

0X791d4014L,  0X7ddc5da3L,  0X709f7b7aL,  0X745e66cdL,  0X9823b6e0L,  0X9ce2ab57L,  0X91a18d8eL, 

0X95609039L,  0X8b27c03cL,  0X8fe6dd8bL,  0X82a5fb52L,  0X8664e6e5L,  0Xbe2b5b58L,  0Xbaea46efL,  

0Xb7a96036L,  0Xb3687d81L,  0Xad2f2d84L,  0Xa9ee3033L,  0Xa4ad16eaL,  0Xa06c0b5dL, 0Xd4326d90L,  

0Xd0f37027L,  0Xddb056feL,  0Xd9714b49L,  0Xc7361b4cL,  0Xc3f706fbL,  0Xceb42022L,  0Xca753d95L,  

0Xf23a8028L,  0Xf6fb9d9fL,  0Xfbb8bb46L,  0Xff79a6f1L,  0Xe13ef6f4L,  0Xe5ffeb43L,  0Xe8bccd9aL,  

0Xec7dd02dL,  0X34867077L,  0X30476dc0L,  0X3d044b19L,  0X39c556aeL,  0X278206abL,  0X23431b1cL,  

0X2e003dc5L,  0X2ac12072L,  0X128e9dcfL,  0X164f8078L,  0X1b0ca6a1L,  0X1fcdbb16L,  0X018aeb13L,  

0X054bf6a4L,  0X0808d07dL,  0X0cc9cdcaL,  0X7897ab07L,  0X7c56b6b0L,  0X71159069L,  0X75d48ddeL,  

0X6b93dddbL,  0X6f52c06cL,  0X6211e6b5L,  0X66d0fb02L,  0X5e9f46bfL,  0X5a5e5b08L,  0X571d7dd1L,  

0X53dc6066L,  0X4d9b3063L,  0X495a2dd4L,  0X44190b0dL,  0X40d816baL,  0Xaca5c697L,  0Xa864db20L,  

0Xa527fdf9L,  0Xa1e6e04eL,  0Xbfa1b04bL,  0Xbb60adfcL,  0Xb6238b25L,  0Xb2e29692L,  0X8aad2b2fL,  

0X8e6c3698L,  0X832f1041L,  0X87ee0df6L,  0X99a95df3L,  0X9d684044L,  0X902b669dL,  0X94ea7b2aL,  

0Xe0b41de7L,  0Xe4750050L,  0Xe9362689L,  0Xedf73b3eL, 0Xf3b06b3bL,  0Xf771768cL,  0Xfa325055L,  

0Xfef34de2L,  0Xc6bcf05fL,  0Xc27dede8L,  0Xcf3ecb31L,  0Xcbffd686L,  0Xd5b88683L,  0Xd1799b34L,  

0Xdc3abdedL,  0Xd8fba05aL,  0X690ce0eeL,  0X6dcdfd59L,  0X608edb80L,  0X644fc637L,  0X7a089632L,  

0X7ec98b85L,  0X738aad5cL,  0X774bb0ebL,  0X4f040d56L,  0X4bc510e1L,  0X46863638L,  0X42472b8fL,  

0X5c007b8aL,  0X58c1663dL,  0X558240e4L,  0X51435d53L,  0X251d3b9eL,  0X21dc2629L,  0X2c9f00f0L,  

0X285e1d47L,  0X36194d42L,  0X32d850f5L,  0X3f9b762cL,  0X3b5a6b9bL,  0X0315d626L,  0X07d4cb91L,  

0X0a97ed48L,  0X0e56f0ffL,  0X1011a0faL,  0X14d0bd4dL,  0X19939b94L,  0X1d528623L, 0Xf12f560eL,  

0Xf5ee4bb9L,  0Xf8ad6d60L,  0Xfc6c70d7L,  0Xe22b20d2L,  0Xe6ea3d65L,  0Xeba91bbcL,  0Xef68060bL,  

0Xd727bbb6L,  0Xd3e6a601L,  0Xdea580d8L,  0Xda649d6fL,  0Xc423cd6aL,  0Xc0e2d0ddL,  0Xcda1f604L,  

0Xc960ebb3L,  0Xbd3e8d7eL,  0Xb9ff90c9L,  0Xb4bcb610L,  0Xb07daba7L,  0Xae3afba2L,  0Xaafbe615L,  

0Xa7b8c0ccL,  0Xa379dd7bL, 0X9b3660c6L,  0X9ff77d71L,  0X92b45ba8L,  0X9675461fL,  0X8832161aL,  

0X8cf30badL,  0X81b02d74L,  0X857130c3L,  0X5d8a9099L,  0X594b8d2eL,  0X5408abf7L,  0X50c9b640L,  

0X4e8ee645L,  0X4a4ffbf2L,  0X470cdd2bL,  0X43cdc09cL,  0X7b827d21L,  0X7f436096L,  0X7200464fL, 

0X76c15bf8L,  0X68860bfdL,  0X6c47164aL,  0X61043093L,  0X65c52d24L,  0X119b4be9L,  0X155a565eL, 

0X18197087L,  0X1cd86d30L,  0X029f3d35L,  0X065e2082L,  0X0b1d065bL,  0X0fdc1becL,  0X3793a651L, 

0X3352bbe6L,  0X3e119d3fL,  0X3ad08088L,  0X2497d08dL,  0X2056cd3aL,  0X2d15ebe3L,  0X29d4f654L,  

0Xc5a92679L,  0Xc1683bceL,  0Xcc2b1d17L,  0Xc8ea00a0L,  0Xd6ad50a5L,  0Xd26c4d12L, 0Xdf2f6bcbL,  

0Xdbee767cL,  0Xe3a1cbc1L, 0Xe760d676L,  0Xea23f0afL,  0Xeee2ed18L,  0Xf0a5bd1dL, 0Xf464a0aaL, 

0Xf9278673L,  0Xfde69bc4L,  0X89b8fd09L,  0X8d79e0beL,  0X803ac667L,  0X84fbdbd0L, 0X9abc8bd5L,  

0X9e7d9662L,  0X933eb0bbL,  0X97ffad0cL,  0Xafb010b1L,  0Xab710d06L,  0Xa6322bdfL,  0Xa2f33668L,

0Xbcb4666dL,  0Xb8757bdaL,  0Xb5365d03L,  0Xb1f740b4L };

#define CRC_32_WIDTH 32 

static void Mk_crc32_table_256(unsigned int * crctable_32, unsigned int simple_formula)

{

unsigned int i;

unsigned int cRc_32 = 0;

int bit;

for (i = 0; i < 256; i++)

{

cRc_32 = i << (CRC_32_WIDTH - 8);

for (bit = 0; bit < 8; bit++)

{

if (cRc_32 & (1 << (CRC_32_WIDTH - 1)))

{

cRc_32 = (cRc_32 << 1) ^ simple_formula;

}

else

{

cRc_32 = cRc_32 << 1;

}

}

crctable_32[i] = cRc_32;

}

}

unsigned int Crc32_calculate(unsigned char * message, unsigned int nBytes, unsigned int remainder_init, unsigned int final_xor_value)

{

unsigned int offset;

unsigned char byte;

unsigned int remainder = remainder_init;

for (offset = 0; offset < nBytes; offset++)

{

byte = (remainder >> (CRC_32_WIDTH - 8)) ^ message[offset];

remainder = Crc32_Table[byte] ^ (remainder << 8);

}

return (remainder ^ final_xor_value);

}

unsigned int Crc32_refcalculate(unsigned char * message, unsigned int nBytes, unsigned int remainder_init, unsigned int final_xor_value)

{

unsigned int offset;

unsigned char byte;

unsigned int remainder = remainder_init;

while (nBytes--)

{

remainder = (remainder >> 8) ^ Crc32_Table_Reflected[(remainder & 0xff) ^ *message++];

}

return (remainder ^ final_xor_value);

}

unsigned char  reflected(unsigned char  b)

{

unsigned char  c = 0;

for (unsigned char  i = 0; i<8; i++)

{

  c <<= 1;

  if (b & 1) c |= 1;

  b >>= 1;

}

return c;

}

unsigned char reflected_c(unsigned char d)

{

unsigned char c = 0;

for (unsigned int i = 0; i<8; i++)

{

c <<= 1;

if (d & 1) c |= 1;

d >>= 1;

}

return c;

}

unsigned int reflected_w(unsigned short d)

{

unsigned short c = 0;

for (unsigned int i = 0; i<16; i++)

{

c <<= 1;

if (d & 1) c |= 1;

d >>= 1;

}

return c;

}

unsigned int reflected_dw(unsigned int d)

{

unsigned int c = 0;

for (unsigned int i = 0; i<32; i++)

{

c <<= 1;

if (d & 1) c |= 1;

d >>= 1;

}

return c;

}

void RefTable_Init()

{

unsigned int i;

for (i = 0; i<256; i++)

{

Crc32_Table_Reflected[i] = reflected_dw(Crc32_Table[reflected(i)]);

Crc16_tmp[i] = reflected_w(Crc16_Table[reflected(i)]);

Crc8_tmp[i] = reflected_c(Crc8_Table[reflected(i)]);

}

}

int main(void)

{

//unsigned char tmp_[12] = { 0x90, 0x78, 0x00, 0x00, 0x78, 0x00, 0x00, 0x88, 0x00, 0x00, 0x5F, 0xe1 };

unsigned char tmp_[12] = { 0x12, 0x33, 0x3f,0x2f, 0x0f, 0x84, 0x5e, 0x57, 0x38, 0x29 };

unsigned char aa[30] = { 0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x7e,0x00,0x11,0x22};// {0x75, 0x62};

unsigned char cc[30] = { 0x11,0x22,0x7e,0x44,0x55,0x0d,0x77,0x88,0x99,0x00,0x11 };// {0x75, 0x62};

unsigned char bb[] = { 0x7E,0x77,0x40,0x00,0x06,0x6D,0x00,0x30,0x0d,0x40,0x00,0x52,0xDE,0x0D };// {0x75, 0x62};

unsigned char dd[] = { 0x7E,0x77,0x40,0x00,0x06,0x6D,0x77,0x00,0x7e,0x00,0x00,0x45,0x52,0x0D };// {0x75, 0x62};

unsigned short check_len = 12,i =0,tmp, u16_ret;

unsigned char send_buf[6] = { 0x00,0x30,0xd4,00,0,0 },u8_ret;

unsigned char recv_1[] = { 0x7e,0x40,0x1,0x0,0x14,0x75,0x74,0x95,0xa,0x0,0xf,0x27,0x0,0xa,0xd1,0x0,0x6c,0x1,0x79,0x1,0x73,0x1,0x89,0x12,0x2,0xb6,0x45,0xd };

char inputch[50] = { '\0' };

int sum,u32_ret,ret,type,datalen,start_data,xor;

ret = memcmp(Crc16_tmp, CRC16_CCITT_Tab, sizeof(Crc16_tmp));

while (1)

{

printf("请输入简记式:\r\n");

scanf_s("%x", &type);

printf("请输入数据长度:\r\n");

scanf_s("%d", &datalen);

printf("请输入余数初始值:\r\n");

scanf_s("%x", &start_data);

printf("请输入结果异或值:\r\n");

scanf_s("%x", &xor);

Mk_crc8_table_256(Crc8_Table, type);

RefTable_Init();

u8_ret = Crc8_calculate(recv_1, datalen, start_data, xor);

printf("****crc is %x****\r\n", u8_ret);

u8_ret = Crc8_refcalculate(recv_1, datalen, start_data, xor);

printf("****refcrc is %x****\r\n", u8_ret);

//Mk_crc16_table_256(Crc16_Table, type);

//Mk_crc32_table_256(Crc32_Table, type);

//u32_ret = Crc32_calculate(tmp_, datalen, start_data, xor);

//printf("****crc is %x****\r\n", u32_ret);

//u32_ret = Crc32_refcalculate(tmp_, datalen, start_data, xor);

//printf("****refcrc is %x****\r\n", u32_ret);

}

//getchar();

return 0;

}

猜你喜欢

转载自blog.csdn.net/u011116085/article/details/85270542
CRC