Implementation of CRC16 using polynomial 0x8005 = X^16 + X^15 + X^2 + 1
Dependents: Manchester_Transmitter Manchester_Receiver ManchesterUART_Transmitter ManchesterUART_Receiver
Fork of CRC16 by
Revision 3:3f5622979e77, committed 2017-09-03
- Comitter:
- hudakz
- Date:
- Sun Sep 03 08:28:01 2017 +0000
- Parent:
- 2:a01521fb2fe1
- Commit message:
- Function parameter added for setting initial CRC value. Defaults to 0x0000.
Changed in this revision
CRC16.cpp | Show annotated file Show diff for this revision Revisions of this file |
CRC16.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/CRC16.cpp Mon May 22 09:32:41 2017 +0000 +++ b/CRC16.cpp Sun Sep 03 08:28:01 2017 +0000 @@ -1,6 +1,8 @@ /* * This is a fork of the CRC16 library COPYRIGHT(c) Emilie Laverge * published at [https://developer.mbed.org/users/EmLa/code/CRC16/] + * using the polynomial 0x8005: X^16 + X^15 + X^2 + 1. + * Default initial CRC value = 0xFFFF * * Modified by Zoltan Hudak */ @@ -44,13 +46,14 @@ 0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202 }; -unsigned short CRC16::calc(char input[], int length) { - unsigned short result = 0; +unsigned short CRC16::calc(char input[], int length, unsigned short crc /*=0x0000*/) { for(int i = 0; i < length; i++) { - unsigned short tableValue = TABLE[((result >> 8) ^ *(char*)input++) & SHIFTER]; - result = (result << 8) ^ tableValue; + unsigned short tableValue = TABLE[((crc >> 8) ^ *(char*)input++) & SHIFTER]; + crc = (crc << 8) ^ tableValue; } - return result; + return crc; } + +
--- a/CRC16.h Mon May 22 09:32:41 2017 +0000 +++ b/CRC16.h Sun Sep 03 08:28:01 2017 +0000 @@ -1,6 +1,8 @@ /* * This is a fork of the CRC16 library COPYRIGHT(c) Emilie Laverge * published at [https://developer.mbed.org/users/EmLa/code/CRC16/] + * using the polynomial 0x8005: X^16 + X^15 + X^2 + 1. + * Default initial CRC value = 0x0000 * * Modified by Zoltan Hudak */ @@ -14,8 +16,8 @@ static const unsigned int SHIFTER; static const unsigned short TABLE[]; public: - CRC16(void) { }; - ~CRC16(void){ }; - unsigned short calc(char input[], int length); + CRC16(void){}; + ~CRC16(void){}; + unsigned short calc(char input[], int length, unsigned short crc = 0x0000); }; #endif // CRC16_H