2
votes

I am trying to write an implementation of a CRC8 checksum for a pic micro controller. I am basing my algorithm off of the one found on this website whose algorithm I have tested and is working.

The only difference is I am making my CRC8 function take a uint16_t as an input and return a uint8_t as an output, instead of binary ascii values. I have copied their code as well as I can but it does not seem to be getting me the same values as their code.

#include <stdio.h>
#include <stdint.h>

uint8_t crc8(uint16_t input);

int main()
{
    uint8_t temp1;
    uint16_t temp2 = 0xAA79;

    printf("CRC input is 0x%X\n", temp2);

    temp1 = crc8(temp2);

    printf("CRC output is 0x%X\n", temp1);

    return 0;
}

uint8_t crc8(uint16_t input)
{
    uint8_t crc[8] = { 0 };
    uint8_t i;
    uint8_t inv;
    uint8_t output = 0;

    for(i = 0; i < 16; i++)
    {
        inv = ((((input >> i) & 1) ^ crc[7]) & 1);

        crc[7] = (crc[6] & 1);
        crc[6] = (crc[5] & 1);
        crc[5] = (crc[4] ^ inv & 1);
        crc[4] = (crc[3] ^ inv & 1);
        crc[3] = (crc[2] & 1);
        crc[2] = (crc[1] & 1);
        crc[1] = (crc[0] & 1);
        crc[0] = (inv & 1);
    }

    for(i = 0; i < 8; i++)
    {
        output |= ((crc[i] << i) & (1 << i));
    }


    return output;
}

The program should return a value of 0x61 for the input of 0xAA79. The crc polynomial is x^8+x^5+x^4+1 if anyone is wondering.

1

1 Answers

3
votes

Your program does work: but you have the bit sequence the wrong way. This below has the linked code and your posted code. The string bs[] has the same bit sequence as temp2 yet fails. The second attempt with a reversed bit string sb[] matches your function output.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

uint8_t crc8(uint16_t input)
{
    uint8_t crc[8] = { 0 };
    uint8_t i;
    uint8_t inv;
    uint8_t output = 0;

    for(i = 0; i < 16; i++)
    {
        inv = ((((input >> i) & 1) ^ crc[7]) & 1);

        crc[7] = (crc[6] & 1);
        crc[6] = (crc[5] & 1);
        crc[5] = (crc[4] ^ inv & 1);
        crc[4] = (crc[3] ^ inv & 1);
        crc[3] = (crc[2] & 1);
        crc[2] = (crc[1] & 1);
        crc[1] = (crc[0] & 1);
        crc[0] = (inv & 1);
    }

    for(i = 0; i < 8; i++)
    {
        output |= ((crc[i] << i) & (1 << i));
    }

    return output;
}

char *MakeCRC(char *BitString)
{
    static char Res[9];                                 // CRC Result
    char CRC[8];
    int  i;
    char DoInvert;

    for (i=0; i<8; ++i)  CRC[i] = 0;                    // Init before calculation

    for (i=0; i<strlen(BitString); ++i)
        {
        DoInvert = ('1'==BitString[i]) ^ CRC[7];         // XOR required?

        CRC[7] = CRC[6];
        CRC[6] = CRC[5];
        CRC[5] = CRC[4] ^ DoInvert;
        CRC[4] = CRC[3] ^ DoInvert;
        CRC[3] = CRC[2];
        CRC[2] = CRC[1];
        CRC[1] = CRC[0];
        CRC[0] = DoInvert;
        }

    for (i=0; i<8; ++i)  Res[7-i] = CRC[i] ? '1' : '0'; // Convert binary to ASCII
    Res[8] = 0;                                         // Set string terminator

    return(Res);
}

int main()
    {
    uint8_t temp1;
    uint16_t temp2 = 0xAA79;
    char bs[] = "1010101001111001";
    char sb[] = "1001111001010101";
    printf("CRC input is 0x%X\n", temp2);
    temp1 = crc8(temp2);
    printf("CRC output is 0x%X\n", temp1);

    printf("Web ver output is %s\n", MakeCRC(bs));
    printf("Web rev output is %s\n", MakeCRC(sb));

    return 0;
}

Program output:

CRC input is 0xAA79
CRC output is 0xAB
Web ver output is 01100001
Web rev output is 10101011