#include <stdint.h>
#include <stdio.h>
uint16_t crc16(uint8_t *data, uint16_t length) {
uint16_t crc = 0xFFFF; // Инициализация контрольной суммы
uint16_t polynomial = 0x1021;
for (uint16_t i = 0; i < length; i++) {
crc ^= (data[i] << 8); // Вставляем байт в старшие биты CRC
for (uint8_t j = 0; j < 8; j++) {
if (crc & 0x8000) {
crc = (crc << 1) ^ polynomial; // Сдвигаем и применяем полином
} else {
crc <<= 1; // Просто сдвигаем
}
}
}
return crc; // Возвращаем итоговый CRC
}
int main() {
uint8_t csend_DATA[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7C, 0x00, 0x7C, 0x00, 0x7C, 0x00, 0x7C,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0xF8};
uint16_t crc = crc16(csend_DATA, sizeof(csend_DATA)/sizeof(csend_DATA[0]));
printf("CRC-16: 0x%04X\n", crc
); // Выводим результат return 0;
}
I2luY2x1ZGUgPHN0ZGludC5oPgojaW5jbHVkZSA8c3RkaW8uaD4KCnVpbnQxNl90IGNyYzE2KHVpbnQ4X3QgKmRhdGEsIHVpbnQxNl90IGxlbmd0aCkgewogICAgdWludDE2X3QgY3JjID0gMHhGRkZGOyAgLy8g0JjQvdC40YbQuNCw0LvQuNC30LDRhtC40Y8g0LrQvtC90YLRgNC+0LvRjNC90L7QuSDRgdGD0LzQvNGLCiAgICB1aW50MTZfdCBwb2x5bm9taWFsID0gMHgxMDIxOwoKICAgIGZvciAodWludDE2X3QgaSA9IDA7IGkgPCBsZW5ndGg7IGkrKykgewogICAgICAgIGNyYyBePSAoZGF0YVtpXSA8PCA4KTsgIC8vINCS0YHRgtCw0LLQu9GP0LXQvCDQsdCw0LnRgiDQsiDRgdGC0LDRgNGI0LjQtSDQsdC40YLRiyBDUkMKICAgICAgICBmb3IgKHVpbnQ4X3QgaiA9IDA7IGogPCA4OyBqKyspIHsKICAgICAgICAgICAgaWYgKGNyYyAmIDB4ODAwMCkg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