|
发表于 2023-6-30 17:18:17
|
显示全部楼层
u8 xdata TP_Data_Return[100]={0x5A,0xA5,0x06,0x83,0x25,0x00,0x01,0x00,0x00};
/*CRC校验*/
u16 crc16(u8 *dat, u16 length)
{
u8 i;
u16 crc = 0xffff; // Initial value
while(length--)
{
crc ^= *dat++; // crc ^= *dat; dat++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA001; // 0xA001 = reverse 0x8005
else
crc = (crc >> 1);
}
}
return crc;
}
crc16(&TP_Data_Return[3],TP_Data_Return[2]-2 );
TP_Data_Return[TP_Data_Return[2]+2] = (u8)(CRC_dat>>8) ;
TP_Data_Return[TP_Data_Return[2]+1] = (u8)(CRC_dat) ;
不查表实时计算的试一下,以这个为例 |
|