CRC8 calc

Lets say I have an array with 8 bytes stored and I want compute the CRC code for that array(I mean all the elements inside it)
After some google search I found this polynomial x^8 + x^2 + x^1 + x^0
My question is how can I implement this
what is the value of "x" in the polynomial? Is the byte number?
if my case was:

byte elements[7] = {0x10,0x11,0x13,0x14,0x15,0x16,0x17};

How can I apply to it this polynomial

x8 + x2 + x1 + x0

In binary, X is 2.

So the polynomial translates to:

28 + 22 + 21 + 20

Which equates to, in binary:

876543210
100000111

However, the first bit is omitted - so it becomes:

76543210
00000111

Or 0x07 in hexadecimal.

This is the CCITT CRC-8 code. (9 bits). There are other variants.

The Wikipedia page on CRC's has information in implementing it: Cyclic redundancy check - Wikipedia

You apply the CRC code to the left-most chunk of your data, then slide it along the result 1 bit. Repeat, until you reach the end.

Is there already any kind of C library that I can include in my project to deal with CRC instead of trying to reinvent the wheel.

This might help - Arduino CRC-32

The OneWire library here OneWire Arduino Library, connecting 1-wire devices (DS18S20, etc) to Teensy has a CRC8 function in it. It uses a different polynomial but you could use that function as a starting point.

Pete

try
http://www.nongnu.org/avr-libc/

which has a selection of crc routines:

http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html

I cannot remember where I got this, but this in an example of one that works in the arduino environment.
I modified it because I wanted to calculate the crc a byte at a time (it was for an application where data
was being read off a serial line, and I did not want to waste the buffer space).
Perhaps you can modify it for crc8.

This was in an include file crc16.h

/**************************************************************************
//
// crc16.c - generate a ccitt 16 bit cyclic redundancy check (crc)
/
//      The code in this module generates the crc for a block of data.
//
**************************************************************************/
/*
//                                16  12  5
// The CCITT CRC 16 polynomial is X + X + X + 1.
// In binary, this is the bit pattern 1 0001 0000 0010 0001, and in hex it
//  is 0x11021.
// A 17 bit register is simulated by testing the MSB before shifting
//  the data, which affords us the luxury of specifiy the polynomial as a
//  16 bit value, 0x1021.
// Due to the way in which we process the CRC, the bits of the polynomial
//  are stored in reverse order. This makes the polynomial 0x8408.
*/

/* RH: modified original routine to a class called one char at a time */

#define POLY 0x8408

/*
// note: when the crc is included in the message, the valid crc is:
//      0xF0B8, before the complement and byte swap,
//      0x0F47, after complement, before the byte swap,
//      0x470F, after the complement and the byte swap.
*/

  
  class Calc_Crc16 {
  private:
  unsigned int data;
  unsigned int crc;
  
  public:
  const static short crc_ok = 0X470F;

  Calc_Crc16() : crc ( 0XFFFF) 
  {
    
  } 
  
  void reset()
  {
        crc = 0XFFFF;
  }

  void next (char dval)
  {
    int i;
                  for (i = 0, data = (unsigned int)0xff & dval;
                  i < 8;
                  i++, data >>= 1) {
                    if ((crc & 0x0001) ^ (data & 0x0001))
                         {  crc = (crc >> 1) ^ POLY;}
                    else
                         {  crc >>= 1;}
                  }    
  }
  
  // taking copies of variables so this can be called validly more than once
  short get_crc() { 
       short crc1 = ~crc;      
       short data1 = crc1;
       crc1 = (crc1 << 8) | (data1 >> 8 & 0xFF);
       return crc1;
   }

  }
;

This code snippet shows how to include, declare and use the routine above:

#include "crc16.h"

Calc_Crc16 crc16;

void set_tag_from_eprom()
{
  g_tag = 0;
  crc16.reset();
  for (g_decode_index = 0; g_decode_index < 4; g_decode_index++)
  {
    g_arr_decode[g_decode_index] = EEPROM.read(EEPROM_TAG_ADD + g_decode_index);   
    crc16.next(g_arr_decode[g_decode_index]);
  }
  if (crc16.get_crc() == crc16.crc_ok)
  {
    g_tag = g_arr_decode[1];
    report_error_now();
  }  
} // end function