一、原理
其实电池内部有个热敏电阻, 与外部分压电阻构成一个简单的分压电路, 根据ADC采样得到的电压j计算热敏阻值再反推此时的温度, 首先我们要先了解热敏电阻阻值和温度一个公式:
/* NTC热敏电阻公式 Rt = R * exp(B*(1/T1-1/T2))Rt:在T1下的电阻值T1/T2: 指的是K度,即开尔文温度, T=273.15 + 摄氏度R: 在T2下的标称阻值,比如在25摄氏度10000欧, T2=273.15 + 25B:热敏电阻一个参数, 比如3950exp:e的n次方我们可以通过温度得到阻值, 也可以通过阻值得到温度Rt = 10000 * exp(3950*(1/(273.15+t1) - 1/298.15))t1=1/(ln(Rt/10000)/3950+1/298.15)-273.15*/
我的外部电路如下:
J2是电池插座, 三根线, 中间的就是NTC, BAT_DET连接处理器的ADC采样引脚, 分压电阻阻值47k, 参考电压1.8v, 所以:
Rt = Vadc * Rf / (Vin-Vadc)Vadc: 热敏电阻对应的电压值Rf: 分压电阻 对应电路47kVin: 参考电压 1.8v
至于Vadc采样电压就是各个处理器自己的事了, 只要确保采集的电压是准确的, 可以和万用表对比
二、示例代码
下面就演示温度从零下-10度到60度 热敏电阻阻值以及根据采样电压反推热敏电阻的温度, 需要注意的是,代码需要包含math.h函数库的支持, 同时, 数学ln()的叫法在C库是log()
/* gcc test.c -lm */ #include#include int main(){ int t1; double Rt, Vadc; /* 温度从零下-10度到60度 热敏电阻阻值 */ for(t1=-10; t1<60; t1++) { Rt = 10000 * exp(3950*(1/(273.15+t1) - 1/298.15)); Vadc = 1800*Rt/(Rt + 47000); printf("%d = %.10fmv\n", t1, Vadc); } /* 根据采样电压反推热敏电阻的温度 */ for(Vadc=100; Vadc<1000; Vadc+=100) { Rt = Vadc * 47000 / (1800-Vadc); t1=1/(ln(Rt/10000)/3950+1/298.15)-273.15; printf("%.10fmv = %d\n", Vadc, t1); } return 0;}
对应的log:
-10 = 996.1667819082mv-9 = 970.8171351943mv-8 = 945.5463090268mv-7 = 920.3938859522mv-6 = 895.3981112286mv-5 = 870.5956912898mv-4 = 846.0216150692mv-3 = 821.7089994324mv-2 = 797.6889594676mv-1 = 773.9905039042mv0 = 750.6404554807mv1 = 727.6633956682mv2 = 705.0816327966mv3 = 682.9151923094mv4 = 661.1818276246mv5 = 639.8970498688mv6 = 619.0741746119mv7 = 598.7243836333mv8 = 578.8567997063mv9 = 559.4785723909mv10 = 540.5949728601mv11 = 522.2094958620mv12 = 504.3239670152mv13 = 486.9386537600mv14 = 470.0523784230mv15 = 453.6626319968mv16 = 437.7656873959mv17 = 422.3567110965mv18 = 407.4298722257mv19 = 392.9784483112mv20 = 378.9949270420mv21 = 365.4711035231mv22 = 352.3981726282mv23 = 339.7668161643mv24 = 327.5672846610mv25 = 315.7894736842mv26 = 304.4229946520mv27 = 293.4572401925mv28 = 282.8814441428mv29 = 272.6847363289mv30 = 262.8561923090mv31 = 253.3848782846mv32 = 244.2598914134mv33 = 235.4703957662mv34 = 227.0056541849mv35 = 218.8550563020mv36 = 211.0081429835mv37 = 203.4546274551mv38 = 196.1844133667mv39 = 189.1876100427mv40 = 182.4545451581mv41 = 175.9757750675mv42 = 169.7420930061mv43 = 163.7445353679mv44 = 157.9743862542mv45 = 152.4231804741mv46 = 147.0827051655mv47 = 141.9450001930mv48 = 137.0023574679mv49 = 132.2473193230mv50 = 127.6726760645mv51 = 123.2714628124mv52 = 119.0369557321mv53 = 114.9626677478mv54 = 111.0423438230mv55 = 107.2699558814mv56 = 103.6396974382mv57 = 100.1459779998mv58 = 96.7834172883mv59 = 93.5468393367mv100.0000000000mv = 57200.0000000000mv = 37300.0000000000mv = 26400.0000000000mv = 18500.0000000000mv = 12600.0000000000mv = 6700.0000000000mv = 2800.0000000000mv = -2900.0000000000mv = -6
三、其他
a. 数学中对数用log表示,ln表示以e为底数, C库使用log()却表示数学的ln, 如果要表示数学的logab, 由于等效数学的lnb/lna, 即等效C代码log(b)/log(a)
b. ADC采样精度是个问题, 同时最好多次采样取平均值
c. 由于我是要在驱动实现这个功能, 内核没有包含这个math.h和libgcc.a库, 所以参考网上实现了个差不多精度函数:
double ln(double a){ int N = 15; int k,nk; double x,xx,y; x = (a-1)/(a+1); xx = x*x; nk = 2*N+1; y = 1.0/nk; for(k=N;k>0;k--) { nk = nk - 2; y = 1.0/nk+xx*y; } return 2.0*x*y;} /* https://blog.csdn.net/mike190267481/article/details/7404702 */
对比log如下:
printf("%.10f, %.10f\n", ln(0.1), log(0.1)); printf("%.10f, %.10f\n", ln(1), log(1)); printf("%.10f, %.10f\n", ln(5), log(5)); printf("%.10f, %.10f\n", ln(10), log(10)); printf("%.10f, %.10f\n", ln(15), log(15)); printf("%.10f, %.10f\n", ln(20), log(20));-2.3023645999, -2.30258509300.0000000000, 0.00000000001.6094377510, 1.60943791242.3023645999, 2.30258509302.7053425934, 2.70805020112.9856609824, 2.9957322736
尴尬的是内核不支持浮点运算, 所以这个在应用程序调试OK的ln()函数作废, 只能先在应用程序打出各个采样电压对应的温度, 做成一个表格, 然后驱动再根据电压查询表格得到对应温度了
至于为何内核不能进行浮点运算, 这是另一个问题了, 可以参考我另一篇博文: