
//
//
// CRC table for implementing CRC16 with 0x1021 polynomial
//
//

__flash const unsigned int crc_table[16] =
{
     0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 
     0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 
}; 

//
//
// routine to calculate the CRC of a current value with a 
// new byte value. The new CRC result is returned.
//
//

unsigned int calc_crc(unsigned int crc, unsigned char data)
{
    crc = crc_table[((crc >> 12) ^ (data >> 4)) & 0x0F] ^ (crc << 4);
    crc = crc_table[((crc >> 12) ^ (data & 0x0F)) & 0x0F] ^ (crc << 4);
    return(crc);
}
