Re: How CRC for CAN bus is calculated
Stephen Glow <[email protected]>
| Newsgroups | gmane.comp.hardware.bus.can |
|---|---|
| Message-ID | <[email protected]> |
Here's some example C code that calculates the CRC for a CAN message.
Rgds,
Steve
static uint16_t CRC_AddBits( uint16_t crc, uint16_t value, int bct )
{
uint16_t mask = 1<<(bct-1);
while( mask )
{
int bit = (value&mask) ? 1 : 0;
if( crc & 0x4000 )
bit = !bit;
crc <<= 1;
if( bit ) crc ^= 0x4599;
mask>>=1;
}
return crc & 0x7FFF;
}
static uint16_t CalcCRC( int id, int ct, uint8_t data[] )
{
uint16_t crc = 0;
int i;
// Add start of frame and ID
crc = CRC_AddBits( crc, id&0x7FF, 12 );
// Add RTR, IDE, r1 and count
crc = CRC_AddBits( crc, ct&0x0F, 7 );
if( ct > 8 ) ct = 8;
for( i=0; i<ct; i++ )
crc = CRC_AddBits( crc, data[i], 8 );
return crc;
}
On 01/08/2014 08:03 AM, yogesh zambare wrote:
> Dear All,
> Can anybody tell me how CRC for CAN bus is calculated?
>
> Regards,
> Yogesh Zambare
> --
> Archives and useful links: http://groups.yahoo.com/group/CANbus
> Subscribe and unsubscribe at www.vector.com/canlist/
> Report any problems to <[email protected]>
--
Archives and useful links: http://groups.yahoo.com/group/CANbus
Subscribe and unsubscribe at www.vector.com/canlist/
Report any problems to <[email protected]>