//计算一组数据的16位CRC
unsigned char crc_buff[]=
{
0x01,0X02,0X03,0X04,0X05
};
//======================================
unsigned int crc_cal(unsigned char *ptr,unsigned char len);
void main(void)
{
unsigned char *ptr;
crc=0; //crc初值
ptr=crc_buff; //指向第一个数据
crc=crc_cal(ptr,5);
while(1);
}
unsigned int crc_cal(unsigned char *ptr,unsigned char len) //prt为数据指针,len为数据长度
{
unsigned char i;
while(len--)
{
for(i=0x80;i!=0;i>>=1)
{
if((crc&0x8000)!=0)
{
crc<<=1;
crc^=0x1021;
}
else
crc<<=1;
if((*ptr&i)!=0)
crc^=0x1021;
}
ptr++;
}
return(crc);
} |