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.