UH Robotics / PololuQik2

Dependents:   theRobot3

Fork of PololuQik2 by Thomas Ashworth

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CRC7.cpp Source File

CRC7.cpp

Go to the documentation of this file.
00001 /**
00002  *    @file        CRC7.cpp
00003  *    @author        Edward Wilson (edwilson1989@gmail.com)
00004  *  @date         Dec 16, 2010
00005  *  @brief        The CRC7 checksum code.
00006  *
00007  *
00008  */
00009 
00010 #include "CRC7.h"
00011 
00012 CRC7::CRC7()
00013 {
00014     GenerateCRCTable();
00015 
00016 }
00017 
00018 unsigned char CRC7::GetCRC(unsigned char val)
00019 {
00020     int j;
00021     for (j = 0; j < 8; j++) {
00022         if (val & 1)
00023             val ^= CRC7_POLY;
00024         val >>= 1;
00025     }
00026     return val;
00027 }
00028 
00029 unsigned char CRC7::CRC(unsigned char message[], unsigned int length)
00030 {
00031     unsigned char i, crc = 0;
00032 
00033     for (i = 0; i < length; i++)
00034         crc = CRCTable[crc ^ message[i]];
00035     return crc;
00036 }
00037 
00038 void CRC7::GenerateCRCTable()
00039 {
00040     int i;
00041     // generate a table value for all 256 possible byte values
00042     for (i = 0; i < 256; i++) {
00043         CRCTable[i] = GetCRC(i);
00044     }
00045 }
00046