I am trying to do crc_table for 12-bit CRC and the algorithm, but always I got wrong results.
Can you help me? To create crc table I try:
void crcInit(void)
{
unsigned short remainder;
int dividend;
unsigned char bit;
for (dividend = 0; dividend < 256; ++dividend)
{
remainder = dividend << 4;
for (bit = 8; bit > 0; --bit)
{
if (remainder & 0x800)
{
remainder = (remainder << 1) ^ 0x180D; //Polynomio of CRC-12
}
else
{
remainder = (remainder << 1);
}
}
crcTable[dividend] = remainder;
}
}
I updated that and CRC algorithm is:
unsigned short crcFast(unsigned char const message[], int nBytes)
{
unsigned short remainder = 0x0000;
unsigned char data;
int byte;
/*
* Divide the message by the polynomial, a byte at a time.
*/
for (byte = 0; byte < nBytes; ++byte)
{
data = message[byte] ^ (remainder >> 4);
remainder = crcTable[data] ^ (remainder << 8);
}
/*
* The final remainder is the CRC.
*/
return (remainder ^ 0);
}
But It isn’t working…..
This doesn’t seem right:
It looks like you are intending this number to be binary, but it is actually decimal. You should use a hex literal instead (0x80).
There also seem to be problems with this number, and with the size of the shift you do: This test should check if the high-order bit of the remainder is set. Since you are doing a 12-bit CRC, the mask should be 0x800 (binary 100000000000). And the shift above that should probably be:
to set the leftmost 8 bits of the remainder.