1

I have an Arduino serial connection with 4 bits per transmission left for a check sum. I have never done a check sum algorithm before and am sadly lost right now.

CRC-4 implementation in C# has already started a question but the answer isn't really helping me go forward.

Input should be an integer of which only 10 bits are important. Output should be 4 bits of a byte.

Can someone explain to me how I can adapt the answer from the other question or point me another direction?

byte checksum(int message)
{
   //Check sum algorithm
}
Community
  • 1
  • 1
John Frost
  • 673
  • 1
  • 10
  • 24

1 Answers1

1

well the other algorithm is easily translatable to pure C:

uint8_t calculate(byte[] bytes) {
    uint16_t crc = 0xFFFF; // initial value
    // loop, calculating CRC for each byte of the string
    for (uint8_t byteIndex = 0; byteIndex < bytes.Length; ++byteIndex) {
        uint8_t bit = 0x80; // initialize bit currently being tested
        for (uint8_t bitIndex = 0; bitIndex < 8; ++bitIndex) {
            bool xorFlag = ((crc & 0x8000) == 0x8000);
            crc <<= 1;
            if (((bytes[byteIndex] & bit) ^ (uint8_t)0xff) != (uint8_t)0xff)
                crc = crc + 1;
            if (xorFlag)
                crc = crc ^ 0x1021;
            bit >>= 1;
        }
    }
    return (uint8_t)crc;
}

the only difference being the use of the stdint.h types.

I also changed the type of crc to be exactly a 16 bits unsigned, and for the indexes, only for sparing some arduino memory which is precious (every byte counts when you got only 2.5k of RAM! :-) )

Though, I did neither test or proof read that code, so it should be as good as the original C# one. If it's buggy, that one will be as well.

EDIT: As the OP added in comment, this resource is a good explanation of how the above CRC algorithm work: http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code. HTH

zmo
  • 24,463
  • 4
  • 54
  • 90
  • I think my problem is more understand how the algorithms actually works. For example: (crc & 0x8000) == 0x8000 what is that for? – John Frost Feb 26 '14 at 13:50
  • See bit-operatios and hexadecimal numbers. It just checks if the first bit of 16 bits (the one with the highest value) is 1. Other than that, http://en.wikipedia.org/wiki/Cyclic_redundancy_check#Computation_of_CRC is very easy to understand. Since you have only 10 bit data insetad of something like 2 byte: one byte is data&0xff and one data>>8 (if data is a unit16). The two bytes in an array will be sufficient to call this function – deviantfan Feb 26 '14 at 13:55
  • 1
    Ok, thanks. I understand the concept of CRC now. http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code also a very good link for CRC-4-ITU. – John Frost Feb 26 '14 at 14:04
  • oh, sorry, but your question was `Can someone explain to me how I can adapt the answer from the other question or point me another direction?` so basically, to adapt the answer from the other question is just a matter of adapting a few types :-) You should have said in your question that you wanted actually documentation/explanation of the algorithm ;-) Your link is indeed a good one, which explains better and more thoroughly than I'd have done myself. – zmo Feb 26 '14 at 15:46