C language: CRC check

Publisher:qiuxubiaoLatest update time:2015-01-19 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. CRC code
CRC: Cylic Reduancy check is translated into Chinese as cyclic redundancy check code.
 
2. XOR
XOR: Logical operator exclusive OR. I don’t know how to write it with the symbol, but the operation rule is that different is 1 and the same is 0.
 
3. Two examples of using XOR to replace division in arithmetic operations.
1. 10110010000/11001
The first XOR (division) gives a quotient of 1 and a remainder of 1111. Add the next 0 and proceed
The second XOR operation gives a quotient of 1 and a remainder of 111. Add the next digit 1 and the remainder is 1111. The four digits and the divisor 5 digits cannot be XORed, so the quotient is 0. Add the next digit 0 and continue.
The third XOR operation yields a quotient of 1 and a remainder of 111. Similarly, the quotient of the fifth digit is 0, and the remainder continues to be added to the next 0 of the dividend.
The fourth XOR operation yields a quotient of 1 and a remainder of 101. Adding the last digit of the dividend, 0, yields a quotient of 0, a remainder of 1010, and a final quotient of 1101010. The calculation process is shown in the figure below:

If we add the dividend 10110010000 to the remainder 1010, the result is 10110011010, and then divide it by (XOR) 11001, the remainder is 0000 (no specific calculation is done here).
2. 1111000/1001
After three XOR (division), the quotient is 1110 and the remainder is 110. The specific routine is as follows:

Similarly, let the dividend 1111000 in this example plus the remainder 110 be 1111110, and divide it by (XOR) the divisor 100 to get a remainder of 000.
 
4. CRC Verification Principle
From the above two examples, we can see that during the communication process, the data to be transmitted is added as the "dividend", and then the remainder is added before transmission. After the receiving party receives the complete data, it is divided by the divisor. If the remainder is 0, it means that the transmitted data is correct. If it is not 0, it means that the transmitted data is incorrect. Because for a certain "divisor", there will be a unique remainder corresponding to it. This process is actually a CRC verification process. However, the name can be changed and it cannot be called dividend or divisor. It can be stipulated that the above divisor is called a generating polynomial or generating term, represented by g(x). The remainder is called a CRC check code. From the above, we know that for different generating terms, there will be a unique CRC check code corresponding to it. For the data to be transmitted, a polynomial with coefficients of only 0 and 1 can also be used to correspond one by one. For example, the polynomial corresponding to the code 1010111 is x^6+x^4+x^2+x+1. And the code corresponding to the polynomial x^5+x^3+x^2+x+1 is 101111.
In fact, the dividend is not the real data to be transmitted. The real data to be transmitted is a code after the polynomial is shifted left by the number of CRC check code bits. The number of bits of the CRC check code is defined to represent its width.
In addition, regarding the generated item, it is not difficult to find that its highest bit is 1. In fact, it will be eliminated every time XOR is performed in division, so the first 1 is generally not used as a reference, and the following bits are the most important. This is why the number of bits in the generated item is 1 greater than the CRC width. And the last bit of the generated item must also be 1 at the same time. Generally, the highest bit 1 is not written when the generated item is abbreviated. The following are various commonly used generated item expressions:
CRC-4: x^4+x+1;-------------------------------->0x03
CRC-8:x^8+x^5+x^4+1;-------------------------->0x31
CRC-8:x^8+x^2+x^1+1;-------------------------->0x07
CRC-8:x^8+x^6+x^4+x^3+x^2+x;------------------->0x5E
CRC-12:x^12+x^11+x^4+x^3+x+1;------------------>0x80
CRC-16:x^16+x^15+x^2+1;------------------------>0x80005
CRC-16:x^16+x^12 +x^5+1;------------------------>0x1021
CRC-32:x^32+x^26 +x^23+.... +x^2+x+1;------------>0x04C11DB7
CRC-32:x^32+x^28 +x^27+.... +x^8+x^6+1;---------->0x1EDC6F41
 
5. Implementation of C language
 
 
 
 
 
appendix:
Reference address:C language: CRC check

Previous article:Usage of const in C language
Next article:Programming of wireless RF chip CC1100e

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号