这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » CRC校验的C语言实现

共11条 2/2 1 2 跳转至
菜鸟
2015-04-18 13:44:03     打赏
11楼
/* 
CRC校验算法实现 C语言代码实现 
文件 CRC16.h 
*/ 
#ifndef _CRC16_h 
#define _CRC16_h 
static unsigned char auchCRCHi[] = {
0x0,0x80,0x80,0x0,0x80,0x0,0x0,0x80,0x80,0x0,
0x0,0x80,0x0,0x80,0x80,0x0,0x80,0x0,0x0,0x80,
0x0,0x80,0x80,0x0,0x0,0x80,0x80,0x0,0x80,0x0,
0x0,0x80,0x80,0x0,0x0,0x80,0x0,0x80,0x80,0x0,
0x0,0x80,0x80,0x0,0x80,0x0,0x0,0x80,0x0,0x80,
0x80,0x0,0x80,0x0,0x0,0x80,0x80,0x0,0x0,0x80,
0x0,0x80,0x80,0x0,0x81,0x1,0x1,0x81,0x1,0x81,
0x81,0x1,0x1,0x81,0x81,0x1,0x81,0x1,0x1,0x81,
0x1,0x81,0x81,0x1,0x81,0x1,0x1,0x81,0x81,0x1,
0x1,0x81,0x1,0x81,0x81,0x1,0x1,0x81,0x81,0x1,
0x81,0x1,0x1,0x81,0x81,0x1,0x1,0x81,0x1,0x81,
0x81,0x1,0x81,0x1,0x1,0x81,0x1,0x81,0x81,0x1,
0x1,0x81,0x81,0x1,0x81,0x1,0x1,0x81,0x83,0x3,
0x3,0x83,0x3,0x83,0x83,0x3,0x3,0x83,0x83,0x3,
0x83,0x3,0x3,0x83,0x3,0x83,0x83,0x3,0x83,0x3,
0x3,0x83,0x83,0x3,0x3,0x83,0x3,0x83,0x83,0x3,
0x3,0x83,0x83,0x3,0x83,0x3,0x3,0x83,0x83,0x3,
0x3,0x83,0x3,0x83,0x83,0x3,0x83,0x3,0x3,0x83,
0x3,0x83,0x83,0x3,0x3,0x83,0x83,0x3,0x83,0x3,
0x3,0x83,0x2,0x82,0x82,0x2,0x82,0x2,0x2,0x82,
0x82,0x2,0x2,0x82,0x2,0x82,0x82,0x2,0x82,0x2,
0x2,0x82,0x2,0x82,0x82,0x2,0x2,0x82,0x82,0x2,
0x82,0x2,0x2,0x82,0x82,0x2,0x2,0x82,0x2,0x82,
0x82,0x2,0x2,0x82,0x82,0x2,0x82,0x2,0x2,0x82,
0x2,0x82,0x82,0x2,0x82,0x2,0x2,0x82,0x82,0x2,
0x2,0x82,0x2,0x82,0x82,0x2};
/* CRC低位字节值表*/ 
static unsigned char auchCRCLo[] = {
0x0,0x5,0xf,0xa,0x1b,0x1e,0x14,0x11,0x33,0x36,
0x3c,0x39,0x28,0x2d,0x27,0x22,0x63,0x66,0x6c,0x69,
0x78,0x7d,0x77,0x72,0x50,0x55,0x5f,0x5a,0x4b,0x4e,
0x44,0x41,0xc3,0xc6,0xcc,0xc9,0xd8,0xdd,0xd7,0xd2,
0xf0,0xf5,0xff,0xfa,0xeb,0xee,0xe4,0xe1,0xa0,0xa5,
0xaf,0xaa,0xbb,0xbe,0xb4,0xb1,0x93,0x96,0x9c,0x99,
0x88,0x8d,0x87,0x82,0x83,0x86,0x8c,0x89,0x98,0x9d,
0x97,0x92,0xb0,0xb5,0xbf,0xba,0xab,0xae,0xa4,0xa1,
0xe0,0xe5,0xef,0xea,0xfb,0xfe,0xf4,0xf1,0xd3,0xd6,
0xdc,0xd9,0xc8,0xcd,0xc7,0xc2,0x40,0x45,0x4f,0x4a,
0x5b,0x5e,0x54,0x51,0x73,0x76,0x7c,0x79,0x68,0x6d,
0x67,0x62,0x23,0x26,0x2c,0x29,0x38,0x3d,0x37,0x32,
0x10,0x15,0x1f,0x1a,0xb,0xe,0x4,0x1,0x3,0x6,
0xc,0x9,0x18,0x1d,0x17,0x12,0x30,0x35,0x3f,0x3a,
0x2b,0x2e,0x24,0x21,0x60,0x65,0x6f,0x6a,0x7b,0x7e,
0x74,0x71,0x53,0x56,0x5c,0x59,0x48,0x4d,0x47,0x42,
0xc0,0xc5,0xcf,0xca,0xdb,0xde,0xd4,0xd1,0xf3,0xf6,
0xfc,0xf9,0xe8,0xed,0xe7,0xe2,0xa3,0xa6,0xac,0xa9,
0xb8,0xbd,0xb7,0xb2,0x90,0x95,0x9f,0x9a,0x8b,0x8e,
0x84,0x81,0x80,0x85,0x8f,0x8a,0x9b,0x9e,0x94,0x91,
0xb3,0xb6,0xbc,0xb9,0xa8,0xad,0xa7,0xa2,0xe3,0xe6,
0xec,0xe9,0xf8,0xfd,0xf7,0xf2,0xd0,0xd5,0xdf,0xda,
0xcb,0xce,0xc4,0xc1,0x43,0x46,0x4c,0x49,0x58,0x5d,
0x57,0x52,0x70,0x75,0x7f,0x7a,0x6b,0x6e,0x64,0x61,
0x20,0x25,0x2f,0x2a,0x3b,0x3e,0x34,0x31,0x13,0x16,
0x1c,0x19,0x8,0xd,0x7,0x2};
/*该方法中生成多项式必须唯一确定*/
/*利用第一种方法的结论,以前一次crc校验值为输入,由其生成的后一次crc校验值为输出,初始输入值为0*/
unsigned short CRC16(unsigned char *puchMsg,unsigned short usDataLen ) 
{ 
unsigned short uchCRCHi = 0x00 ; /* 高CRC字节初始化 */ 
unsigned char  uchCRCLo = 0x00 ; /* 低CRC 字节初始化 */ 
unsigned short uIndex ; /* CRC循环中的索引 */ 
while (usDataLen--) /* 传输消息缓冲区 */ 
{ 
uIndex = uchCRCHi ^ *puchMsg++ ; /* 计算CRC前八位初始值,并作为索引 */ 
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex]; /* 利用索引计算CRC前八位初始值对应的crc校验值,后叠加在CRC后八位初始值上*/ 
uchCRCLo = auchCRCLo[uIndex]; /*根据第一种算法,生成的后8位crc校验值与CRC后八位初始无关,所以有相同的索引,最后一位必然为0*/
} 
return (uchCRCHi << 8 | uchCRCLo); 
} 
#endif //_CRC16_h 

/* 
CRC16.c 
生成 CRC码实现 
*/ 
#include <stdio.h> 
#include "CRC2.h" 
void main() 
{ 
unsigned char ppp[13] = {0xDB,0x6A}; 
    unsigned short result;  
// 计算CRC 
    result = CRC16(ppp,2); 
printf("%x\n",result); 
} 

 


共11条 2/2 1 2 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]