Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of workinQM_5thJan_azad by
Diff: crc.h
- Revision:
- 19:79e69017c855
- Parent:
- 3:07e15677a75c
- Child:
- 20:949d13045431
--- a/crc.h Sat May 14 11:19:13 2016 +0000 +++ b/crc.h Sat Jun 04 11:29:13 2016 +0000 @@ -7,73 +7,83 @@ #define POLYNOMIAL16 0x1021 #define POLYNOMIAL8 0xEA -namespace CRC{ +namespace CRC +{ typedef uint16_t crctype16; - crctype16 crc16_gen(const unsigned char message[], unsigned int nBytes){ - crctype16 remainder = 0xffff; - int byte; - char bit; + crctype16 crc16_gen(const unsigned char message[], unsigned int nBytes) + { + crctype16 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] << 8 ); + 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] << 8 ); - for( bit = 8 ; bit > 0 ; bit--){ - /* - for each bit, xor the remainder with polynomial - if the MSB is 1 - */ - if(remainder & TOPBIT16){ - remainder = (remainder << 1) ^ POLYNOMIAL16; - /* - 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 - */ + for( bit = 8 ; bit > 0 ; bit--) + { + /* + for each bit, xor the remainder with polynomial + if the MSB is 1 + */ + if(remainder & TOPBIT16) + { + remainder = (remainder << 1) ^ POLYNOMIAL16; + /* + 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); + } + } } - else{ - remainder = (remainder << 1); - } - } + + return remainder; } - - return remainder; - } typedef uint8_t crctype8; - crctype8 crc8_gen(const unsigned char message[], unsigned int nBytes){ - - crctype8 remainder = 0xff; + crctype8 crc8_gen(const unsigned char message[], unsigned int nBytes) + { + crctype8 remainder = 0xff; - for(int 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] ); + for(int 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] ); - for(int bit = 8 ; bit > 0 ; bit--){ - /* - for each bit, xor the remainder with polynomial - if the MSB is 1 - */ - if(remainder & TOPBIT8){ - remainder = (remainder << 1) ^ POLYNOMIAL8; - /* - 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 - */ + for(int bit = 8 ; bit > 0 ; bit--) + { + /* + for each bit, xor the remainder with polynomial + if the MSB is 1 + */ + if(remainder & TOPBIT8) + { + remainder = (remainder << 1) ^ POLYNOMIAL8; + /* + 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); + } + } } - else{ - remainder = (remainder << 1); - } - } + + return remainder; } - - return remainder; - } } \ No newline at end of file