Close but your not quite there on char array. A single char can contain a value between 0 and 255. 0x07 is a way to represent the number 7 in hexadecimal and 0x22 is hexadecimal 22 (34 decimal), when the example says 0x07 0x22 that is just 2 chars not 8 as you have. Also your not letting the compiler know 'data' is an array by putting [] after the name.
int pec15Table[256];
is telling the compiler to reserve 256 int size memory locations (512 bytes as int is 2 bytes each).
char data[] = {0x7,0x22};
Does not specify the array size between [] but the data in the array is defined between the {} brackets so the compiler will still know the size the array needs to be +1
To cut a long story short here is what your looking for.
int pec15Table[256];
const int CRC15poly = 0x4599;
char data[] = {0x7,0x22};
int len;
void setup() {
init_PEC15_Table(); //run once
Serial.begin(9600); //
len = sizeof(data);
Serial.println(len);
int ergebnis = pec15(data, len);
Serial.println(ergebnis,HEX);
}
void loop() {
}
// Tabelle erstellen
void init_PEC15_Table() {
int remainder;
for (int i = 0; i < 256; i++)
{
remainder = i << 7;
for (int bit = 8; bit > 0; --bit)
{
if (remainder & 0x4000)
{
remainder = ((remainder << 1));
remainder = (remainder ^ CRC15poly);
}
else
{
remainder = ((remainder << 1));
}
}
pec15Table[i] = remainder&0xFFFF;
}
}
// Berechnung
unsigned int pec15 (char *data , int len) {
int remainder,address;
remainder = 16;//PEC seed
for (int i = 0; i < len; i++)
{
address = ((remainder >> 7) ^ data[i]) & 0xff;//calculate PEC table address
remainder = (remainder << 8 ) ^ pec15Table[address];
}
return (remainder*2);//The CRC15 has a 0 in the LSB so the final value must be multiplied by 2
}
You should really put the CRC table in flash memory to save RAM.