CRC Calibration Method

Source: Internet
Author: User
Tags 0xc0

1 , cyclic check code (CRC code):

Is the most commonly used error check code in the field of data communication, its characteristic is that the length of information field and check field can be arbitrarily selected.

2 , the basic principle of generating CRC code:

Any code consisting of a bits string can correspond to a polynomial one by one with a coefficient of only ' 0 ' and ' 1 '. For example: Code 1010111 corresponds to a polynomial of x6+x4+x2+x+1, while the polynomial is x5+x3+x2+x+1 corresponding to the code 101111.

The standard CRC-generated polynomial is shown in the following table:

Name-generating polynomial précis-writers * standard reference

CRC-4 x4+x+1 3 ITU g.704

CRC-8 x8+x5+x4+1 0x31

CRC-8 x8+x2+x1+1 0x07

CRC-8 x8+x6+x4+x3+x2+x1 0x5E

CRC-12 x12+x11+x3+x+1 80F

CRC-16 x16+x15+x2+1 8005 IBM SDLC

Crc16-ccitt x16+x12+x5+1 1021 ISO HDLC, ITU x. v.34/v.41/v.42, Ppp-fcs

CRC-32 x32+x26+x23+...+x2+x+1 04c11db7 ZIP, RAR, IEEE 802 lan/fddi, IEEE 1394, Ppp-fcs

crc-32c x32+x28+x27+...+x8+x6+1 1edc6f41 SCTP

3 , the use of CRC-16 check code:

Now choose the most common CRC-16 check, explain how it is used.

according to Modbus protocol, General 485 communication information sent in the form as follows:

Address function code data information check code

1byte 1byte Nbyte 2byte

CRC check is the checksum value of the previous data content, for a 16-bit data, when sent, low 8 bits in front, high 8 is the last.

For example: The Information field code is: 1011001, check fields: 1010.

Sender : Transmit field emitted: 1 0 1 1 0 0 1 1 0

information field Check field

receiver : Use the same calculation method to calculate the check code of the Information field, compare the actual check code received, if the equality and information is correct, not equal the information error, or will receive all the information in addition to the polynomial, if can be done, the information is correct.

4 , CRC-16 Check code calculation method:

Common table-checking method and calculation method. The calculation methods are generally:
(1), preset a 16-bit register is a hexadecimal FFFF (that is, all 1), called this register is the CRC register;
(2), the first 8-bit binary data (both the first byte of the communication information frame) and the 16-bit CRC register Low
8 bits different or, put the result in CRC Register, high eight bit data unchanged;
(3), the CRC register to the right of the content of one (toward the low) with 0 to fill the highest bit, and check the right shift after the position;
(4), if the move out bit is 0: repeat the 3rd step (one bit to the right again), if the move out bit is a 1,CRC register with multiple

Item A001 (1010 0000 0000 0001) for XOR;
(5), repeat steps 3 and 4, until the right shift 8 times, so that the entire 8-bit data are all processed;
(6), repeat step 2 to step 5, the processing of the next byte of the communication information frame;
(7), the communication information frame all bytes according to the above steps, the resulting 16-bit CRC register high and low
bytes to be exchanged;
(8), the last obtained CRC register content is: CRC code.

The polynomial A001 in the above calculation steps is the result of a 8005 bitwise reversal.

The tabular method is to make a table of the displacement XOR calculation results, that is, the 0~256 into a 16-bit length of the register low eight bits, high eight bit padding 0, and then the register and the polynomial 0xa001 according to the above 3, 4 steps, until the eight bits are all removed, the last register value is the data in the table, High eight-bit, low eight-bit table separately.

5 , providing examples of C programs (all verified through)

Check table method:

Features: Fast speed, few statements, but the table occupies a certain amount of program space.

*pucframe is the first address of the data to be verified and the Uslen is the length of the data to be verified. The return value is the checksum result.

USHORT usMBCRC16 (UCHAR * pucframe, USHORT Uslen)

{

UCHAR Uccrchi = 0xFF;

UCHAR Uccrclo = 0xFF;

int iIndex;

while (uslen--)

{

IIndex = uccrclo ^ * (pucframe++);

Uccrclo = (UCHAR) (Uccrchi ^ auccrchi[iindex]);

Uccrchi = Auccrclo[iindex];

}

Return (USHORT) (Uccrchi << 8 | uccrclo);

}

static const UCHAR auccrchi[] = {

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,

0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,

0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,

0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,

0x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BD, 0xBC, 0x7C, 0xb4, 0x74, 0x75, 0xb5,

0x77, 0xb7, 0xb6, 0x76, 0x72, 0xb2, 0xb3, 0x73, 0xb1, 0x71, 0x70, 0xb0,

0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,

0x55, 0x95, 0x94, 0x54, 0x9c, 0x5C, 0x5d, 0x9d, 0x5f, 0x9F, 0x9E, 0x5E,

0x5A, 0x9a, 0x9b, 0x5b, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,

0x4B, 0x8b, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4f, 0x8d, 0x4d, 0x4C, 0x8c,

0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,

0x41, 0x81, 0x80, 0x40

};

CRC Calibration Method

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.