Shared NTC sampling + M2 continuous temperature report

This commit is contained in:
2026-10-09 23:56:58 +08:00
parent bae56ad6cb
commit 6d9f1f54a8
5 changed files with 112 additions and 9 deletions
+67 -2
View File
@@ -61,6 +61,13 @@ static const uint8_t s_report_status_ok[] = { 'o', 'k' }; /**< 成功响应数
static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应数据 */
/** @} */
/** @name M2 连续温度上报状态
* @{ */
static bool s_temp_report_enabled = false; /**< 连续上报使能(M2 切换) */
static uint16_t s_temp_report_period_ms = 500; /**< 上报间隔(ms),M2S<ms> 设置,100~60000 */
static uint16_t s_temp_report_counter = 0; /**< 主循环 10ms 圈计数 */
/** @} */
void system_software_reset(void)
{
// 确保所有待处理的内存访问完成
@@ -373,9 +380,9 @@ void handle_command(const uint8_t *frame, uint8_t len) {
/* ==========================================
* M1 单次查询温度值
* ========================================== */
case 1u: // M1: 查询当前 NTC 温度,响应 payload 为 ASCII 摄氏度(如 "23.8")
case 1u: // M1: 查询最新 NTC 温度(后台 100ms 采样值),payload 为 ASCII 摄氏度(如 "23.8")
{
float temp = ntc_read_temperature();
float temp = ntc_get_temperature();
char temp_str[8];
uint8_t n;
@@ -391,6 +398,37 @@ void handle_command(const uint8_t *frame, uint8_t len) {
}
return;
/* ==========================================
* M2 连续温度上报开关(画曲线用)
* M2 : 切换连续上报 使能/停止(默认间隔 500ms)
* M2S<ms> : 设置上报间隔(100~60000,10ms 步进)并开启上报
* 上报帧格式与 M1 响应相同:B5 F0 LEN [ASCII 摄氏度] CRC
* ========================================== */
case 2u:
{
uint32_t period = 0;
if (find_parameter_value(&cmd[cmd_index], (uint8_t)(cmd_len - cmd_index), 'S', &period)) {
/* 带参数:设置间隔并开启 */
if (period >= 100U && period <= 60000U) {
s_temp_report_period_ms = (uint16_t)period;
s_temp_report_enabled = true;
s_temp_report_counter = 0;
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
} else {
send_response(RESP_TYPE_PARAM_ERR, s_report_status_err, sizeof(s_report_status_err));
}
} else {
/* 无参数:切换使能 */
s_temp_report_enabled = !s_temp_report_enabled;
s_temp_report_counter = 0;
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
}
COMMAND_DEBUG("M2 report %s, period=%ums",
s_temp_report_enabled ? "ON" : "OFF", s_temp_report_period_ms);
}
return;
/* ==========================================
* M888 软件重启命令
* ========================================== */
@@ -561,6 +599,33 @@ void command_process(void) {
}
}
/**
* @brief M2 连续温度上报任务(在主循环中周期调用,主循环周期 10ms)。
* @details 使能时按 s_temp_report_period_ms 间隔向 RS485 发送温度帧,
* 帧格式与 M1 响应相同(B5 F0 LEN [ASCII 摄氏度] CRC)。
*/
void command_report_task(void) {
if (!s_temp_report_enabled) return;
s_temp_report_counter++;
if (s_temp_report_counter >= (uint16_t)(s_temp_report_period_ms / 10U)) {
s_temp_report_counter = 0;
float temp = ntc_get_temperature();
char temp_str[8];
uint8_t n;
if (temp <= -274.0f) {
n = (uint8_t)sprintf(temp_str, "SHRT");
} else if (temp <= -273.0f) {
n = (uint8_t)sprintf(temp_str, "OPEN");
} else {
n = (uint8_t)sprintf(temp_str, "%.1f", temp);
}
send_response(RESP_TYPE_OK, (const uint8_t *)temp_str, n);
}
}
/**
* @brief 执行命令(简化版)
* @details 根据命令字符串直接构造协议帧并执行,无需手动构造协议帧
+6 -2
View File
@@ -48,13 +48,17 @@ int main(void)
/* RS485 数据口命令处理(D5 帧协议,具体业务命令在 command.c 扩展) */
command_process();
/* debug 串口周期打印 NTC 温度(约 1s 一次) */
/* NTC 后台采样(100ms 一次)+ M2 连续上报任务 */
ntc_sample_task();
command_report_task();
/* debug 串口周期打印 NTC 温度(约 1s 一次,读后台采样全局值) */
#if DEBUG_MODE == CFG_ENABLE
{
static uint16_t temp_report_counter = 0;
if (++temp_report_counter >= 100U) { /* 主循环 10ms 一圈 → 1s */
temp_report_counter = 0;
float temp = ntc_read_temperature();
float temp = ntc_get_temperature();
if (temp <= -274.0f) {
printf("NTC SHORT (PC5 pulled low)\r\n");
} else if (temp <= -273.0f) {
+19
View File
@@ -89,3 +89,22 @@ float ntc_read_temperature(void) {
float t_k = 1.0f / (1.0f / NTC_T25 + logf(r_ntc / NTC_R25) / (float)NTC_B_VALUE);
return t_k - 273.15f;
}
/******************************************************************************/
/* 后台采样 + 全局最新值:各消费者(debug 打印 / M1 / M2 上报)共享一次采样 */
/******************************************************************************/
static float s_ntc_last_temp = -273.15f; /* 最新温度(初值 = 开路标记) */
static uint16_t s_ntc_sample_divider = 0; /* 主循环 10ms 圈分频计数 */
void ntc_sample_task(void) {
/* 主循环周期 10ms:按 NTC_SAMPLE_PERIOD_MS 分频触发一次采样 */
if (++s_ntc_sample_divider >= (NTC_SAMPLE_PERIOD_MS / 10U)) {
s_ntc_sample_divider = 0;
s_ntc_last_temp = ntc_read_temperature();
}
}
float ntc_get_temperature(void) {
return s_ntc_last_temp;
}