(1)首先要明白的是,热敏电阻,是一种温度传感器。它的电阻值随温度的升高而下降,属于负温度系数材料。
(2)ADC采集的电压是对应某一个温度
(3)了解二分法是什么
(4)上代码bsp_ntc.c
#include "bsp_ntc.h"
static const uint16_t NTC_ADValue[] =
{
3252,3249,3245,3241,3237,3233,3229,3224,3219,3214,
3209,3203,3197,3191,3184,3177,3170,3162,3154,3146,
3137,3127,3118,3108,3097,3086,3074,3062,3050,3037,
3023,3009,2994,2979,2963,2946,2929,2912,2894,2875,
2855,2835,2815,2793,2772,2749,2726,2702,2678,2653,
2627,2601,2575,2547,2520,2491,2463,2433,2403,2373,
2343,2312,2280,2248,2216,2184,2151,2118,2085,2052,
2019,1985,1951,1918,1884,1850,1816,1783,1749,1715,
1682,1649,1616,1583,1567,1559,1551,1534,1518,1486,
1454,1423,1392,1361,1331,1301,1271,1243,1214,1186,
1158,1130,1117,1110,1103,1077,1051,1026,1001,976,
952,929,905,883,861,839,832,825,818,797,
777,757,738,719,701,683,665,648,631,615,
599,584,569,554,540,526,513,499,487,474,
462,450,444,438,427,416,406,395,385,375,
366,357,348,339,331,327,322,314,306,298,
291,284,277,270,263,257,251,245,239,233,
227,222,217,212,206,201,196,191,187,183,
178,174,170,166,163,159,156,152,148,145,
142
};
static const int16_t NTC_TempValue[] =
{
-40,-39,-38,-37,-36,-35,-34,-33,-32,-31,
-30,-29,-28,-27,-26,-25,-24,-23,-22,-21,
-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,
-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,
0,1,2,3,4,5,6,7,8,9,
10,11,12,13,14,15,16,17,18,19,
20,21,22,23,24,25,26,27,28,29,
30,31,32,33,34,35,36,37,38,39,
40,41,42,43,44,45,46,47,48,49,
50,51,52,53,54,55,56,57,58,59,
60,61,62,63,64,65,66,67,68,69,
70,71,72,73,74,75,76,77,78,79,
80,81,82,83,84,85,86,87,88,89,
90,91,92,93,94,95,96,97,98,99,
100,101,102,103,104,105,106,107,108,109,
110,111,112,113,114,115,116,117,118,119,
120,121,122,123,124,125,126,127,128,129,
130,131,132,133,134,135,136,137,138,139,
140,141,142,143,144,145,146,147,148,149,
150
// -40,-39,-38,-37,-36,-35,-34,-33,-32,-31,
// -30,-29,-28,-27,-26,-25,-24,-23,-22,-21,
// -20,-19,-18,-17,-16,-15,-14,-13,-12,-11,
// -10,-9,-8,-7,-6,-5,-4,-3,-2,-1,
// 0,1,2,3,4,5,6,7,8,9,
// 10,11,12,13,14,15,16,17,18,19,
// 20,21,22,23,24,25,26,27,28,29,
// 30,31,32,33,34,35,36,37,38,39,
// 40,41,42,43,44,45,46,47,48,49,
// 50,51,52,53,54,55,56,57,58,59,
// 60,61,62,63,64,65,66,67,68,69,
// 70,71,72,73,74,75,76,77,78,79,
// 80,81,82,83,84,85,86,87,88,89,
// 90,91,92,93,94,95,96,97,98,99,
// 100,101,102,103,104,105,106,107,108,109,
// 110,111,112,113,114,115,116,117,118,119,
// 120,121,122,123,124,125
};
static uint8_t ADC_ValueGet(uint16_t data) //二分法查温度
{
uint8_t start = 0;
uint8_t end = sizeof(NTC_ADValue) / sizeof(uint16_t); // 一共多少个ADC值
uint8_t mid = 0;
if(data <= NTC_ADValue[0])
{
while(start <= end)
{
mid = (start + end ) / 2;
if(data == NTC_ADValue[mid])
break;
if((data < NTC_ADValue[mid]) && (data>NTC_ADValue[mid+1]))
break;
if(data > NTC_ADValue[mid])
end = mid-1;
else if(data < NTC_ADValue[mid])
start = mid+1;
}
}
return mid;
}
int16_t TempValueGet(uint16_t data)
{
uint8_t loc = 0;
loc = ADC_ValueGet(data);
return NTC_TempValue[loc];
}
(5)bsp_ntc.h
#ifndef __BSP_NTC_H_
#define __BSP_NTC_H_
#include "bsp.h"
static uint8_t ADC_ValueGet(uint16_t data);
int16_t TempValueGet(uint16_t data);
#endif
(6)main.c
#include "bsp.h"
int main(void)
{
while(1)
{
TempValueGet(3000);
}
}