As the functions of data acquisition systems become increasingly powerful, due to the influence of many possible factors such as transmission distance and field conditions, unpredictable errors often occur in the communication data between computers and controlled devices. In order to prevent the impact of errors, error checking must be performed at the receiving end of the data. Although error checking can also be completely borne by hardware, since both microcontrollers and PCs have strong software programming capabilities, this provides a prerequisite for implementing software error checking, and software error checking has the advantages of being economical and practical and does not increase hardware costs.
1 Principle of CRC method
Traditional error detection methods include: parity check method, checksum method, row and column redundancy check method, etc. These methods all add a certain number of redundant bits after the data and send them out at the same time. For example, in the communication modes 2 and 3 of the single-chip microcomputer, TB8 can be sent out together with the data as a parity check bit. At the receiving end of the data, the data information is compared, judged or simply summed, and then the obtained and received redundant bits are compared. If they are equal, it is considered that the data is received correctly, otherwise it is considered that an error occurs during the data transmission process. However, the redundant bits can only reflect the parity of the data row or column, so this type of inspection method is not sensitive to the even number of errors in the data row or column, and the probability of missed judgment is very high. Therefore, the reliability of this method is poor.
The English name of cyclic redundancy check is Cyclical Redundancy Check, or CRC for short. It uses the principle of division and remainder for error detection. In actual application, the sending device calculates the CRC value and sends it to the receiving device along with the data. The receiving device recalculates the CRC for the received data and compares it with the received CRC. If the two CRC values are different, it means that an error has occurred in the data communication. Since this method of obtaining the check code has a strong information coverage capability, it is an extremely efficient error checking method. The probability of error is almost zero. This redundant check communication protocol is used in many instruments and equipment.
According to different application environments and habits, CRC can be divided into the following standards:
① CRC-12 code;② CRC-16 code;
③ CRC-CCITT code;④ CRC-32 code.
CRC-12 code is usually used to transmit 6-bit character strings. CRC-16 and CRC-CCITT codes are used to transmit 8-bit characters, of which CRC-16 is used by the United States and CRC-CCITT is used by European countries. CRC-32 code is mostly used in a type of synchronous transmission called Point-to-Point.
2 CRC check code generation process
We take the most commonly used CRC-16 code as an example.
The redundant cyclic code consists of 2 bytes, i.e. 16-bit binary numbers. First, all 16-bit registers are preset to 1, and then every 8 bits of data information are processed step by step. When performing CRC calculation, only 8 data bits, start bit and stop bit, and parity bit if there is one, are used, and they are not involved in CRC calculation.
When calculating the CRC code, the 8-bit data is XORed with the data in the register, the resulting data is shifted one bit lower, the highest bit is filled with 0, and then the lowest bit is checked. If the lowest bit is 1, the contents of the register are XORed with the preset number; if the lowest bit is 0, no XOR calculation is performed.
This process is repeated 8 times. After the 8th shift, the next 8-bit data is XORed with the current content in the register. This process is repeated 8 times. When all the information is processed, the final content in the register is the CRC code. This CRC code will be sent by the sending device together with the last data.
The steps to calculate CRC are:
(1) Preset the 16-bit register to the hexadecimal number FFFF (i.e., all 1s). This register is called the CRC register.
(2) XOR the first 8-bit data with the low bit of the 16-bit register and place the result in the CRC register;
(3) Shift the contents of the register right one bit (towards the lower bit), fill the highest bit with 0, and check the lowest bit;
(4) If the lowest bit is 0, repeat step 3 (shift again);
If the lowest bit is 1, the CRC register is XORed with the polynomial code;
(5) Repeat steps 3 and 4 until the data is right-shifted 8 times, so that the entire 8-bit data is processed;
(6) Repeat steps 2 to 5 to process the next 8 bits of data;
(7) The final CRC register is the CRC code. [page]
3 CRC software implementation
/******************************************************
Function: Calculate the CRC16 checksum value
Modified date: 2006.7.4
To be modified: OK
Parameters: *STr points to txbuf, the array to be sent;
num is the number of bytes in the message
The final calculated result is a 2-byte number.
During MODBUS transmission, the CRC low bit is in front, and crc%256 is used to calculate the low bit;
The high bit comes last, crc/256 seeks the high bit. ?********************************************************/
uint crc16(uchar *str,uint num) //CRC calculation subroutine,
{
uchar i;
//uint crc;
crc=0xffff;
for (i=0; i {
arc= (str[i] ^ crc) & 0x00ff;
crc=_irol_(crc,8); //Integer circular right shift instruction
crc= crc & 0x00ff;
crc = crc ^ crctable[arc];
}
return(crc);
}
/****************************************************** ************
*************
Function: CRC check function
Modified date: 2006.7.4
To be modified: OK
Parameters: N is the number of bytes in the message, rxbuf[] is the receiving message area
If the final calculation result CRC==0, it means that the message is correct during the transmission process.
*************************************************** ***********
*************/
void crc_verify(uchar N) //CRC verification program
{
uchar i;
crc = 0xFFFF; //modbus_crc initial value
for (i=0; i<=7;i++ ) //CRC check method can be processed as a subroutine
{
arc= (rxbuf[i] ^ crc) & 0x00FF; //xor
crc=_irol_(crc,8); //Integer circular right shift instruction
crc= crc & 0x00FF;
crc= crc ^ crctable[arc]; //xor
_nop_();
}
}
uint code crctable[]={ //CRC calculation table
0x0000,0xC0C1,0xC181,0x0140,0xC301,0x03C0,0x0280,0xC241,
0xC601,0x06C0,0x0780,0xC741,0x0500,0xC5C1,0xC481,0x0440,
0xCC01,0x0CC0,0x0D80,0xCD41,0x0F00,0xCFC1,0xCE81,0x0E40,
0x0A00,0xCAC1,0xCB81,0x0B40,0xC901,0x09C0,0x0880,0xC841,
0xD801,0x18C0,0x1980,0xD941,0x1B00,0xDBC1,0xDA81,0x1A40,
0x1E00,0xDEC1,0xDF81,0x1F40,0xDD01,0x1DC0,0x1C80,0xDC41,
0x1400,0xD4C1,0xD581,0x1540,0xD701,0x17C0,0x1680,0xD641,
0xD201,0x12C0,0x1380,0xD341,0x1100,0xD1C1,0xD081,0x1040,
0xF001,0x30C0,0x3180,0xF141,0x3300,0xF3C1,0xF281,0x3240,
0x3600,0xF6C1,0xF781,0x3740,0xF501,0x35C0,0x3480,0xF441,
0x3C00,0xFCC1,0xFD81,0x3D40,0xFF01,0x3FC0,0x3E80,0xFE41,
0xFA01,0x3AC0,0x3B80,0xFB41,0x3900,0xF9C1,0xF881,0x3840,
0x2800,0xE8C1,0xE981,0x2940,0xEB01,0x2BC0,0x2A80,0xEA41,
0xEE01,0x2EC0,0x2F80,0xEF41,0x2D00,0xEDC1,0xEC81,0x2C40,
0xE401,0x24C0,0x2580,0xE541,0x2700,0xE7C1,0xE681,0x2640,
0x2200,0xE2C1,0xE381,0x2340,0xE101,0x21C0,0x2080,0xE041,
0xA001,0x60C0,0x6180,0xA141,0x6300,0xA3C1,0xA281,0x6240,
0x6600,0xA6C1,0xA781,0x6740,0xA501,0x65C0,0x6480,0xA441,
0x6C00,0xACC1,0xAD81,0x6D40,0xAF01,0x6FC0,0x6E80,0xAE41,
0xAA01,0x6AC0,0x6B80,0xAB41,0x6900,0xA9C1,0xA881,0x6840,
0x7800,0xB8C1,0xB981,0x7940,0xBB01,0x7BC0,0x7A80,0xBA41,
0xBE01,0x7EC0,0x7F80,0xBF41,0x7D00,0xBDC1,0xBC81,0x7C40,
0xB401,0x74C0,0x7580,0xB541,0x7700,0xB7C1,0xB681,0x7640,
0x7200,0xB2C1,0xB381,0x7340,0xB101,0x71C0,0x7080,0xB041,
0x5000,0x90C1,0x9181,0x5140,0x9301,0x53C0,0x5280,0x9241,
0x9601,0x56C0,0x5780,0x9741,0x5500,0x95C1,0x9481,0x5440,
0x9C01,0x5CC0,0x5D80,0x9D41,0x5F00,0x9FC1,0x9E81,0x5E40,
0x5A00,0x9AC1,0x9B81,0x5B40,0x9901,0x59C0,0x5880,0x9841,
0x8801,0x48C0,0x4980,0x8941,0x4B00,0x8BC1,0x8A81,0x4A40,
0x4E00,0x8EC1,0x8F81,0x4F40,0x8D01,0x4DC0,0x4C80,0x8C41,
0x4400,0x84C1,0x8581,0x4540,0x8701,0x47C0,0x4680,0x8641,
0x8201,0x42C0,0x4380,0x8341,0x4100,0x81C1,0x8081,0x4040
};
Previous article:Design of a small program-controlled exchange based on SM8951
Next article:Idle mode and power-down mode of microcontroller
Recommended ReadingLatest update time:2024-11-16 22:19
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Bluetooth wireless communication technology
- What is the best domestic 32bit arm mcu solution to replace stm32 m3/m4?
- Working principle and function of adjustable chip capacitor
- Circuit diagrams of 250 IC amplifiers. Free of charge, please read with caution.
- Evaluation Weekly Report: Allwinner V853 heterogeneous multi-architecture free application, Qinheng ch224PD portable soldering iron and SMT heating table are released~
- Huawei employee detained for 251 days for extortion over severance pay
- LOTO oscilloscope measured open-loop gain frequency response curve/power supply loop response stability
- Can different DRAMs be mixed and matched?
- How many kilometers does it take to change tires? [I recently rented a car and wanted to know more]
- TMS320C6748 development board EDMA routine