This repository has been archived on 2025-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
torsion/User/board/ntc.c

97 lines
3.5 KiB
C
Raw 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.

#include "ntc.h"
#define TABLE_SIZE 181
#define NTC_SERIES_RESISTOR 10000 // 10K
#define BASE_TEMP -55
// NTC-10K-3950-B值
//-55~125°C对应的电阻阻值表(单位: Ω)
static uint32_t _table[TABLE_SIZE] = {
739500, 705664, 669165, 631466, 593686, //-55~-51
556644, 520911, 486858, 454704, 424553, 396426, 370283, 346049, 323623, 302890, //-50~-41
283730, 266022, 249649, 234498, 220466, 207454, 195372, 184139, 173681, 163931, //-40~-31
154827, 146315, 138347, 130877, 123866, 117280, 111084, 105252, 99756, 94573, //-30~-21
89682, 85063, 80699, 76574, 72672, 68982, 65489, 62183, 59052, 56087, //-20~-11
53280, 50620, 48100, 45712, 43450, 41306, 39274, 37349, 35524, 33795, //-10~-1
32116, // 0
30601, 29128, 27732, 26408, 25152, 23962, 22833, 21762, 20746, 19783, // 1~10
18868, 18000, 17177, 16395, 15652, 14947, 14277, 13641, 13036, 12461, // 11~20
11915, 11395, 10901, 10431, 10000, 9557, 9151, 8765, 8397, 8047, // 21~30
7712, 7394, 7090, 6800, 6523, 6259, 6008, 5767, 5537, 5318, // 31~40
5108, 4907, 4716, 4532, 4357, 4189, 4029, 3875, 3728, 3588, // 41~50
3453, 3324, 3200, 3081, 2968, 2859, 2754, 2654, 2558, 2466, // 51~60
2377, 2293, 2211, 2133, 2058, 1986, 1917, 1850, 1786, 1725, // 61~70
1666, 1610, 1555, 1503, 1452, 1404, 1358, 1313, 1270, 1228, // 71~80
1189, 1150, 1113, 1078, 1044, 1011, 979, 948, 919, 890, // 81~90
863, 837, 811, 787, 763, 740, 718, 697, 676, 657, // 91~100
637, 619, 601, 584, 567, 551, 535, 520, 505, 491, // 101~110
478, 464, 451, 439, 427, 415, 404, 393, 382, 371, // 111~120
361, 351, 342, 333, 324, // 121~125
};
static uint8_t ntc_lookup(const uint32_t *list, uint16_t adc)
{
uint8_t middle = 0;
uint8_t indexL = 0;
uint8_t indexR = TABLE_SIZE - 1;
if (adc >= *(list + 0))
return 0;
if (adc <= *(list + TABLE_SIZE - 1))
return TABLE_SIZE - 1;
while ((indexR - indexL) > 1)
{
middle = (indexL + indexR) >> 1;
if (adc == *(list + middle))
return middle;
else if (adc > *(list + middle))
indexR = middle;
else if (adc < *(list + middle))
indexL = middle;
}
return indexL;
}
void ntc_init(void)
{
}
/**
* @brief 获取温度值单位为0.1摄氏度
* @param {uint16_t} adc采集值
* @return {float32_t} 温度值
* @note
*/
float32_t ntc_get_temp(uint16_t adc)
{
uint8_t index = 0;
int16_t data = 0;
int16_t t = 0;
int16_t result = 0;
uint32_t rt = 0;
const int16_t base = BASE_TEMP * 10;
float32_t res;
res.f = BASE_TEMP;
/**
* ad = (4095*rt)/(rt+10000)
* rt = (ad*100)/(4095-ad)
*/
rt = (adc * NTC_SERIES_RESISTOR) / (4095 - adc);
index = ntc_lookup(_table, rt);
if (rt >= _table[0])
return res;
if (rt <= *(_table + TABLE_SIZE - 1))
{
result = (TABLE_SIZE - 1) * 10 + base;
}
else
{
data = _table[index] - _table[index + 1];
t = 10 * (_table[index] - rt) / data;
result = base + index * 10 + t;
}
res.f = result / 10.0;
return res;
}