crc1021.c

来自「单片机程序,和上位机通讯,采集下位机的射频信号的功率,接收上位机的指令并向上位机」· C语言 代码 · 共 34 行

C
34
字号
#include "fDETDeclare.H"
#include "DETConst.H"
//#include "stdlib.h"

/* CRC余式表 */
static const unsigned int code Crc1021Table[16] = {
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef};
//按生成多项式0X1021进行CRC运算
//输入参数:	*Buff:	需进行CRC运算的数据
//				Len:	运算的数据字节个数
//输出参数:
//				*h:	CRC的高8位
//				*l:	CRC的低8位
void CRC1021(unsigned char *Buff, int Len,unsigned char *h,unsigned char *l)
{
    unsigned int crc;
    unsigned char da;
    crc=0;
    while((Len--)!=0) {
         da=((unsigned char)(crc/256))/16;      /* 暂存CRC的高四位 */
         crc<<=4;                   /* CRC右移4位,相当于取CRC的低12位)*/
         crc^=Crc1021Table[da^((*Buff)/16)];     /* CRC的高4位和本字节的前半字节相加后查表计算CRC,
        然后加上上一次CRC的余数 */
         da=((unsigned char)(crc/256))/16;      /* 暂存CRC的高4位 */
         crc<<=4;                   /* CRC右移4位, 相当于CRC的低12位) */
         crc^=Crc1021Table[da^((*Buff)&0x0f)];   /* CRC的高4位和本字节的后半字节相加后查表计算 */
         Buff++;
    }
    *h = (unsigned char)(crc>> 8);
    *l = (unsigned char)(crc);
    return;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?