Add M1 single-shot temp query command; NTC trimmed-mean filter

This commit is contained in:
2026-10-09 22:47:18 +08:00
parent 318d4c60f4
commit bae56ad6cb
4 changed files with 50 additions and 9 deletions
+20 -3
View File
@@ -46,13 +46,30 @@ static uint16_t ntc_read_raw_once(void) {
}
float ntc_read_temperature(void) {
uint16_t samples[NTC_SAMPLE_COUNT];
uint32_t sum = 0U;
/* 多次采样均值降噪 */
/* 采集 NTC_SAMPLE_COUNT 个样本 */
for (uint8_t i = 0; i < NTC_SAMPLE_COUNT; i++) {
sum += ntc_read_raw_once();
samples[i] = ntc_read_raw_once();
}
float adc_val = (float)sum / (float)NTC_SAMPLE_COUNT;
/* 插入排序(16 元素,开销可忽略),为修剪均值做准备 */
for (uint8_t i = 1; i < NTC_SAMPLE_COUNT; i++) {
uint16_t key = samples[i];
int8_t j = (int8_t)(i - 1);
while (j >= 0 && samples[j] > key) {
samples[j + 1] = samples[j];
j--;
}
samples[j + 1] = key;
}
/* 修剪均值:去掉每端 NTC_TRIM_COUNT 个极值(野值/毛刺)后取平均 */
for (uint8_t i = NTC_TRIM_COUNT; i < (NTC_SAMPLE_COUNT - NTC_TRIM_COUNT); i++) {
sum += samples[i];
}
float adc_val = (float)sum / (float)(NTC_SAMPLE_COUNT - 2U * NTC_TRIM_COUNT);
/* 分压点电压:上拉 R_PULL 在 VREF 侧,NTC 接 GND 侧 */
float voltage = adc_val * NTC_VREF / NTC_ADC_MAX;