User's Manual

28
CRC8 C CODE WORKED EXAMPLE
CRC8 C CODE
CRC8 checksums are used on the standard radar messages. The checksum calculation is performed on all
bytes, up to and including the asterisk character. These checksums are calculated using the following C code.
//Lookup table for CRC8 calculation
//Needs to be initialised with InitCRC8
U8 crc8_table[256];
/********************************MemCRC8*******************************
This function calculates the CRC8 of a data array pointed to by data
and of length length.
Usespolynomialx^8+x^2+x+1.Lookuptableusedbyfunctionisinitialised
by the InitCRC8 function.
*/
unsignedcharMemCRC8(void*data,unsignedintlength)
{
unsigned char crc8;
unsigned char i;
unsigned char *dptr;
dptr=(unsignedchar*)data;
crc8 = 0; //Start with a value of 0
for(i=0;i<length;i++)
{
crc8 = crc8_table[crc8 ^ *dptr];
++dptr;
}
return crc8;
}
#deneGP0x107/*x^8+x^2+x+1*/
#define DI 0x07
/****************************InitCRC8******************************
Initialises the lookup table for the MemCRC8 function
Usespolynomialx^8+x^2+x+1
*/
voidInitCRC8(void)
{
int i,j;
unsigned char crc;
 for(i=0;i<256;i++)
{
crc = i;
 for(j=0;j<8;j++)
{
crc=(crc<<1)^((crc&0x80)?DI:0);
}
crc8_table[i] = crc & 0xFF;
}
}