Interpretation of CRC verification principle

Publisher:花钱123Latest update time:2011-10-12 Keywords:CRC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

};

Keywords:CRC Reference address:Interpretation of CRC verification principle

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

Use of STM32 built-in CRC module
All STM32 chips have a built-in hardware CRC calculation module, which can be easily applied to programs that need to communicate. This CRC calculation module uses the common calculation polynomial used in Ethernet:   X32 + X26 + X23 + X22 + X16 + X12 + X11 + X10 +X8 + X7 + X5 + X4 + X2 + X + 1 written in hexadec
[Microcontroller]
Use of STM32 built-in CRC module
STM8 CRC calculation
STM8 CRC calculation CRC check is only used to ensure the reliability of communication. Separate CRC calculators are used for data transmission and data reception. CRC is calculated by performing a programmable polynomial operation on each received bit. The CRC calculation is performed on the sampling clock edge defin
[Microcontroller]
STM32 CRC ST Library 3.0.0
void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState) Enable or disable AHB peripheral clock Enables or disables the AHB peripheral clock. For example: Enable CRC hardware check RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE); ==========================================================
[Microcontroller]
Research on CRC Algorithm
In the past two days, I studied the CRC algorithm at home because I needed to verify the data read by DS18B20 with AVR. I learned some methods on the Internet and finally figured it out. I wrote it into a C++ class library and hope it will be helpful to everyone. After testing, it was successful! Directly call CRC::
[Microcontroller]
STM32 hardware CRC and software CRC speed comparison
1. Test conditions Hardware: STM32L432KC  Main frequency: 80MHz  Compiler: IAR 8.20.1  Compiler options: High Speed ​​no size constraints  CRC generator polynomial: 0x782f 2. Test Method The software generates a CRC table in advance for query. The software CRC algorithm and the hardware CRC peripheral are used to cal
[Microcontroller]
CRC algorithm principle and C language implementation
    There is a good article about the principle of CRC algorithm and its implementation in C language  , but I dare not keep it to myself! CRC algorithm principle and C language implementation (3 methods introduced) CRC algorithm principle and C language implementation - from (I love microcontrollers)        
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号