在使用WB031芯片做开发时,外接了10个温度芯片,通过单总线协议one-wire与MCU进行通信。使用官方SDK进行开发的,但是我发现采集的温度数据有时候会出现为0等错误情况,我目前猜测是任务调度过程中,频繁中断会打断单总线协议的时序,有没有大佬提供一点思路啊?
//main函数
int main(void)
{
//for hold the SWD before sleep
delay_n_10us(200*1000);
OW_Init();
OW_RecallE2();
// Serial_Init();
app_ble_init();
// periph init
// LedInit(LED1_PORT,LED1_PIN); // power led
// LedInit(LED2_PORT,LED2_PIN); //connection state
// LedOn(LED1_PORT,LED1_PIN);
while (1)
{
/*schedule all pending events*/
rwip_schedule();
ns_sleep();
}
}
////////////////////////////
///////app_htps.c部分函数
void app_htpt_temp_timeout_handler(void)
{
// 1. 采集所有传感器温度
GetTemp();
// 2. 发所有传感器数据
for (uint8_t i = 0; i < app_htpt_env.sensor_count; i++) {
// Serial_SendString("\r\n");
// Serial_SendString("Sensor");
// Serial_SendNumber(i, 1);
// Serial_SendString(":");
// Serial_SendFloat(g_TempValue);
// Serial_SendString("\r\n");
app_htpt_temp_send(g_TempValue, i);
}
// 3. 仅当通知启用时重启定时器
// if (app_htpt_env.ntf_cfg & HTPT_CFG_INTERM_MEAS_NTF) {
ns_timer_cancel(health_thermometer_timer_id);
health_thermometer_timer_id = ns_timer_create(
HEALTH_THERMOMETER_SEND_DELAY,
app_htpt_temp_timeout_handler
);
// }
}
// 依次发送每个通道的数据,间隔500ms
void app_htpt_temp_send(float temp_celsius, uint8_t sensor_id)
{
struct htpt_temp_send_req *req = KE_MSG_ALLOC(
HTPT_TEMP_SEND_REQ,
prf_get_task_from_id(TASK_ID_HTPT),
TASK_APP,
htpt_temp_send_req
);
// 使用正确的 IEEE 11073 浮点格式
int32_t mantissa = (int32_t)(temp_celsius * 100); // 转换为 0.01°C 单位
req->temp_meas.temp = (uint32_t)mantissa & 0x00FFFFFF; // 24位尾数
req->temp_meas.temp |= (0xFE << 24); // 指数 -2 (0xFE = -2的补码)
// 设置标志位:包含温度类型
req->temp_meas.flags = HTP_FLAG_TYPE_BIT;
req->temp_meas.type = sensor_id + 1; // 温度类型 (1-9)
// 关键:设置为非稳定测量(使用中间温度特性)
req->stable_meas = FALSE;
ke_msg_send(req);
}
//////////////////////////////////////////
|