generated from hulk/gd32e23x_template_cmake_vscode
release:(1.0.1)添加涡流传感器温度标定方法和标定命令
This commit is contained in:
102
Src/command.c
102
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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user