The CRC

Central Remedial Clinic

CRC is an easy but powerful technique to obtain frame data reliability. You may find really complex explanations on how it works, they do not fall into the scope of this tutorial. Putting it simply, you correspond a data byte to a polynomial of the form X^7+X^6+...+X^0. Both communication sides have agreed on a divider which is called CRC and in our case is X^8+X^5+X^4+X^0. The transmitter does the division and appends the remainder to the data frame. This remainder is called checksum (this trick started with sending the bit sum instead of the remainder). Now, upon reception the receiver does the division itself and checks if the remainder is the same as the transmitted one. If it is, communication was fine. What made this trick popular is the ease of its implemetation, with shifts and xors we can get away. Read Maxim's application note 27, have a look at the diagram and the routine.


u8 CRC8(u8 input, u8 seed)
{
    u8 i, feedback;

    for (i=0; i<8; i++)
    {
        feedback = ((seed ^ input) & 0x01);
        if (!feedback) seed >>= 1;
        else
        {
            seed ^= 0x18;
            seed >>= 1;
            seed |= 0x80;
        }
        input >>= 1;
    }

    return seed;   
}
 

This CRC routine is for a single byte. Try to get the code from the diagram. The seed is the initial value (in the box) of the checksum thus, you should call the routine with seed=0. The next step is to create a CRC routine for our 8 data bytes. It is easier than you think it is.


u8 CRC8x8(u8 *input)
{
    u8 i, check;

    check=0;
    for (i=0; i<8; i++)
    {
        check = CRC8(*input, check);
        input++;
    }
   
    return check;
}
 

Each time the new seed is the previous checksum. If you pass an array of 8 bytes to this one, it should return the CRC checksum which is the 9th byte you received ... of course if communication was flawless.

Links
http://www.ciphersbyritter.com/ARTS/CRCMYST.HTM http://www.geocities.com/SiliconValley/Pines/8659/crc.htm
 
 
Visit my other site!