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.
CRC.cpp
00001 #include "CRC.h" 00002 00003 const unsigned short CRCPOLYNOM = 0x8005; 00004 00005 // The last two bytes of data corresponds to the remainder. If they are of 0,0, this function will return the remainder to put into them. 00006 // If the two last bytes contains the crc received, the return should be of 0 00007 int crc16Remainder(std::vector<unsigned char> data, unsigned short generator) 00008 { 00009 // shift generator 00010 unsigned int polynom = generator << 16; 00011 00012 if (data.empty()) 00013 { 00014 return -1; 00015 } 00016 00017 unsigned int remainder = data[0] << 24 | data[1] << 16; 00018 00019 for (int i = 2; i < data.size(); i++) 00020 { 00021 remainder = remainder | data[i] << 8; 00022 for (int j = 0; j < 8; j++) 00023 { 00024 // if msb is 1, shift and xor 00025 if (remainder >> 31 ==1) 00026 { 00027 remainder = (remainder << 1) ^ polynom; 00028 } 00029 else 00030 { 00031 remainder = remainder << 1; 00032 } 00033 } 00034 } 00035 // Calculates remainder of last 16 bits 00036 for (int i = 0; i < 16; i++) 00037 { 00038 // if msb is 1, shift and xor 00039 if (remainder >> 31 == 1) 00040 { 00041 remainder = (remainder << 1) ^ polynom; 00042 } 00043 else 00044 { 00045 remainder = remainder << 1; 00046 } 00047 } 00048 return remainder >> 16; 00049 00050 }
Generated on Tue Jul 12 2022 18:11:54 by
1.7.2