#include <Arduino.h>
// CRC-16 Polynomial (0x8005) Parameters
#define CRC16_POLYNOMIAL 0x8005
#define CRC16_INITIAL_VALUE 0x0000
// Function to calculate CRC-16 (0x8005) for a byte array using shift left operators
uint16_t calculateCRC16(const uint8_t* data, size_t length) {
uint16_t crc = CRC16_INITIAL_VALUE;
for (size_t i = 0; i < length; i++) {
crc ^= ((uint16_t)data[i] << 8); // XOR with the current byte, shifted left by 8 bits
for (int j = 0; j < 8; j++) {
if (crc & 0x8000) {
crc = (crc << 1) ^ CRC16_POLYNOMIAL; // Shift left and XOR with polynomial
} else {
crc = (crc << 1); // Shift left
}
}
}
return crc;
}
void setup() {
Serial.begin(9600);
// Example data
// uint8_t data[] = {0x01, 0x02, 0x03, 0x04, 0x05};
uint8_t data[] = {0x01, 0x55, 0xAA, 0x30, 0x31, 0xC1, 0x31, 0x32, 0x33, 0x34, 0x04};
size_t dataLength = sizeof(data);
// Calculate CRC-16 (0x8005)
uint16_t crc = calculateCRC16(data, dataLength);
Serial.print("Data: ");
for (size_t i = 0; i < dataLength; i++) {
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print("\nCRC-16 (0x8005) with Shift Left: 0x");
Serial.println(crc, HEX);
}
void loop() {
// Your code here
}