MCU: AT32F423CBT6BSP版本:AT32F423_Firmware_Library_V2.0.3
原理我不会,照抄的GD32H7的CRC16配置,代码如下:
crc16_rtu_init: MODBUS-RTU CRC16配置:
void crc16_rtu_init(void)
{
/* enable crc clock */
crm_periph_clock_enable(CRM_CRC_PERIPH_CLOCK, TRUE);
crc_poly_value_set(0x8005);
crc_poly_size_set(CRC_POLY_SIZE_16B);
crc_reverse_input_data_set(CRC_REVERSE_INPUT_BY_BYTE);
crc_reverse_output_data_set(CRC_REVERSE_OUTPUT_DATA);
crc_data_reset();
}
crc16_rtu_calculate:MODBUS-RTU CRC16计算:
uint32_t crc16_rtu_calculate(void *array, uint32_t size)
{
uint8_t *data8;
uint32_t index;
data8 = (uint8_t *)array;
for(index = 0U; index < size; index++) {
REG8(CRC) = data8[index];
}
return CRC->dt;
}
测试结果:
对以下2个数组进行CRC16计算:
uint8_t TestCrcBuf1[5] = {0x01, 0x02, 0x03, 0x04, 0x05};
uint8_t TestCrcBuf2[9] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
运行结果和校验工具计算的一致
|