typedef
unsigned char
uchar;
typedef
unsigned int
uint;
code uchar crcbuff [] = { 0x00,0x00,0x00,0x00,0x06,0x0d,0xd2,0xe3}; //待发送数据
uint crc;
// CRC 码
void main(void)
{
uchar *ptr;
crc = 0;
// CRC
初值
ptr = crcbuff;
//
指向待发送数据
crc = crc16l(ptr,8);
while(1);
} uint crc16l(uchar *ptr,uchar len)
// ptr 为数据指针,len 为数据长度
{
uchar i;
while(len--)
{
for(i=0x80; i!=0; i>>=1)
{
if((crc&0x8000)!=0)
{
crc<<=1;
crc^=0x1021;
}
//1-1
else
{
crc<<=1;
//1-2
}
if((*ptr&i)!=0)
{
crc^=0x1021;
//1-3
}
}
ptr++;
}
return(crc);
}
还是用除法来做呢? |