A. crc computation in c – Pololu Qik 2s12v10 User Manual
Page 30

Steps 3, 4, & 5:
_______________________________________________
1 0 0 0 1 0 0 1 ) 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
XOR 1 0 0 0 1 0 0 1 | | | | | | | | | | | | | | |
--------------- | | | | | | | | | | | | | | |
1 0 0 1 0 0 0 1 | | | | | | | | | | | | | |
shift ----> 1 0 0 0 1 0 0 1 | | | | | | | | | | | | | |
_______________ | | | | | | | | | | | | | |
1 1 0 0 0 0 0 0 | | | | | | | | | | |
1 0 0 0 1 0 0 1 | | | | | | | | | | |
_______________ | | | | | | | | | | |
1 0 0 1 0 0 1 0 | | | | | | | | | |
1 0 0 0 1 0 0 1 | | | | | | | | | |
_______________ | | | | | | | | | |
1 1 0 1 1 0 0 0 | | | | | | |
1 0 0 0 1 0 0 1 | | | | | | |
_______________ | | | | | | |
1 0 1 0 0 0 1 0 | | | | | |
1 0 0 0 1 0 0 1 | | | | | |
_______________ | | | | | |
1 0 1 0 1 1 0 0 | | | |
1 0 0 0 1 0 0 1 | | | |
_______________ | | | |
1 0 0 1 0 1 0 0 | |
1 0 0 0 1 0 0 1 | |
_______________ | |
1 1 1 0 1 0 0 = 0x17
So the full command packet we would send to retrieve the PWM configuration parameter with CRC enabled is:
0x83, 0x01, 0x17
There are some tricks you can use in your programs to make the CRC calculation much faster. You can find an
example of this
6.a. CRC Computation in C
The following example program shows how to compute a CRC in the C programming language. The idea is that
the CRC of every possible byte is stored in a lookup table, so that it can be quickly loaded when computing the
CRC of a longer message. The individual CRC bytes are XORed together with the C operator ^ to get the final
CRC of the message. In the example main() routine, this is applied to generate the CRC byte in the message 0x83,
0x01, that was used in
#include
const unsigned char CRC7_POLY = 0x91;
unsigned char CRCTable[256];
unsigned char GetCRC(unsigned char val)
{
unsigned char j;
for (j = 0; j < 8; j++)
{
if (val & 1)
val ^= CRC7_POLY;
val >>= 1;
}
return val;
}
void GenerateCRCTable()
{
int i, j;
// generate a table value for all 256 possible byte values
for (i = 0; i < 256; i++)
{
CRCTable[i] = GetCRC(i);
Qik 2s12v10 User's Guide
© 2001–2012 Pololu Corporation
6. Cyclic Redundancy Check (CRC) Error Detection
Page 30 of 33