初始化测量温度通道
cpp
void temp_ad_init(void)
{
adc_add_sample_ch(AD_CH_PA10);
gpio_set_die(IO_PORTA_10, 0);
gpio_set_direction(IO_PORTA_10, 1);
gpio_set_pull_down(IO_PORTA_10, 0);
gpio_set_pull_up(IO_PORTA_10, 0);
}
cpp
#include "temperature.h"
//温度-阻值关系表:二维数组
// 温度(℃) : 电阻中心值 (KΩ)
float temperature_values[] = {
97.8396, // -20℃
92.3020, // -19℃
87.1124, // -18℃
82.2471, // -17℃
77.6837, // -16℃
73.4018, // -15℃
69.3823, // -14℃
65.6077, // -13℃
62.0616, // -12℃
58.7288, // -11℃
55.5953, // -10℃
52.6480, // -9℃
49.8747, // -8℃
47.2643, // -7℃
44.8062, // -6℃
42.4906, // -5℃
40.3086, // -4℃
38.2516, // -3℃
36.3117, // -2℃
34.4817, // -1℃
32.7547, // 0℃
31.1243, // 1℃
29.5847, // 2℃
28.1301, // 3℃
26.7556, // 4℃
25.4562, // 5℃
24.2274, // 6℃
23.0650, // 7℃
21.9650, // 8℃
20.9239, // 9℃
19.9380, // 10℃
19.0041, // 11℃
18.1193, // 12℃
17.2807, // 13℃
16.4857, // 14℃
15.7317, // 15℃
15.0164, // 16℃
14.3376, // 17℃
13.6933, // 18℃
13.0816, // 19℃
12.5005, // 20℃
11.9485, // 21℃
11.4239, // 22℃
10.9252, // 23℃
10.4510, // 24℃
10.0000, // 25℃
9.5709, // 26℃
9.1626, // 27℃
8.7738, // 28℃
8.4037, // 29℃
8.0512, // 30℃
7.7154, // 31℃
7.3953, // 32℃
7.0903, // 33℃
6.7995, // 34℃
6.5221, // 35℃
6.2576, // 36℃
6.0051, // 37℃
5.7642, // 38℃
5.5342, // 39℃
5.3146, // 40℃
5.1049, // 41℃
4.9045, // 42℃
4.7130, // 43℃
4.5300, // 44℃
4.3551, // 45℃
4.1878, // 46℃
4.0278, // 47℃
3.8748, // 48℃
3.7283, // 49℃
3.5882, // 50℃
3.4540, // 51℃
3.3255, // 52℃
3.2025, // 53℃
3.0846, // 54℃
2.9717, // 55℃
2.8635, // 56℃
2.7597, // 57℃
2.6603, // 58℃
2.5649, // 59℃
2.4734, // 60℃
2.3856, // 61℃
2.3014, // 62℃
2.2206, // 63℃
2.1431, // 64℃
2.0686, // 65℃
1.9970, // 66℃
1.9283, // 67℃
1.8623, // 68℃
1.7989, // 69℃
1.7380 // 70℃
};
double real_voltage;
double res_value;
u8 temp_count=0;
double temp_samples[5]; // 存储五次采样值
void adc_temperature()
{
temp_samples[temp_count] = adc_get_voltage(AD_CH_PA10);
// 采集五次样本
temp_count++;
if(temp_count < 5) { // 未满5次采样时直接返回
return;
}
// 冒泡排序
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4 - i; j++) {
if(temp_samples[j] > temp_samples[j+1]) {
double temp = temp_samples[j];
temp_samples[j] = temp_samples[j+1];
temp_samples[j+1] = temp;
}
}
}
// printf("temp_samples:%d mv\n\n",(int)temp_samples[0]);
// printf("temp_samples:%d mv\n\n",(int)temp_samples[1]);
// printf("temp_samples:%d mv\n\n",(int)temp_samples[2]);
// printf("temp_samples:%d mv\n\n",(int)temp_samples[3]);
// printf("temp_samples:%d mv\n\n",(int)temp_samples[4]);
// 取中间三个值求平均
double avg_voltage = (temp_samples[1] + temp_samples[2] + temp_samples[3]) / 3.0;
temp_count = 0; // 重置计数器
// ... 保持后续计算逻辑不变 ...
printf("temp_voltage:%d mv\n\n",(int)avg_voltage);
res_value=(10*avg_voltage)/(3300-avg_voltage);
printf("res_value:%d ohm\n\n", (int)(res_value*1000));
// 温度值查找
int temp = -20; // 默认最低温度
int current_value=res_value*1000;
for(int i = 0; i < sizeof(temperature_values)/sizeof(float); i++) {
// 找到第一个小于等于实际阻值的索引
int target_value=temperature_values[i]*1000;
if(current_value >= target_value) {
printf("res_value:%d ohm\n\n", (int)(res_value*1000));
printf("temperature_values:%d ohm\n\n", (int)(temperature_values[i]*1000));
temp = i - 20;
break;
}
}
int temperature = temp*10;
printf("当前温度:%d C\n", temp);
}
void temp_init(void)
{
printf("temp_init\n");
sys_timer_add(NULL,adc_temperature,200);
}
(int)res_value*1000 > (int)temperature_values[i]*1000, 这种写法先将浮点数转换为整数,再进行比较,会导致精度损失。之前我这么写采到的温度一直不准,28度直接跳到31度,换了上述写法才准了