//取Word变量的高位字节、低位字节的宏
#define WORD_LO(xxx) ((unsigned char) ((unsigned int)(xxx) & 255))
#define WORD_HI(xxx) ((unsigned char) ((unsigned int)(xxx) >> 8))
//CRC校验计算函数
unsigned int cal_crc(unsigned char *ptr, unsigned int len)
{
unsigned int crc=0xffff;
unsigned char i;
while(len!=0)
{
crc^=*ptr;
for(i=0;i<8;i++)
{
if((crc&0x0001)==0) crc=crc>>1;
else
{
crc=crc>>1;
crc^=0xa001;
}
}
len-=1;
ptr++;
}
return crc;
}
//应用关键技巧举例:
unsigned char rx_buffer[8]; //定义接收数据缓冲区
unsigned int intTemp; //定义临时Word变量
intTemp=cal_crc(&rx_buffer[0],6); //求取CRC16结果
rx_buffer[6]=WORD_LO(intTemp); //获得CRC16的低位字节
rx_buffer[7]=WORD_HI(intTemp); //获得CRC16的高位字节
CRC16串口工具ourdev_712704HXDGP3.rar(文件大小:190K) (原文件名:Commix.rar)
|