diff --git a/Inc/board_config.h b/Inc/board_config.h index 7171109..006612d 100644 --- a/Inc/board_config.h +++ b/Inc/board_config.h @@ -68,9 +68,9 @@ void usart1_irq_handler(void); /******************************************************************************/ -#define LED_RCU RCU_GPIOB -#define LED_PORT GPIOB -#define LED_PIN GPIO_PIN_1 +#define LED_RCU RCU_GPIOA +#define LED_PORT GPIOA +#define LED_PIN GPIO_PIN_7 /******************************************************************************/ diff --git a/Inc/command.h b/Inc/command.h index 23db45b..7541fc9 100644 --- a/Inc/command.h +++ b/Inc/command.h @@ -105,4 +105,8 @@ void eddy_current_report(void); void temperature_raw_value_report(void); +void eddy_current_compensated_report(void); + +void calibration_data_report(void); + #endif // COMMAND_H diff --git a/Src/command.c b/Src/command.c index ba13289..d5fc993 100644 --- a/Src/command.c +++ b/Src/command.c @@ -74,6 +74,15 @@ #define RESP_TYPE_LEN_ERR 0xF4 /**< 长度错误 */ /** @} */ +/* 温度补偿参数定义 + * TEMP_COMP_REF_TEMP_X10000: 参考温度,单位 0.0001°C + * TEMP_COMP_COEFF_NUMERATOR / TEMP_COMP_COEFF_DENOMINATOR: 温漂系数,默认表示 counts/°C + * 补偿公式: compensated = raw - (temp - ref) * coeff + */ +#define TEMP_COMP_REF_TEMP_X10000 (250000L) +#define TEMP_COMP_COEFF_NUMERATOR (0L) +#define TEMP_COMP_COEFF_DENOMINATOR (10000L) + /* ============================================================================ * 模块内部变量 * ============================================================================ */ @@ -95,6 +104,9 @@ static const uint8_t s_report_status_ok[] = { 'o', 'k' }; /**< 成功响应 static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应数据 */ /** @} */ +static int32_t tmp112_get_temperature_x10000(void); +static int64_t calculate_temperature_compensation_offset(int32_t temperature_x10000); + /* ============================================================================ * 公共接口函数 * ============================================================================ */ @@ -332,6 +344,14 @@ void handle_command(const uint8_t *frame, uint8_t len) { temperature_raw_value_report(); return; + case 5u: + eddy_current_compensated_report(); + return; + + case 6u: + calibration_data_report(); + return; + // case 201u: // M201命令 // send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok)); // return; @@ -513,20 +533,82 @@ void eddy_current_report(void) { send_response(RESP_TYPE_OK, sensor_data, sizeof(sensor_data)); } -void temperature_raw_value_report(void) { - // if (!g_temperature_sensor_report_enabled) return; - uint8_t raw_result[4]; +static int32_t tmp112_get_temperature_x10000(void) +{ uint8_t value[2] = {0}; - uint32_t raw_value = 0; + int16_t temp_raw; - // i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value); tmp112a_get_raw_temperature_value(value); - raw_value = (uint32_t)((uint16_t) (value[0] << 4) | (value[1]>>4)) * 625; - raw_result[0] = (uint8_t)(raw_value >> 24); - raw_result[1] = (uint8_t)(raw_value >> 16); - raw_result[2] = (uint8_t)(raw_value >> 8); - raw_result[3] = (uint8_t)(raw_value & 0xFF); + + temp_raw = (int16_t)(((uint16_t)value[0] << 8) | value[1]); + temp_raw >>= 4; + + return (int32_t)temp_raw * 625; +} + +static int64_t calculate_temperature_compensation_offset(int32_t temperature_x10000) +{ + int32_t delta_temp_x10000 = temperature_x10000 - TEMP_COMP_REF_TEMP_X10000; + + return ((int64_t)delta_temp_x10000 * TEMP_COMP_COEFF_NUMERATOR) / + TEMP_COMP_COEFF_DENOMINATOR; +} + +void temperature_raw_value_report(void) { + uint8_t raw_result[4]; + int32_t raw_value = tmp112_get_temperature_x10000(); + + raw_result[0] = (uint8_t)((uint32_t)raw_value >> 24); + raw_result[1] = (uint8_t)((uint32_t)raw_value >> 16); + raw_result[2] = (uint8_t)((uint32_t)raw_value >> 8); + raw_result[3] = (uint8_t)((uint32_t)raw_value & 0xFF); send_response(RESP_TYPE_OK, raw_result, sizeof(raw_result)); } + +void eddy_current_compensated_report(void) +{ + uint8_t sensor_data[4]; + uint32_t raw_result = ldc1612_get_raw_channel_result(CHANNEL_0); + + if ((raw_result & 0xF0000000u) == 0u) { + int32_t temperature_x10000 = tmp112_get_temperature_x10000(); + int64_t compensated_value = (int64_t)raw_result - + calculate_temperature_compensation_offset(temperature_x10000); + + if (compensated_value < 0) { + raw_result = 0; + } else if (compensated_value > 0x0FFFFFFFLL) { + raw_result = 0x0FFFFFFFu; + } else { + raw_result = (uint32_t)compensated_value; + } + } + + sensor_data[0] = (uint8_t)(raw_result >> 24); + sensor_data[1] = (uint8_t)(raw_result >> 16); + sensor_data[2] = (uint8_t)(raw_result >> 8); + sensor_data[3] = (uint8_t)(raw_result & 0xFF); + + send_response(RESP_TYPE_OK, sensor_data, sizeof(sensor_data)); +} + +void calibration_data_report(void) +{ + uint8_t payload[9]; + uint32_t eddy_raw_value = ldc1612_get_raw_channel_result(CHANNEL_0); + int32_t temperature_raw_value = tmp112_get_temperature_x10000(); + + payload[0] = (uint8_t)(eddy_raw_value >> 24); + payload[1] = (uint8_t)(eddy_raw_value >> 16); + payload[2] = (uint8_t)(eddy_raw_value >> 8); + payload[3] = (uint8_t)(eddy_raw_value & 0xFF); + payload[4] = 0xFF; + payload[5] = (uint8_t)((uint32_t)temperature_raw_value >> 24); + payload[6] = (uint8_t)((uint32_t)temperature_raw_value >> 16); + payload[7] = (uint8_t)((uint32_t)temperature_raw_value >> 8); + payload[8] = (uint8_t)((uint32_t)temperature_raw_value & 0xFF); + + send_response(RESP_TYPE_OK, payload, sizeof(payload)); +} \ No newline at end of file diff --git a/Src/main.c b/Src/main.c index 7f8c988..d4b9476 100644 --- a/Src/main.c +++ b/Src/main.c @@ -97,7 +97,7 @@ int main(void) #ifndef EDDY_DRIVE_CURRENT_DETECTION command_process(); delay_ms(10); - // if (g_eddy_current_sensor_report_enabled) + if (g_eddy_current_sensor_report_enabled) eddy_current_report(); #else