IAR-YT-HEAT/DOC/temp_table.h

89 lines
3.7 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef TEMP_TABLE_H
#define TEMP_TABLE_H
#include <stdint.h>
/**
* @file temp_table.h
* @brief NTC热敏电阻温度-阻值对照表 (单位:10Ω)
*
* 数据来源:温度对照表.txt
* 数组长度238个元素
* 数据顺序:先第一列所有值,再第二列所有值,最后第三列所有值
*/
const uint16_t r_temp[] = {
9533, 9265, 9005, 8754, 8510, 8273, 8044, 7822, 7606, 7398,
7195, 6999, 6808, 6624, 6444, 6271, 6102, 5939, 5780, 5626,
5476, 5331, 5191, 5054, 4921, 4792, 4667, 4546, 4428, 4314,
4202, 4094, 3989, 3887, 3788, 3692, 3598, 3508, 3419, 3333,
3250, 3169, 3090, 3013, 2938, 2866, 2795, 2727, 2660, 2595,
2532, 2471, 2411, 2353, 2296, 2241, 2187, 2135, 2085, 2035,
1987, 1940, 1895, 1850, 1807, 1765, 1724, 1684, 1645, 1607,
1570, 1534, 1499, 1465, 1431, 1399, 1367, 1337, 1307, 1221,
1194, 1167, 1141, 1116, 1092, 1068, 1044, 1022, 1000, 978,
957, 936, 916, 896, 877, 858, 840, 822, 805, 788,
771, 755, 739, 724, 709, 694, 680, 666, 652, 639,
626, 613, 600, 588, 576, 565, 553, 542, 531, 521,
510, 500, 490, 480, 471, 462, 453, 444, 435, 427,
418, 410, 402, 395, 387, 380, 372, 365, 358, 351,
345, 338, 332, 326, 320, 314, 308, 302, 296, 291,
286, 280, 275, 270, 265, 260, 256, 251, 238, 233,
229, 225, 221, 217, 213, 210, 206, 202, 199, 195,
192, 189, 185, 182, 179, 176, 173, 170, 167, 164,
161, 159, 156, 153, 151, 148, 146, 143, 141, 139,
136, 134, 132, 130, 127, 125, 123, 121, 119, 117,
115, 114, 112, 110, 108, 107, 105, 103, 101, 100,
98, 97, 95, 94, 92, 91, 89, 88, 87, 85,
84, 83, 81, 80, 79, 78, 77, 75, 74, 73,
72, 71, 70, 69, 68, 67, 66
};
/**
* @brief 根据电阻值获取对应的温度(线性插值版)
* @param resistance 电阻值单位10Ω
* @return 对应的温度值(摄氏度)
*
* 注意温度对照表包含238个元素索引0-237
* 每个元素对应0.5°C温度步进
* 温度范围:-20.0°C 到 98.5°C
*/
static inline float get_temp_from_resistance(uint16_t resistance) {
int i = 0;
const int array_size = 238; // 实际数组大小
const int last_index = array_size - 1; // 最后一个索引237
// 处理超出上限的电阻值(大于最大电阻值)
if (resistance >= r_temp[0]) {
i = 0; // 使用第一个区间进行外推
}
// 处理低于下限的电阻值(小于最小电阻值)
else if (resistance <= r_temp[last_index]) {
i = last_index - 1; // 使用最后一个区间进行外推索引236-237
}
// 在有效范围内查找区间
else {
// 遍历237个区间索引0-236对应区间0-1,1-2,...,236-237
for (; i < last_index; i++) {
// 检查电阻值是否在当前区间 [r_temp[i], r_temp[i+1]) 内
if (resistance <= r_temp[i] && resistance > r_temp[i+1]) {
break;
}
}
}
// 获取当前区间的电阻边界值
float R_high = r_temp[i]; // 较高电阻值(对应较低温度)
float R_low = r_temp[i+1]; // 较低电阻值(对应较高温度)
// 获取当前区间的温度边界值
float T_high = -20.0f + 0.5f * i; // 较低温度
float T_low = -20.0f + 0.5f * (i+1); // 较高温度
// 最高温度:-20 + 0.5 * 237 = 98.5°C
// 线性插值计算温度
// 公式T = T_high + (R_high - resistance) * (T_low - T_high) / (R_high - R_low)
return T_high + (R_high - resistance) * (T_low - T_high) / (R_high - R_low);
}
#endif // TEMP_TABLE_H