kyle johnson
/
PololuQik2
Example
Fork of PololuQik2 by
CRC7.cpp@1:1ac7b13ff4c3, 2014-09-15 (annotated)
- Committer:
- johnson6987
- Date:
- Mon Sep 15 18:25:20 2014 +0000
- Revision:
- 1:1ac7b13ff4c3
- Parent:
- 0:511d65ef1276
PololuQik2 Example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
srsmitherman | 0:511d65ef1276 | 1 | /** |
srsmitherman | 0:511d65ef1276 | 2 | * @file CRC7.cpp |
srsmitherman | 0:511d65ef1276 | 3 | * @author Edward Wilson (edwilson1989@gmail.com) |
srsmitherman | 0:511d65ef1276 | 4 | * @date Dec 16, 2010 |
srsmitherman | 0:511d65ef1276 | 5 | * @brief The CRC7 checksum code. |
srsmitherman | 0:511d65ef1276 | 6 | * |
srsmitherman | 0:511d65ef1276 | 7 | * |
srsmitherman | 0:511d65ef1276 | 8 | */ |
srsmitherman | 0:511d65ef1276 | 9 | |
srsmitherman | 0:511d65ef1276 | 10 | #include "CRC7.h" |
srsmitherman | 0:511d65ef1276 | 11 | |
srsmitherman | 0:511d65ef1276 | 12 | CRC7::CRC7() |
srsmitherman | 0:511d65ef1276 | 13 | { |
srsmitherman | 0:511d65ef1276 | 14 | GenerateCRCTable(); |
srsmitherman | 0:511d65ef1276 | 15 | |
srsmitherman | 0:511d65ef1276 | 16 | } |
srsmitherman | 0:511d65ef1276 | 17 | |
srsmitherman | 0:511d65ef1276 | 18 | unsigned char CRC7::GetCRC(unsigned char val) |
srsmitherman | 0:511d65ef1276 | 19 | { |
srsmitherman | 0:511d65ef1276 | 20 | int j; |
srsmitherman | 0:511d65ef1276 | 21 | for (j = 0; j < 8; j++) { |
srsmitherman | 0:511d65ef1276 | 22 | if (val & 1) |
srsmitherman | 0:511d65ef1276 | 23 | val ^= CRC7_POLY; |
srsmitherman | 0:511d65ef1276 | 24 | val >>= 1; |
srsmitherman | 0:511d65ef1276 | 25 | } |
srsmitherman | 0:511d65ef1276 | 26 | return val; |
srsmitherman | 0:511d65ef1276 | 27 | } |
srsmitherman | 0:511d65ef1276 | 28 | |
srsmitherman | 0:511d65ef1276 | 29 | unsigned char CRC7::CRC(unsigned char message[], unsigned int length) |
srsmitherman | 0:511d65ef1276 | 30 | { |
srsmitherman | 0:511d65ef1276 | 31 | unsigned char i, crc = 0; |
srsmitherman | 0:511d65ef1276 | 32 | |
srsmitherman | 0:511d65ef1276 | 33 | for (i = 0; i < length; i++) |
srsmitherman | 0:511d65ef1276 | 34 | crc = CRCTable[crc ^ message[i]]; |
srsmitherman | 0:511d65ef1276 | 35 | return crc; |
srsmitherman | 0:511d65ef1276 | 36 | } |
srsmitherman | 0:511d65ef1276 | 37 | |
srsmitherman | 0:511d65ef1276 | 38 | void CRC7::GenerateCRCTable() |
srsmitherman | 0:511d65ef1276 | 39 | { |
srsmitherman | 0:511d65ef1276 | 40 | int i; |
srsmitherman | 0:511d65ef1276 | 41 | // generate a table value for all 256 possible byte values |
srsmitherman | 0:511d65ef1276 | 42 | for (i = 0; i < 256; i++) { |
srsmitherman | 0:511d65ef1276 | 43 | CRCTable[i] = GetCRC(i); |
srsmitherman | 0:511d65ef1276 | 44 | } |
srsmitherman | 0:511d65ef1276 | 45 | } |
srsmitherman | 0:511d65ef1276 | 46 |