0
votes

I am trying to calculate CRC for the first time. I have read a few pages that explain what is crc and how to calculate. Mainly this : https://www.fatalerrors.org/a/implementation-of-crc-checksum-c-crc16-as-an-example.html

I have a code that calculates CRC16

uint16_t CalCrc(uint16_t crc_initial, uint8_t* buf,uint16_t len,uint16_t POLY)
{
    unsigned int byte;
    unsigned char k;
    unsigned short ACC,TOPBIT;
    unsigned short remainder = crc_initial;
    TOPBIT = 0x8000;
    for (byte = 0; byte < len; ++byte)
    {
        ACC = buf[byte];
        remainder ^= ACC;
        printf("new remainder = %u \n",remainder);
        for (k = 8; k > 0; --k)
        {
            if (remainder & TOPBIT)
            {
                remainder = remainder << 1;
                remainder = remainder ^ POLY;
            }
            else
            {
                remainder = (remainder << 1);
            }
        }
    }
    remainder = remainder^0x0000;
    printf("result remainder = %02X \n",remainder);
    return remainder;
}

and I call it my main.c:

    uint8_t crc_buf[5] = {0x05,0x03,0x04,0x08,0x04};
    CalCrc(0xffff,crc_buf,5,0x1021);

The result is:

result remainder = BDD5

As you can see from my function call, I pass 0xffff as initial crc value and 0x1021 as polynomial.

I am trying to confirm if my calculation is correct with some online crc calculators such as: http://www.sunshine2k.de/coding/javascript/crc/crc_js.html https://crccalc.com/

According to the initial and polynomial values that I use, the result should be: enter image description here

Could someone help me understand what is wrong here? Thanks in advance.

1
Didn't you tracked down the intermediate values and compared them to what is expected by hand? - Jean-Baptiste Yunès
Hey @Jean-BaptisteYunès. I am not sure what do you mean. Could you clarify please - TheBestPlayer

1 Answers

1
votes

You are missing a bit shift in the calculation. Change:

        ACC = buf[byte];

to:

        ACC = (unsigned)buf[byte] << 8;