TELECOMMAND MANAGER V1
Dependencies: mbed SLCD mbed-rtos
Diff: crc.h
- Revision:
- 0:b5b370873460
- Child:
- 1:df31097c8442
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crc.h Wed Apr 15 10:43:22 2015 +0000 @@ -0,0 +1,43 @@ +#define WIDTH 16 +#define TOPBIT (1 << 15) +#define POLYNOMIAL 0x1021 + +class CRC{ +private: + typedef unsigned short int crctype; +public: + // no need of intializer + crctype crcGenerate(const char message[], int nBytes){ + crctype remainder = 0xffff; + int byte; + char bit; + + for( byte = 0 ; byte < nBytes ; byte++ ){ + /* + Bring the data byte by byte + each time only one byte is brought + 0 xor x = x + */ + remainder = remainder ^ ( message[byte] << (WIDTH-8) ); + + for( bit = 8 ; bit > 0 ; bit--){ + /* + for each bit, xor the remainder with polynomial + if the MSB is 1 + */ + if(remainder & TOPBIT){ + remainder = (remainder << 1) ^ POLYNOMIAL; + /* + each time the remainder is xor-ed with polynomial, the MSB is made zero + hence the first digit of the remainder is ignored in the loop + */ + } + else{ + remainder = (remainder << 1); + } + } + } + + return remainder; + } +}; \ No newline at end of file