Principle of CRC algorithm and Implementation of C Language

Source: Internet
Author: User

Principle of CRC algorithm and Implementation of C Language

Abstract This paper theoretically exports the implementation principle of CRC algorithm, and provides three C language programs that adapt to different computer or microcontroller hardware environments. Readers can write more practical CRC computing programs in different languages based on the principle of this algorithm.
Key words CRC algorithm C Language
1 Introduction
Cyclic Verification Code CRC testing is widely used in the measurement and control and communication fields. CRC computing can be implemented by dedicated hardware. However, for low-cost microcontroller systems, CRC testing is implemented without hardware support. The key issue is how to complete CRC computing through software, that is, the CRC algorithm.
Three algorithms are provided here, which are slightly different. They are applicable to a Microcontroller System with extremely demanding program space but low CRC computing speed, another method is applicable to computer or microcontroller systems with large program space and high CRC computing speed requirements. The last method is applicable to systems with low program space, the CRC computing speed cannot be too slow for a Microcontroller System.
2 CRC Introduction
The basic idea of CRC verification is to use the linear encoding theory to generate a verification supervised code (both CRC code) at the sender Based on the K-bit binary code sequence to be transmitted with certain rules) r bit, which is attached to the back of the information to form a new binary code sequence number (K + r) bit, and finally sent out. At the receiving end, the system checks the rules followed by the Information Code and CRC code to determine whether an error occurs during transmission.
The 16-bit CRC code is generated by first shifting the number of binary sequences to be sent to the left by 16 bits (multiplied by) and then dividing by a polynomial. The final remainder is both a CRC code, as shown in formula (2-1), B (X) indicates the number of N-bit binary sequences, g (x) is a polynomial, and Q (x) is an integer, R (x) is the remainder (both CRC code ).
(2-1)
The addition and subtraction algorithm of mod 2 is used to calculate the CRC code, which is not to add bitwise addition and subtraction of bitwise AND borrow. This addition and subtraction operation is actually an exclusive or logical operation. addition and subtraction are equivalent, multiplication and division operations are the same as ordinary algebraic multiplication and division operations. The polynomial that generates the CRC code is as follows, where the CRC-16 and the CRC-CCITT generate the 16-bit CRC code, while the CRC-32 generates the 32-bit CRC code. This article does not discuss the 32-bit CRC algorithm. If you are interested, you can deduce the calculation method based on the ideas in this article.
CRC-16: (used in the US binary synchronization system)
CRC-CCITT: (recommended by CCITT in Europe)
CRC-32:

The receiver divides the number of received binary sequences (including the Information Code and CRC Code) by the polynomial. If the remainder is 0, no error occurs during transmission. Otherwise, the transmission is incorrect, I will not describe its principles here. When the CRC code is calculated using software, the receiver can calculate the CRC code for the received information code, and compare the result with whether the received CRC code is the same.

