RobTillaart / CRC

CRC library for Arduino
MIT License
79 stars 17 forks source link

Binary CRC8 #27

Closed Fluxanode closed 2 years ago

Fluxanode commented 2 years ago

Will this lib work on a 24bit string? I need to calculate the CRC8 for a 24bit binary string. I will append the CRC to the 24bit string to sent a SPI transaction from a teensy arduino to an ad74412r ADC device.

Can you point me to an example?

Thanks, Chuck

RobTillaart commented 2 years ago

Thanks for the question, it is pretty straightforward as you have a fixed length array. The code below should get you started:

#include "CRC.h"

uint8_t  binaryString[24] = { 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3 };

void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);

  //  from CRC.h
  //  uint8_t crc8(const uint8_t *array, uint16_t length, const uint8_t polynome = 0xD5, const uint8_t startmask = 0x00, const uint8_t endmask = 0x00, const bool reverseIn = false, const bool reverseOut = false);

  Serial.println( crc8(binaryString, 24), HEX);  // add or adjust other parameters if needed
  Serial.println( crc8(binaryString, 24), DEC);  // add or adjust other parameters if needed
  Serial.println( crc8(binaryString, 24), BIN);  // add or adjust other parameters if needed
}

if this solves the issue, you may close it.

Fluxanode commented 2 years ago

received an error when i tried to run it...

sketch_jul01b:12: error: expected unqualified-id before 'const' uint8_t endmask = 0x00, const bool reverseIn = false, const bool reverseOut = false); ^ sketch_jul01b:12: warning: unused variable 'endmask' uint8_t endmask = 0x00, const bool reverseIn = false, const bool reverseOut = false); ^ Using library CRC at version 0.3.1 in folder: D:\ChuckW\Arduino\libraries\CRC expected unqualified-id before 'const'

RobTillaart commented 2 years ago

there is apparently a \n introduced in the copy / paste process.

I edited the sketch above, please try again

Fluxanode commented 2 years ago

ok works now . I don't understand the string binaryString. My data will only be 1s and 0s as a 24 bit binary array, will this still work?

RobTillaart commented 2 years ago

post your code

my mistake I interpreted it as 24 bytes

Fluxanode commented 2 years ago

Also what is the polynomial being used? I need to use C(x) = x8 + x2 + x1 + 1.

Please be patient I'm still learning this CRC stuff.

RobTillaart commented 2 years ago

24 bits = 3 bytes, so you shoul try this

#include "CRC.h"

uint8_t  binaryString[3] = { 0b10011001, 0b01010101, 0b11110000 };  //  3 bytes as 24 bits

void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);

  //  from CRC.h
  //  uint8_t crc8(const uint8_t *array, uint16_t length, const uint8_t polynome = 0xD5, const uint8_t startmask = 0x00, const uint8_t endmask = 0x00, const bool reverseIn = false, const bool reverseOut = false);

  Serial.println( crc8(binaryString, 3), HEX);
}
Fluxanode commented 2 years ago

Thanks i believe this may work, I'll work with it and let you know. I'll close the issue if all is good. Thank you for helping me with this

RobTillaart commented 2 years ago

Also what is the polynomial being used? I need to use C(x) = x8 + x2 + x1 + 1.

x8 + x2 + x1 + 1 ==>
1*x8 + 0*x7 + 0*x6 + 0*x5 + 0*x4 + 0*x3 + 1*x2 + 1*x1 + 1*x0  ==>
1      0      0      0      0      0      1      1      1  ==>
1 0000 0111  ==>   (highest power is omitted)
0000 0111 ==> 
0x07  ==>

Serial.println( crc8(binaryString,     3,      0x07), HEX);
                        string       length  polynome
Fluxanode commented 2 years ago

What is the x in the equation? That is confusing to me.

Fluxanode commented 2 years ago

I think i answered my own question. is x the value of the bit at that location? And when they ask for the initial value what is that and most of the time i see it as 0?

RobTillaart commented 2 years ago

this explains all you ever want to know about CRC

(there are many sites about this subject)

Fluxanode commented 2 years ago

I think the CRC8 polynomial is not the one i need as I'm not getting the correct result. I need it to be x8 + x2 + x1 + 1. Can you add that poly to the code?

Fluxanode commented 2 years ago

E.g. 0x41002E has a CRC of 0x27

RobTillaart commented 2 years ago

Have you verified e.g with https://crccalc.com/ ?

RobTillaart commented 2 years ago

You need to set the correct polynome

#include "CRC.h"

uint8_t  binaryString[3] = { 0x41, 0x00, 0x2E };  //  3 bytes as 24 bits

void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);

  //  from CRC.h
  //  uint8_t crc8(const uint8_t *array, uint16_t length, const uint8_t polynome = 0xD5, const uint8_t startmask = 0x00, const uint8_t endmask = 0x00, const bool reverseIn = false, const bool reverseOut = false);

  Serial.println( crc8(binaryString, 3, 7), HEX);
}

void loop()
{
}

// -- END OF FILE --

output 27

Fluxanode commented 2 years ago

Thanks that works. I was thinking it was something like that!