F. crc-generation algorithm in c – Pololu TReX User Manual

Page 20

Advertising
background image

Steps 3, 4, & 5:

_______________________________________________

1 0 0 0 1 0 0 1 ) 1 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0

XOR 1 0 0 0 1 0 0 1 | | | | | | | | | | | | | | |

--------------- | | | | | | | | | | | | | | |

0 0 0 1 1 1 1 0 0 0 1 | | | | | | | | | | |

shift ----> 1 0 0 0 1 0 0 1 | | | | | | | | | | |

_______________ | | | | | | | | | | |

1 1 1 1 0 0 0 1 | | | | | | | | | |

1 0 0 0 1 0 0 1 | | | | | | | | | |

_______________ | | | | | | | | | |

1 1 1 1 0 0 0 1 | | | | | | | | |

1 0 0 0 1 0 0 1 | | | | | | | | |

_______________ | | | | | | | | |

1 1 1 1 0 0 0 1 | | | | | | | |

1 0 0 0 1 0 0 1 | | | | | | | |

_______________ | | | | | | | |

1 1 1 1 0 0 0 1 | | | | | | |

1 0 0 0 1 0 0 1 | | | | | | |

_______________ | | | | | | |

1 1 1 1 0 0 0 0 | | | | | |

1 0 0 0 1 0 0 1 | | | | | |

_______________ | | | | | |

1 1 1 1 0 0 1 0 | | | | |

1 0 0 0 1 0 0 1 | | | | |

_______________ | | | | |

1 1 1 1 0 1 1 0 | | | |

1 0 0 0 1 0 0 1 | | | |

_______________ | | | |

1 1 1 1 1 1 1 0 | | |

1 0 0 0 1 0 0 1 | | |

_______________ | | |

1 1 1 0 1 1 1 0 | |

1 0 0 0 1 0 0 1 | |

_______________ | |

1 1 0 0 1 1 1 0 |

1 0 0 0 1 0 0 1 |

_______________ |

1 0 0 0 1 1 1 0

1 0 0 0 1 0 0 1

_______________

0 0 0 0 1 1 1 = 0x07

So the full command packet we would send to retrieve the raw channel inputs for all five channels with CRC enabled
is: 0x86, 0x1F, 0x07

There are some tricks you can use in your programs to make the CRC calculation much more efficient from a speed
perspective. You can find an example of this

Section 5.f

.

5.f. CRC-Generation Algorithm in C

The CRC algorithm is typically defined as a bit-wise operation, however it is possible to rewrite the algorithm to
work at the byte level if we do some calculations ahead of time. We can store CRC computations for all 256 possible
byte values when our program first runs, and then retrieve those values as needed when generating a CRC for a given
message. This requires we set aside 256 bytes of RAM/EEPROM/flash, but it makes the CRC calculation for an
arbitrary message much faster than if we work through the message bit by bit.

The following sample code stores a CRC lookup table in RAM using the function GenerateCRCTable(), which
only needs to be called once (probably somewhere during the initialization phase of your program). You could easily
change this function to store the lookup table to EEPROM or flash. To process a message, initialize your CRC variable
to zero and use the CRCAdd() function to “add” each message byte to your CRC, as is done in the getCRC()
function.

unsigned char CRCPoly = 0x89; // the value of our CRC-7 polynomial

unsigned char CRCTable[256];

void GenerateCRCTable()

Pololu TReX User's Guide

© 2001–2013 Pololu Corporation

5. The Serial Interface

Page 20 of 24

Advertising