3 calculate CRC by bit
The number of binary sequences can be expressed as (3-1 ):
(3-1)
When calculating the CRC code of the binary sequence number, multiply it by (shifts 16 bits left) and divide it by the polynomial g (x). The remainder is the required CRC code. As shown in Formula 3-2:
(3-2)
It can be set to (3-3)
It is an integer and a 16-bit binary remainder. Replace formula (3-3) with the following formula (3-2:

(3-4)
Reset: (3-5)
It is an integer, which is a 16-bit binary remainder. The formula (3-5) is substituted into the formula (3-4), and the result is as follows:
(3-6)
According to the definition of CRC, it is clear that the 16-bit binary number is the required CRC code.
Formula (3-5) is the key to programming CRC calculation. It indicates that the CRC code after the base is calculated is equal to the previous CRC code multiplied by 2 and divided by the polynomial, the resulting remainder plus the base value divided by the remainder of the polynomial. It is not difficult to understand the C language program for finding the CRC code below. * PTR points to the first byte of the sending buffer. Len indicates the total number of bytes to be sent. 0x1021 is related to polynomial.
Unsigned int cal_crc (unsigned char * PTR, unsigned char Len ){
Unsigned char I;
Unsigned int CRC = 0;
While (Len --! = 0 ){
For (I = 0x80; I! = 0; I/= 2 ){
If (CRC & 0x8000 )! = 0) {CRC * = 2; CRC ^ = 0X1021;}/* multiply the remainder CRC by 2 and then calculate CRC */
Else CRC * = 2;
If (* PTR & I )! = 0) CRC ^ = 0X1021;/* plus the standard CRC */
}
PTR ++;
}
Return (CRC );
}
Although the Code for calculating CRC by bit is simple and the memory occupied is relatively small, the biggest drawback is that one-bit computing occupies a lot of processing time of the processor, this disadvantage is especially unacceptable in high-speed communication scenarios. Therefore, we will introduce a new method for fast CRC Calculation by byte lookup.
4. calculate CRC in bytes
It is easy to understand that the number of binary sequences can be expressed in bytes (4-1), which is a byte (8 bytes in total ).
(4-1)
When calculating the CRC code of the binary sequence number, multiply it by (shifts 16 bits left) and divide it by the polynomial g (x). The remainder is the required CRC code. As shown in formula (4-2:
(4-2)
It can be set to (4-3)
It is an integer and a 16-bit binary remainder. Replace formula (4-3) into formula (4-2:
(4-4)
Because:
(4-5)
It is the high eight bits, which is the low eight bits. Replace formula (4-5) into formula (4-4:
(4-6)
Reset: (4-7)
It is an integer and a 16-bit binary remainder. The formula (4-7) is substituted into the formula (4-6), and so on, finally:
(4-8)
Obviously, the number of 16-bit binary is the required CRC code.
Formula (4-7) is the key to writing a byte CRC program, it indicates that the CRC code after the calculation of this byte is equal to the low 8 bits of the remaining CRC code of the previous byte, add the CRC code obtained after the sum of the last byte CRC to the right shift of 8 bits (also taking 8 bits in height) and the sum of the Current byte. If we calculate all the CRC values of the 8 bits in binary sequence, if a table is placed in a table, the query table method can greatly improve the computing speed. It is not difficult to understand the following C language program to calculate the CRC code by byte. * PTR points to the first byte of the sending buffer. Len indicates the total number of bytes to be sent. The CRC remainder table is obtained based on the 0x11021 polynomial.
Unsigned int cal_crc (unsigned char * PTR, unsigned char Len ){
Unsigned int CRC;
Unsigned char da;
Unsigned int crc_ta [256] = {/* CRC residual table */
0x0000, 0X1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
0 xdbfd, 0 xcbdc, 0 xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c0, 0x1c41,
0 xedae, 0xfd8f, 0 xcdec, 0 xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
0xff9f, 0 xefbe, 0 xdfdd, 0 xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0 xabbb, 0xbb9a,
0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0 xbdaa, 0xad8b, 0x9de8, 0x8dc9,
0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca, 0x2c83, 0x1ce0, 0x0cc0,
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0 xbfba, 0x8fd9, 0x9ff8,
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};

CRC = 0;
While (Len --! = 0 ){
DA = (uchar) (CRC/256);/* store the high 8 bits of CRC in the form of 8 bits */
CRC <= 8;/* shifts 8 bits to the left, which is equivalent to 8 bits of CRC multiplied */
CRC ^ = crc_ta [da ^ * PTR];/* Add the 8-bit high to the Current byte, then look up the table to find the CRC, and add the previous CRC */
PTR ++;
}
Return (CRC );
}
Obviously, when CRC is calculated by byte, the query table method is used, which greatly improves the computing speed. But for the widely used 8-bit microprocessor, the code space is limited, and 256 CRC redundant tables (512 bytes of memory in total) have been stretched, however, the CRC calculation speed cannot be too slow. Therefore, we will introduce the following algorithm to calculate the CRC by half-byte.
5. calculate CRC in half bytes
Similarly, for a binary sequence, the number of bytes can be expressed as the formula (5-1), where half bytes (4 in total ).
(5-1)
When calculating the CRC code of the binary sequence number, multiply it by (shifts 16 bits left) and divide it by the polynomial g (x). The remainder is the required CRC code. As shown in formula (4-2:
(5-2)
Possible values: (5-3)
It is an integer and a 16-bit binary remainder. Replace formula (5-3) into formula (5-2:
(5-4)
Because:
(5-5)
Here is the 4-digit high, which is the 12-digit low. The formula (5-5) is substituted into the formula (5-4). After sorting, you can:
(5-6)
Reset: (5-7)
It is an integer and a 16-bit binary remainder. The formula (5-7) is substituted into the formula (5-6), and the formula is as follows:
(5-8)
Obviously, the number of 16-bit binary is the required CRC code.
Formula (5-7) is the key to writing a byte CRC program. It indicates that the CRC code after calculating the Current byte is equal to the low 12-bit CRC code of the previous byte and shifted to 4-bit left, in addition, the CRC code obtained after the sum of the preceding one-byte CRC value shifted to four places (or four digits in height) and the Current byte value is used. If we calculate all the CRC values of the four-bit binary sequences, put it in a table and use the lookup table method. Every byte is calculated twice (half byte), and the speed and memory space can be balanced. It is not difficult to understand the following C language program to calculate the CRC code by half byte. * PTR points to the first byte of the sending buffer. Len indicates the total number of bytes to be sent. The CRC remainder table is obtained based on the 0x11021 polynomial.
Unsigned cal_crc (unsigned char * PTR, unsigned char Len ){
Unsigned int CRC;
Unsigned char da;
Unsigned int crc_ta [16] = {/* CRC residual table */
0x4084, 0 x, 0x2042,0x3063,0 X, 0x50a5, 0x60c6, 0x70e7,
0x8108,0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
}

CRC = 0;
While (Len --! = 0 ){
DA = (uchar) (CRC/256)/16;/* top four digits of the temporary CRC */
CRC <= 4;/* shifts the CRC value four places to the right, which is equivalent to 12 lower CRC values )*/
CRC ^ = crc_ta [da ^ (* PTR/16,
Then add the remainder of the last CRC */
DA = (uchar) (CRC/256)/16;/* store 4-bit high CRC records */
CRC <= 4;/* CRC shifts 4 bits to the right, which is equivalent to 12 bits of CRC )*/
CRC ^ = crc_ta [da ^ (* PTR & 0x0f)];/* calculate the CRC by adding the four high bits and the second half of the byte,
Then add the remainder of the last CRC */
PTR ++;
}
Return (CRC );
}
5 conclusion
The three programs described in the preceding section calculate CRC at a slow bitwise speed, but occupy the minimum memory space. The method for finding CRC by byte lookup is faster, but occupies a large amount of memory; the method to calculate CRC Based on the half-byte query table is to balance the first two, that is, it does not occupy too much memory, and the speed is not too slow. It is suitable for 8-bit small memory Single-Chip Microcomputer Applications. The above C program can be changed based on the characteristics of the microprocessor compiler, such as placing the CRC residual table in the program storage area.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.