/*
**---------------------------------------------------------------------------
**
** Abstract: Calculate j1850 CRC
**
** Parameters: Pointer to frame buffer, frame length
**
** Returns: CRC of frame
**
**---------------------------------------------------------------------------
*/
// calculate J1850 message CRC
u8 j1850_crc(u8 *msg_buf, u16 nbytes)
{
u8 crc_reg=0xff,poly,bit_count;
u16 byte_count;
u8 *byte_point;
u8 bit_point;
for (byte_count=0, byte_point=msg_buf; byte_count {
for (bit_count=0, bit_point=0x80 ; bit_count<8; ++bit_count, bit_point>>=1)
{
if (bit_point & *byte_point) // case for new bit = 1
{
if (crc_reg & 0x80)
poly=1; // define the polynomial
else
poly=0x1c;
crc_reg= ( (crc_reg << 1) | 1) ^ poly;
}
else // case for new bit = 0
{
poly=0;
if (crc_reg & 0x80)
poly=0x1d;
crc_reg= (crc_reg << 1) ^ poly;
}
}
}
return ~crc_reg; // Return CRC
}
阅读(3652) | 评论(0) | 转发(1) |