最近在调试STM32ADC采集NTC热敏电阻的温度值,总结下NTC热敏电阻温度值的采集方法。
硬件平台:STM32F205
软件平台:keil V5
函数库:标准库
热敏电阻的相关知识点见百度文库的介绍:传送门。
本例中使用的热敏电阻型号为mfh103-3950。其电阻与温度对应表如下:
附表1
NTC热敏电阻R/T对照表
T(℃) R(KΩ) T(℃) R(KΩ) T(℃) R(KΩ)
-20.0 95.3370 20.5 12.2138 61.0 2.3820
-19.5 92.6559 21.0 11.9425 61.5 2.3394
-19.0 90.0580 21.5 11.6778 62.0 2.2977
-18.5 87.5406 22.0 11.4198 62.5 2.2568
-18.0 85.1009 22.5 11.1681 63.0 2.2167
-17.5 82.7364 23.0 10.9227 63.5 2.1775
-17.0 80.4445 23.5 10.6834 64.0 2.1390
-16.5 78.2227 24.0 10.4499 64.5 2.1013
-16.0 76.0689 24.5 10.2222 65.0 2.0644
-15.5 73.9806 25.0 10.0000 65.5 2.0282
-15.0 71.9558 25.5 9.7833 66.0 1.9928
-14.5 69.9923 26.0 9.5718 66.5 1.9580
-14.0 68.0881 26.5 9.3655 67.0 1.9240
-13.5 66.2412 27.0 9.1642 67.5 1.8906
-13.0 64.4499 27.5 8.9677 68.0 1.8579
-12.5 62.7122 28.0 8.7760 68.5 1.8258
-12.0 61.0264 28.5 8.5889 69.0 1.7944
-11.5 59.3908 29.0 8.4063 69.5 1.7636
-11.0 57.8038 29.5 8.2281 70.0 1.7334
-10.5 56.2639 30.0 8.0541 70.5 1.7037
-10.0 54.7694 30.5 7.8842 71.0 1.6747
-9.5 53.3189 31.0 7.7184 71.5 1.6462
-9.0 51.9111 31.5 7.5565 72.0 1.6183
-8.5 50.5445 32.0 7.3985 72.5 1.5910
-8.0 49.2178 32.5 7.2442 73.0 1.5641
-7.5 47.9298 33.0 7.0935 73.5 1.5378
-7.0 46.6792 33.5 6.9463 74.0 1.5120
-6.5 45.4649 34.0 6.8026 74.5 1.4867
-6.0 44.2856 34.5 6.6622 75.0 1.4619
-5.5 43.1403 35.0 6.5251 75.5 1.4375
-5.0 42.0279 35.5 6.3912 76.0 1.4136
-4.5 40.9474 36.0 6.2604 76.5 1.3902
-4.0 39.8978 36.5 6.1326 77.0 1.3672
-3.5 38.8780 37.0 6.0077 77.5 1.3447
-3.0 37.8873 37.5 5.8858 78.0 1.3225
-2.5 36.9246 38.0 5.7666 78.5 1.3008
-2.0 35.9892 38.5 5.6501 79.0 1.2795
-1.5 35.0801 39.0 5.5363 79.5 1.2586
-1.0 34.1965 39.5 5.4251 80.0 1.2381
-0.5 33.3378 40.0 5.3164 80.5 1.2180
0.0 32.5030 40.5 5.2102 81.0 1.1983
0.5 31.6915 41.0 5.1064 81.5 1.1789
1.0 30.9026 41.5 5.0049 82.0 1.1599
1.5 30.1355 42.0 4.9057 82.5 1.1412
2.0 29.3896 42.5 4.8088 83.0 1.1229
2.5 28.6644 43.0 4.7140 83.5 1.1050
3.0 27.9590 43.5 4.6213 84.0 1.0873
3.5 27.2730 44.0 4.5307 84.5 1.0700
4.0 26.6058 44.5 4.4421 85.0 1.0530
4.5 25.9567 45.0 4.3554 85.5 1.0363
5.0 25.3254 45.5 4.2707 86.0 1.0199
5.5 24.7111 46.0 4.1878 86.5 1.0038
6.0 24.1135 46.5 4.1068 87.0 0.9880
6.5 23.5320 47.0 4.0275 87.5 0.9725
7.0 22.9661 47.5 3.9500 88.0 0.9573
7.5 22.4154 48.0 3.8742 88.5 0.9424
8.0 21.8795 48.5 3.8000 89.0 0.9277
8.5 21.3579 49.0 3.7275 89.5 0.9133
9.0 20.8502 49.5 3.6565 90.0 0.8991
9.5 20.3559 50.0 3.5870 90.5 0.8852
10.0 19.8747 50.5 3.5190 91.0 0.8715
10.5 19.4063 51.0 3.4525 91.5 0.8581
11.0 18.9502 51.5 3.3875 92.0 0.8450
11.5 18.5060 52.0 3.3238 92.5 0.8320
12.0 18.0735 52.5 3.2615 93.0 0.8193
12.5 17.6523 53.0 3.2005 93.5 0.8068
13.0 17.2421 53.5 3.1408 94.0 0.7945
13.5 16.8426 54.0 3.0824 94.5 0.7825
14.0 16.4534 54.5 3.0252 95.0 0.7707
14.5 16.0743 55.0 2.9692 95.5 0.7590
15.0 15.7049 55.5 2.9144 96.0 0.7476
15.5 15.3450 56.0 2.8608 96.5 0.7364
16.0 14.9944 56.5 2.8082 97.0 0.7253
16.5 14.6528 57.0 2.7568 97.5 0.7145
17.0 14.3198 57.5 2.7065 98.0 0.7038
17.5 13.9954 58.0 2.6572 98.5 0.6933
18.0 13.6792 58.5 2.6089 99.0 0.6831
18.5 13.3710 59.0 2.5616 99.5 0.6729
19.0 13.0705 59.5 2.5153 100.0 0.6630
19.5 12.7777 60.0 2.4700
20.0 12.4922 60.5 2.4255
热敏电阻的采样电路图如下图:
从上图电路原理可知,温度上升,NTC阻值下降,对应的ADC采样电压下降,j进而AD采样值下降。即每对应一个温度值,就对应一个ad采样值,这样便可得到一个温度与ad值的对应表。
那么NTC采集温度的原理就是利用采样的ad值与对应表进行比较,得到温度值。温度与ad值的对应表可利用已经做好的表格来自动转化(输入参数,即可得到对应的ad值),NTC计算表下载地址见:传送门。
本例中VCC是3.3V,电路中ADC参考电压是3V,所以转换表格中乘以1.1进行换算,若输入电压和ADC采样的参考电压都是3.3V,删去1.1即可。
利用上面的NTC计算表,可得到如下对应表。
//NTC的ad值对应表
static u16 NTCTAB[241]=
{
0xFED ,0xFE2 ,0xFD6 ,0xFCB ,0xFBF ,0xFB3 ,0xFA6 ,0xF9A ,0xF8D ,0xF80,
0xF73 ,0xF65 ,0xF58 ,0xF4A ,0xF3B ,0xF2D ,0xF1E ,0xF0F ,0xF00 ,0xEF1,
0xEE1 ,0xED1 ,0xEC1 ,0xEB1 ,0xEA0 ,0xE8F ,0xE7E ,0xE6C ,0xE5B ,0xE49,
0xE37 ,0xE24 ,0xE12 ,0xDFF ,0xDEC ,0xDD9 ,0xDC5 ,0xDB1 ,0xD9D ,0xD89,
0xD75 ,0xD60 ,0xD4B ,0xD36 ,0xD21 ,0xD0B ,0xCF6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 ,0x1E9 ,0x1E2 ,0x1DB ,0x1D4 ,0x1CD ,0x1C7 ,0x1C0 ,0x1BA ,0x1B3,
0x1AD ,0x1A7 ,0x1A1 ,0x19B ,0x195 ,0x18F ,0x18A ,0x184 ,0x17E ,0x179,
0x174 ,0x16E ,0x169 ,0x164 ,0x15F ,0x15A ,0x155 ,0x150 ,0x14C ,0x147,
0x142 ,0x13E ,0x139 ,0x135 ,0x131 ,0x12C ,0x128 ,0x124 ,0x120 ,0x11C,
0x118
};
接下来,编写查表函数,当采集到ad值后,从这个表中查到最接近的ad值。
//查表函数
u8 look_up_table(u16 *a,u8 ArrayLong,u16 data)
{
u16 begin,end,middle ;
u8 i ;
begin = 0 ;
end = ArrayLong-1 ;
i = 0 ;
if(data >= a[begin]) return begin ;
else if(data <= a[end]) return end ;
while(begin < end)
{
middle = (begin+end)/2 ;
if(data == a[middle] ) break ;
if(data < a[middle] && data > a[middle+1]) break ;
if(data > a[middle]) end = middle ;
else begin = middle ;
if(i++ > ArrayLong) break ;
}
if(begin > end ) return 0 ;
return middle ;
}
上述函数中参数u16 *a是表头,即表的首地址,u8 ArrayLong是表的元素的个数,u16 data是要查的ad值,函数返回值是查到的ad值在表中的序号值。得到对应的序号值,就可得到对应的温度值。
//输入表的序号值,得到温度值
float num_to_temperature(u8 num)
{
float data;
data = 0.5*num-20;
return data;
}
上述是查到与采样AD最近的ad值,求出温度值。这个温度值精度不高,可作为初略计算的温度。
下面也可利用得到的这个粗略的温度值t1,结合表中的下一个节点t2,进行线性计算,求的更精确的温度值tx。
简单的计算公式如下:
if( (data<=NTCTAB[0]) && (data>NTCTAB[240]) )
{
num=look_up_table(NTCTAB,241,data);
t1=num_to_temperature(num);
tx=0.5*(data-NTCTAB[num])/(NTCTAB[num+1]-NTCTAB[num])+t1;
printf("temp_ntc:%4.2f\r\n",tx);
}
ADC的配置采集部分见另外两篇文章:传送门1和传送门2。根据获得的ad采样值和上述查表计算方法,便可得到NTC的温度值。