1. Introduction to CRC Check
Cyclic Redundancy Check (CRC) is a commonly used checksum with error detection and correction capabilities. It is widely used in early communications. Cyclic Redundancy Check is often used for data verification of external storage and computer synchronous communication. Cyclic Redundancy Check establishes the agreed relationship between data bits and check bits through some mathematical operations.
Different from parity check, sum check, XOR check and other verification methods, the calculation process of CRC check is relatively complicated.
2. Introduction to Modulo 2 Division
Although the CRC check principle seems complicated, it is not difficult to understand. The basic idea is to first append a number to the frame to be sent, generate a new frame and send it to the receiving end. Of course, this additional number is not random. It must make the generated new frame divisible by a specific number selected by the sending and receiving ends. Here, binary division is not directly used, but a method called "modulo 2 division" is used. After reaching the receiving end, the received new frame is divided by the selected divisor (also using "modulo 2 division"). Because the sender has added a number before sending the data frame, the "remainder removal" process has been performed, so the result should have no remainder and the result can be divided by the divisor. If there is a remainder, it means that an error has occurred in the transmission process of the frame.
Here, in order to have a deeper understanding of CRC verification, let's introduce modulo 2 division. It is similar to "arithmetic division", but it does not borrow from the upper bit, nor does it compare the values of the same bits of the divisor and dividend. As long as the same number of bits is used for division, it will be fine. Modulo 2 subtraction is actually a bitwise XOR operation. That is, after comparison, if the corresponding bits of the two are the same, the result is "0", and if they are different, the result is "1". As shown in the figure below, the binary number 1010101 is divided by 1001, and the result is 1011, and the remainder is 110.
3. CRC verification principle
Above we have briefly introduced CRC verification. Now let’s take a look at how CRC verification is implemented.
First, in order to perform CRC check, the sender and receiver must agree on a divisor, which is usually expressed in a polynomial form, and this polynomial is also called a "generator polynomial". For example, the commonly used polynomial x4 + x + 1 represents a divisor of 10011, and the polynomial x16 + x15 + x2 + 1 represents a divisor of 110000000000000101. The following table lists the commonly used divisor polynomial formulas.
CRC algorithm name Polynomial formula Width Polynomial Initial value Result XOR value Input inversion Output inversion
CRC-4/ITU x4 + x + 1 4 03 00 00 true true
CRC-5/EPC x5 + x3 + 1 5 09 09 00 false false
CRC-5/ITU x5 + x4 + x2 + 1 5 15 00 00 true true
CRC-5/USB x5 + x2 + 1 5 05 1F 1F true true
CRC-6/ITU x6 + x + 1 6 03 00 00 true true
CRC-7/MMC x7 + x3 + 1 7 09 00 00 false false
CRC-8 x8 + x2 + x + 1 8 07 00 00 false false
CRC-8/ITU x8 + x2 + x + 1 8 07 00 55 false false
CRC-8/ROHC x8 + x2 + x + 1 8 07 FF 00 true true
CRC-8/MAXIM x8 + x5 + x4 + 1 8 31 00 00 true true
CRC-16/IBM x16 + x15 + x2 + 1 16 8005 0000 0000 true true
CRC-16/MAXIM x16 + x15 + x2 + 1 16 8005 0000 FFFF true true
CRC-16/USB x16 + x15 + x2 + 1 16 8005 FFFF FFFF true true
CRC-16/MODBUS x16 + x15 + x2 + 1 16 8005 FFFF 0000 true true
CRC-16/CCITT x16 + x12 + x5 + 1 16 1021 0000 0000 true true
CRC-16/CCITT-FALSE x16 + x12 + x5 + 1 16 1021 FFFF 0000 false false
CRC-16/X25 x16 + x12 + x5 + 1 16 1021 FFFF FFFF true true
CRC-16/XMODEM x16 + x12 + x5 + 1 16 1021 0000 0000 false false
CRC-16/DNP x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1 16 3D65 0000 FFFF true true
CRC-32 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1 32 04C11DB7 FFFFFFFF FFFFFFFF true true
CRC-32/MPEG-2 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1 32 04C11DB7 FFFFFFFF 00000000 false false
After selecting the divisor, you need to add k-1 "0" bits after the data to be sent, where k is the number of digits of the divisor. Then divide the new number with k-1 "0" bits added (a total of m+k-1 bits) by the divisor in the "modulo 2 division" method, and the remainder is the CRC check code of the data. However, it should be noted that the number of digits of the remainder must be one less than the number of digits of the divisor, even if the first digit is 0, or even all 0 (when it is divisible), it cannot be omitted.
Finally, the check code is appended to the original data to construct a new frame and send it to the receiving end. Finally, the receiving end divides the new frame by the previously selected divisor using "modulo 2 division". If there is no remainder, it indicates that there was no error in the transmission of the frame, otherwise an error occurred.
Next, we use an example to illustrate the calculation process of CRC check.
Suppose we calculate the CRC checksum of the hexadecimal number 0xBB, and the generator polynomial we use is x4 + x + 1. The number to be calculated is represented by the binary bit 1011 1011, and the divisor is 10011. Since the number of bits of the generator polynomial is 5, according to the above introduction, the number of bits of the CRC checksum is 4 (the number of bits of the checksum is 1 less than the number of bits of the generator polynomial). Therefore, add 4 more zeros after the original data to get 1011 1011 0000, and then divide this number by the generator polynomial in the "modulo 2 division" method, and the remainder (i.e. CRC code) is 1111, as shown in the figure below.
Replace the four "0"s after the original frame 1011 1011 0000 with the CRC check 1111 calculated in the previous step to get the new frame 1011 1011 1111. Then send this new frame to the receiving end. When the above new frame arrives at the receiving end, the receiving end will divide the new frame with the divisor 10011 selected above in the "modulo 2 division" mode to verify whether the remainder is 0. If it is 0, it proves that there is no error in the transmission process of the frame data, otherwise there is an error.
4. CRC check c code implementation
The calculation process of CRC check is relatively complicated, and the general code implementation adopts the table lookup method. The following code is the code for CRC16 check implementation, the corresponding CRC algorithm is CRC-16/MODBUS, and the generated polynomial is x16 + x15 + x2 + 1.
#include const unsigned char chCRCHTalbe[] = //CRC high byte value table { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 }; const unsigned char chCRCLTalbe[] = //CRC low byte value table { 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0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0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 }; unsigned short CRC16(unsigned char* pchMsg, unsigned short wDataLen)
Previous article:A simple microcontroller serial port assistant program implemented in Python
Next article:MC9S12X Series Dual-core MCU Coprocessor (Xgate) Study Notes
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- NMOS tube on-state voltage test problem
- EEWORLD University - How to use independent measurement ADC to design high-precision CT phase meter
- A collection of selected PCB design resources, free points download for a limited time
- MCU model confirmation and decryption
- [Zero-knowledge ESP8266 tutorial] Quick start 25 Blynk control RGB LED
- Need a 10uh inductor, ESR < 0.2Ω, saturation current > 0.8A
- Ti DSP optimization basic strategy
- Bluetooth Protocol Stack-01
- Thanks to shihuntaotie netizen for donating a large number of development boards!
- Scattered Noise Figure