NTC: add open/short detection (V near rails) instead of bogus temp

This commit is contained in:
2026-10-09 19:28:28 +08:00
parent 061fa4d950
commit cda8f482d2
2 changed files with 16 additions and 5 deletions
+8 -1
View File
@@ -53,7 +53,14 @@ int main(void)
static uint16_t temp_report_counter = 0; static uint16_t temp_report_counter = 0;
if (++temp_report_counter >= 100U) { /* 主循环 10ms 一圈 → 1s */ if (++temp_report_counter >= 100U) { /* 主循环 10ms 一圈 → 1s */
temp_report_counter = 0; temp_report_counter = 0;
printf("NTC temp: %.1f C\r\n", ntc_read_temperature()); float temp = ntc_read_temperature();
if (temp <= -274.0f) {
printf("NTC SHORT (PC5 pulled low)\r\n");
} else if (temp <= -273.0f) {
printf("NTC OPEN (PC5 at VREF, check NTC wiring/solder)\r\n");
} else {
printf("NTC temp: %.1f C\r\n", temp);
}
} }
} }
#endif #endif
+8 -4
View File
@@ -57,11 +57,15 @@ float ntc_read_temperature(void) {
/* 分压点电压:上拉 R_PULL 在 VREF 侧,NTC 接 GND 侧 */ /* 分压点电压:上拉 R_PULL 在 VREF 侧,NTC 接 GND 侧 */
float voltage = adc_val * NTC_VREF / NTC_ADC_MAX; float voltage = adc_val * NTC_VREF / NTC_ADC_MAX;
/* 反解 NTC 阻值:V = VREF * R / (R_PULL + R) => R = R_PULL * V / (VREF - V) */ /* 开路/短路判定:电压贴近电源轨说明 NTC 支路没有形成分压通路 */
if (voltage <= 0.0f || voltage >= NTC_VREF) { if (voltage >= 0.97f * NTC_VREF) {
/* 开路/短路等异常,返回 -273.15 表示无效 */ return -273.15f; /* 开路(PC5 被上拉拉满,NTC 未连通) */
return -273.15f;
} }
if (voltage <= 0.03f * NTC_VREF) {
return -274.15f; /* 短路(PC5 被拉到 GND) */
}
/* 反解 NTC 阻值:V = VREF * R / (R_PULL + R) => R = R_PULL * V / (VREF - V) */
float r_ntc = NTC_R_PULL * voltage / (NTC_VREF - voltage); float r_ntc = NTC_R_PULL * voltage / (NTC_VREF - voltage);
/* B 值公式:1/T = 1/T25 + ln(R/R25)/B */ /* B 值公式:1/T = 1/T25 + ln(R/R25)/B */