1.Open Windows Start menu. 2.Select Run. 3.Type msconfig. 4.In the Services tab disable all items. 5.In the Startup tab uncheck all items. 6.Restart computer in Safe Mode (press F8 during bootup).
Home Page
Premiqui per la versione Italiana
The CRC is used to ensure the integrity of packets transferredbetween two MCUsor between MCU and PC connected through an interface such as UART, SPI,I2CETC.
Some latest MCUs include hardware generation of CRC for the I2C, USARTetc.
Whatwe do here is toimplement the CRC by software.
The principle of theCRC is to treat asbinary sequences of binary polynomials, that is polynomials whosecoefficientscorrespond to the binary sequence.
The binary sequence 0110101001can berepresented with the following polynomial form:
In this way, the bit ofweight less of thesequence (the rightmost bit) represents the degree 0 of thepolynomial (X0= 1), the fourth bit fromthe right representsthe degree of the polynomial 3 (X3) ...
A sequence of n bits is therefore a polynomial of maximum degree n-1.
Allpolynomial expressionsare subsequently manipulated by modulo 2.
- In practice itis assumed that the polynomial used to generate the CRC is known toboth the transmitter and receiver.
- Generator polynomial G(n + 1 bit)
- The Message(M) to betransmitted can be formed from any sequence of bits
- The CRC will be given bythe division of M / G(see explanationbelow)
- The message to be transmitted, that contains also the CRC,will be M'which is given by Mwith the CRCappended to the end of the message
- The receiver calculates the CRC on the received message M' (M' = M + CRC)and will get rest as 0(zero) if the transfer of data occurred without error.
GeneratorPolynomial Gnormally as you choose a prime number that begins and ends with 1, someexamples are:
1001- 11101 -1100000001111 (CRC12) - 11000000000000101 (CRC16)
APPLICATION
Let M be the message tobe transmitted, the valueis: 10100011 10101100
It is assumed that Gis 11010
We call M' the message with the CRC
The CRC isM / G.
The CRCcode is the result of thedivisionM /Gwhereto M are attachednbits null(0) corresponding to the degree ofG.
We assume that G = 11010this means that its gradeis 4;this means G(bit)=4sowe attach 4null bits (0) to M.
Now Mis:
M = 1010 0011 1010 11000000.
Below is an example on how to calculate the CRC.
It is assumed that Gis 11010
We call M' the message with the CRC
The CRC isM / G.
The CRCcode is the result of thedivisionM /Gwhereto M are attachednbits null(0) corresponding to the degree ofG.
We assume that G = 11010this means that its gradeis 4;this means G(bit)=4sowe attach 4null bits (0) to M.
Now Mis:
M = 1010 0011 1010 11000000.
Below is an example on how to calculate the CRC.
1010 0011 1010 1100 0000
11010||| since the leftmost bit is 1 we do the subtraction
0111 0||| here's the result after the XOR
111 00|| bring down the next digit, an since the leftmost bit is 1 we again subtract
11010|| perform the subtraction
001 10|| here's the result after the XOR
01 101| bring down the next digit, and since the leftmost bit is 0 not do the subtraction and bring down another bit
1 1011 as the leftmost bit is 1 we again subtract
11010
0 0001 here's the result after the XOR
11010||| since the leftmost bit is 1 we do the subtraction
0111 0||| here's the result after the XOR
111 00|| bring down the next digit, an since the leftmost bit is 1 we again subtract
11010|| perform the subtraction
001 10|| here's the result after the XOR
01 101| bring down the next digit, and since the leftmost bit is 0 not do the subtraction and bring down another bit
1 1011 as the leftmost bit is 1 we again subtract
11010
0 0001 here's the result after the XOR
To create M' it is sufficientto concatenate the CRCobtained to the M(message) to be transmitted:
M' = 1010 001110101100+ 1010
M' = 1010 0011101011001010
M' = 1010 001110101100+ 1010
M' = 1010 0011101011001010
Ingeneral, thealgorithm used can be represented as below.
Input there is:
M' = M + G(bit)
The loop is repeated until the last bit of M'
Input there is:
M' = M + G(bit)
The loop is repeated until the last bit of M'
If the receiver makesthe division ofM' with G and gets 0 (zero) it means that there were no errorsduringtransmission.
Below there is anexample where the CRCis concatenated to message during Tx and Rx that receive the messageandrecalculate the CRC(mustbe0).
Main Polynomials
- CRC-12 : X12 + X11+ X3 + X2 + X + 1
1100000001111 - CRC-16 : X16 + X15+ X2 + 1
11000000000000101 - CRC CCITT V41 : X16+ X12 + X5 + 1 (This code is primarily used in the HDLC)
10001000000100001 - CRC-32 (Ethernet) : =X32 + X26 + X23+ X22 + X16 + X12+ X11 + X10 + X8+ X7 + X5 + X4+ X2 + X + 1
- CRC ARPA : X24 + X23+X17 + X16 + X15+ X13 + X11 + X10+ X9 + X8 + X5+ X3 + 1
- SDLC: X16+ X12 + X5+ 1
MCUImplementation
There are two different techniques for implementing a CRC in software.
- Loop driven implementation
- Table drivenimplementation
Loop driven implementationworks likethe calculation showed in Figure 1 (for 8bit MCU).
Data is entered into shift register and shifted to the left in sequenceone ata time.
If one popsout at the MSb, thecontent is XORed with the polynomial.
In the other case, the registers are shifted by one position to theleft.
In the other case, the registers are shifted by one position to theleft.
![Cyclic Cyclic](/uploads/1/2/6/3/126370231/277599024.jpg)
Tabledriven implementation
Another method to calculatea CRC is to useprecalculated values and XOR them to the data received.
The idea behind atable driven CRCimplementation is that instead of calculating the CRC bit by bit,precomputedbytes are XORed to the data.
The advantageof the table drivenimplementation is that it isfaster thantheloop driven solution.
The drawback is that it consumes more program memory because of thesize of thelook-up table.
The CRC atthe table drivenimplementation is generated by reading a precomputed value out of atable andXOR.
Another method to calculatea CRC is to useprecalculated values and XOR them to the data received.
The idea behind atable driven CRCimplementation is that instead of calculating the CRC bit by bit,precomputedbytes are XORed to the data.
The advantageof the table drivenimplementation is that it isfaster thantheloop driven solution.
The drawback is that it consumes more program memory because of thesize of thelook-up table.
The CRC atthe table drivenimplementation is generated by reading a precomputed value out of atable andXOR.
ComparationTablefor 8bit MCU
Table driven implementation
TABLE precalculation
Table drivenimplementation
Free source code in C
Theexample Loop driven andTable drivenready to use on Windowsdeveloped by Dev-C++is here.
The Dev-C++ is free and it is possible to get it here,if you need to use myversion of Dev-C++ click here.
The Dev-C++ is free and it is possible to get it here,if you need to use myversion of Dev-C++ click here.
Theoriginal source code for theseCRC computations is placed into the public domain and is available inelectronic form at http://www.netrino.com/code/crc.zip
Otherexamples to be tested:
Polynomial: x16 + x12 + x5 + 1
Start Value: 0xFFFF
CRC_POLYNOM = 0x8408;
CRC_PRESET = 0xFFFF;
C-Example:
unsigned internal CRC = CRC_PRESET;
for (i = 0; i < cnt; i++) /* cnt = number of protocol byteswithout CRC*/
{
crc ^= DATA[i];
for (j = 0; j < 8; j++)
{
if (crc& 0x0001)
crc = (crc >> 1) ^CRC_POLYNOM;
else
crc = (crc >> 1);
}
}
Start Value: 0xFFFF
CRC_POLYNOM = 0x8408;
CRC_PRESET = 0xFFFF;
C-Example:
unsigned internal CRC = CRC_PRESET;
for (i = 0; i < cnt; i++) /* cnt = number of protocol byteswithout CRC*/
{
crc ^= DATA[i];
for (j = 0; j < 8; j++)
{
if (crc& 0x0001)
crc = (crc >> 1) ^CRC_POLYNOM;
else
crc = (crc >> 1);
}
}
References
Webpage that calculatesthe CRC:
http://www.zorc.breitbandkatze.de/crc.html
Articleson the theory ofthe CRC:
http://en.wikipedia.org/wiki/Cyclic_redundancy_check(EN)
http://it.wikipedia.org/wiki/Cyclic_redundancy_check(IT)
APAINLESS GUIDE TOCRC ERROR DETECTION ALGORITHM
http://www.geocities.com/SiliconValley/Pines/8659/crc.htm
http://www.repairfaq.org/filipg/LINK/F_crc_v3.html
ATable-Drivenimplementation for speed up the CRC calculation (Chaper 10)
http://www.repairfaq.org/filipg/LINK/F_crc_v33.html#CRCV_001
http://www.repairfaq.org/filipg/LINK/F_crc_v3.html
http://it.kioskea.net/contents/base/control.php3
http://www.microst.it/Progetti/CRC_2.html
EXAMPLEs
http://www.codeproject.com/KB/cs/csRedundancyChckAlgorithm.aspx
http://www.relisoft.com/science/CrcOptim.html
http://www.experts-exchange.com/Programming/Editors_IDEs/C_CPP_CS/CPP_Builder/Q_21192644.html
http://www.faqs.org/faqs/compression-faq/part1/section-26.html
Home Page
![Cyclic Redundancy Check Program In C With Explanation Cyclic Redundancy Check Program In C With Explanation](/uploads/1/2/6/3/126370231/535805519.jpeg)