119 lines
4.6 KiB
C
119 lines
4.6 KiB
C
/**
|
||
* @file ntc_3950.c
|
||
* @author xxx
|
||
* @date 2023-08-30 08:58:43
|
||
* @brief 用于实现NTC的应用功能
|
||
* @copyright Copyright (c) 2023 by xxx, All Rights Reserved.
|
||
*/
|
||
|
||
#include "ntc_3950.h"
|
||
#define CPU_VREF 2.5
|
||
#define NTC_VREF 2.5
|
||
#define TABLE_SIZE 185
|
||
#define NTC_SERIES_RESISTOR 200000 ///< 200K
|
||
#define TEMP_MIN -55
|
||
#define TEMP_MAX 129
|
||
|
||
const uint32_t _table[TABLE_SIZE] = {
|
||
8989000, 8242680, 7592960, 7021380, 6513750, // -55,-54,-53,-52,-51
|
||
6059060, 5648680, 5275800, 4935020, 4621990, 4333220, 4065840, 3817520, 3586310, 3370600, // -50,-49,-48,-47,-46,-45,-44,-43,-42,-41
|
||
3169000, 2980330, 2803600, 2637910, 2482470, 2336580, 2199620, 2071020, 1950230, 1836790, // -40,-39,-38,-37,-36,-35,-34,-33,-32,-31
|
||
1730230, 1630150, 1536140, 1447840, 1364900, 1287000, 1213820, 1145090, 1080530, 1019890, // -30,-29,-28,-27,-26,-25,-24,-23,-22,-21
|
||
962912, 909379, 859074, 811797, 767359, 725581, 686296, 649348, 614590, 581883, // -20,-19,-18,-17,-16,-15,-14,-13,-12,-11
|
||
551100, 522117, 494824, 469113, 444886, 422050, 400518, 380209, 361048, 342963, // -10,-9,-8,-7,-6,-5,-4,-3,-2,-1
|
||
326560, 309764, 294529, 280131, 266520, 253647, 241470, 229946, 219036, 208706, // 0,1, 2, 3,4,5,6,7,8,9
|
||
198920, 189647, 180857, 172523, 164618, 157118, 150000, 143243, 136827, 130731, // 10,11 ,12, 13,14,15,16,17,18,19
|
||
124940, 119435, 114202, 109225, 104491, 100000, 95699, 91617, 87731, 84028, // 20,21, 22, 23,24,25,26,27,28,29
|
||
80501, 77140, 73936, 70881, 67968, 65188, 62537, 60006, 57590, 55283, // 30,31, 32, 33,34,35,36,37,38,39
|
||
53080, 50976, 48965, 47044, 45207, 43451, 41771, 40165, 38628, 37157, // 40,41, 42, 43,34,35,36,37,38,39
|
||
35750, 34402, 33112, 31876, 30692, 29558, 28471, 27429, 26430, 25472, // 50,51, 52, 53,54,55,56,57,58,59
|
||
24554, 23672, 22827, 22016, 21237, 20489, 19771, 19082, 18420, 17784, // 60,61, 62, 63,64,65,66,67,68,69
|
||
17172, 16585, 16020, 15477, 14955, 14453, 13970, 13505, 13058, 12628, // 70,71, 72, 73,74,75,76,77,78,79
|
||
12213, 11815, 11431, 11061, 10705, 10362, 10031, 9712, 9405, 9110, // 80,81, 82, 83,84,85,86,87,88,89
|
||
8824, 8549, 8284, 8028, 7782, 7544, 7314, 7093, 6879, 6673, // 90,91, 92, 93,94,95,96,97,98,99
|
||
6474, 6281, 6096, 5916, 5743, 5576, 5415, 5259, 5108, 4963, // 101,102, 103,104,105,106,107,108,109
|
||
4822, 4687, 4555, 4428, 4306, 4187, 4073, 3962, 3855, 3751, // 111,112, 113,114,115,116,117,118,119
|
||
3651, 3555, 3461, 3371, 3283, 3199, 3100, 3099, 2899, 2799, // 121,122, 123,124,125,126,127,128,129
|
||
};
|
||
|
||
/**
|
||
* @brief 温度查表
|
||
* @param {uint32_t} *list
|
||
* @param {uint32_t} rt
|
||
* @return {*}
|
||
* @note
|
||
*/
|
||
static uint8_t ntc_lookup(const uint32_t *list, uint32_t rt)
|
||
{
|
||
uint8_t middle = 0;
|
||
uint8_t indexL = 0;
|
||
uint8_t indexR = TABLE_SIZE - 1;
|
||
if (rt >= *(list + 0))
|
||
return 0;
|
||
if (rt <= *(list + TABLE_SIZE - 1))
|
||
return TABLE_SIZE - 1;
|
||
|
||
while ((indexR - indexL) > 1)
|
||
{
|
||
middle = (indexL + indexR) >> 1;
|
||
if (rt == *(list + middle))
|
||
return middle;
|
||
else if (rt > *(list + middle))
|
||
indexR = middle;
|
||
else if (rt < *(list + middle))
|
||
indexL = middle;
|
||
}
|
||
return indexL;
|
||
}
|
||
|
||
/**
|
||
* @brief 获取温度值,单位为0.1摄氏度
|
||
* @param {uint16_t} adc采集值
|
||
* @return {float32_u} 温度值
|
||
* @note
|
||
*/
|
||
float32_u 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 = TEMP_MIN * 10;
|
||
float32_u res;
|
||
res.f = TEMP_MIN;
|
||
|
||
/**
|
||
* ad = (4095*rt)/(rt+10000)
|
||
*
|
||
* rt = (ad*NTC_SERIES_RESISTOR*CPU_VREF)/(NTC_VREF*4095-CPU_VREF*ad)
|
||
*/
|
||
rt = (adc * NTC_SERIES_RESISTOR * CPU_VREF) / (NTC_VREF * 4095 - CPU_VREF * 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;
|
||
}
|
||
|
||
// 判断传感器是否故障
|
||
BOOL ntc_is_fault_temp(float32 temp)
|
||
{
|
||
if (temp < TEMP_MIN || temp > TEMP_MAX)
|
||
{
|
||
return TRUE;
|
||
}
|
||
return FALSE;
|
||
}
|