GY-63_MS5611/libraries/CRC/examples/CRC12_test/CRC12_test.ino

85 lines
1.5 KiB
Arduino
Raw Normal View History

2023-08-01 11:27:01 -04:00
//
// FILE: CRC16_test.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// DATE: 2022-01-24
// (c) : MIT
2022-01-26 04:27:58 -05:00
#include "CRC12.h"
#include "CRC.h"
char str1[24] = "123456789";
char str2[24] = "123456789123456789";
CRC12 crc;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
2022-01-28 08:56:11 -05:00
// Serial.println("Verified with - http://zorc.breitbandkatze.de/crc.html \n");
2022-01-26 04:27:58 -05:00
test();
}
void loop()
{
}
void test()
{
2023-08-01 11:27:01 -04:00
// calcCRCxx() uses default parameters
2022-01-26 04:27:58 -05:00
Serial.println(crc_check((uint8_t *) str1, 9), HEX);
2023-07-13 05:08:10 -04:00
Serial.println(calcCRC12((uint8_t *) str1, 9), HEX);
2022-01-26 04:27:58 -05:00
Serial.println(crc_check((uint8_t *) str2, 18), HEX);
2023-07-13 05:08:10 -04:00
Serial.println(calcCRC12((uint8_t *) str2, 18), HEX);
2022-01-26 04:27:58 -05:00
crc.add((uint8_t*)str1, 9);
2023-07-13 05:08:10 -04:00
Serial.println(crc.calc(), HEX);
crc.add((uint8_t*)str2, 9);
Serial.println(crc.calc(), HEX);
2022-01-26 04:27:58 -05:00
2023-07-13 05:08:10 -04:00
crc.restart();
2022-01-26 04:27:58 -05:00
for (int i = 0; i < 9; i++)
{
crc.add(str1[i]);
Serial.print(i);
Serial.print("\t");
2023-07-13 05:08:10 -04:00
Serial.println(crc.calc(), HEX);
2022-01-26 04:27:58 -05:00
}
Serial.println(crc.count());
}
////
// reference function
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;
}
2023-08-01 11:27:01 -04:00
// -- END OF FILE --