RobTillaart / CRC

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

how to get crc12? #13

Closed agusfaudin closed 2 years ago

agusfaudin commented 2 years ago

I have a problem to program crc12 on arduino, can you provide a solution or explanation how to get crc12 with your library?

thanks very much

RobTillaart commented 2 years ago

Thanks for raising the issue.

The library does not support CRC12. In fact i have never seen iit. Do you have a link to it or a datasheet mentioning it ?

Closed it accidentally.

agusfaudin commented 2 years ago

Oh, I see

I'm researching how to access the Windbell brand ATG with the RS485 protocol,

I attached the reference

if you have an idea to make crc12, I says thank you,

because it really helps, makes the program concise,

crc12

RobTillaart commented 2 years ago

Looks pretty straightforward to add into the library. Will give it a try today.

RobTillaart commented 2 years ago

Typed it over so it becomes usable - testable.

uint16_t crc_check(uint8_t * ptr, uint8_t length)   // crc12
{
  uint16_t crc12out = 0;
  uint8_t i, j;
  for (j = 0; j < length; j++)
  {
    for (i = 0; i < 8; i++)
    {
      if (*(ptr + j) & (0x80 >> i)) crc12out |= 0x01;
      if (crc12out >= 0x1000) crc12out ^= 0x180D;
      crc12out <<= 1;
    }
  }
  for (i = 0; i < 12; i ++)
  {
    if (crc12out >= 0x1000) crc12out ^= 0x180D;
    crc12out <<= 1;
  }
  crc12out >>= 1;
  return crc12out;
}

This CRC-12 has some extras which I do not have in my other CRC functions. All these functions follow the same pattern so I strongly believe this version is proprietary for the Windbell brand ATG with the RS485 protocol,

The proprietary part is at least that it adds 12 zero bits at the end of the data array. For a communication protocol that does a CRC on packets I can understand.

The core loops are implemented differently but they might be similar (just different implemented)

As it is proprietary it will not be added to the library sec for now. Need to investigate more to understand the diffs. BUt the above implementation should work for you

RobTillaart commented 2 years ago

found a twist, the polynome 0x180D is order 13 while it should be order 12. Think that is part of the different implementation.

to be continued..

RobTillaart commented 2 years ago

Think I got it, Please download the develop branch and verify if it works for you - https://github.com/RobTillaart/CRC/tree/develop

It also includes a small test sketch that compares with the reference above.