【简介】
在很多应用场景需要采集当前的温度信息,热敏电阻是很常用的一种方式,我们本次实验使用TI 的TMP 6131DECT 热敏电电阻来采集温度信息。本地饰演的环境原理图如下,在不同温度下RTI的电阻值是不同的从而通过adc 采样到的电压信息也是不同的。
【查表法】
从上述原理图可以看出根据ADC采集到的电压值,就可以计算出当前热敏电阻的电阻值,根据电阻值既可以获取到温度信息,TI 的热敏电阻官方资料已经提热敏电阻的电阻值对应的温度信息我们可以简单的使用查表法完成温度信息的采集。一下是部分的温度与电阻值的关系。
根据上述的原理图我们很容易计算出adc 和电阻值的关系,本地使用的adc 的参考电压为3.3v,对应的adc 精度为12bit,adc 最大值为4096,对应的转换公式如下
resistor = (uint16_t)((10000.0f)/(4096.0f/(float)adc - 1.0f));
我们根据上述的温度和电阻值的关系使用查表法既可以获取到当前温度信息。
#include <stdint.h> #define LOOKUP_TABLE_OFFSET 40 static const uint16_t lookup_table[166] = { 6543, 6586, 6630, 6673, 6717, 6761, 6806, 6851, 6896, 6941, 6987, 7033, 7079, 7126, 7172, 7220, 7267, 7315, 7363, 7411, 7460, 7509, 7558, 7607, 7657, 7707, 7758, 7808, 7859, 7911, 7962, 8014, 8066, 8119, 8172, 8225, 8278, 8332, 8386, 8440, 8495, 8550, 8605, 8660, 8716, 8772, 8829, 8885, 8942, 9000, 9057, 9115, 9174, 9232, 9291, 9350, 9410, 9469, 9529, 9590, 9651, 9712, 9773, 9835, 9897, 9959, 10021, 10084, 10147, 10211, 10275, 10339, 10403, 10468, 10533, 10599, 10664, 10731, 10797, 10864, 10931, 10998, 11066, 11134, 11202, 11270, 11339, 11409, 11478, 11548, 11618, 11689, 11760, 11831, 11903, 11975, 12047, 12119, 12192, 12265, 12339, 12413, 12487, 12562, 12637, 12712, 12787, 12863, 12940, 13016, 13093, 13170, 13248, 13326, 13404, 13483, 13562, 13641, 13721, 13801, 13881, 13962, 14043, 14124, 14206, 14288, 14371, 14454, 14537, 14620, 14704, 14789, 14873, 14958, 15043, 15129, 15215, 15302, 15389, 15476, 15563, 15651, 15739, 15828, 15917, 16006, 16096, 16186, 16277, 16368, 16459, 16551, 16643, 16735, 16828, 16921, 17014, 17108, 17203, 17297, 17392, 17488, 17584, 17680, 17777, 17874 }; int16_t tmp6131decr_temp_lookup(uint16_t resistor) { int i = 0; for(;i < (sizeof(lookup_table)/sizeof(lookup_table[0]));i++) { if(resistor>= lookup_table[i] && resistor < lookup_table[i+1]) break; } return i - LOOKUP_TABLE_OFFSET; }
添加如下测试代码读取温度信息
void ADC12_0_INST_IRQHandler(void) { uint16_t adc,resistor; switch (DL_ADC12_getPendingInterrupt(ADC12_0_INST)) { case DL_ADC12_IIDX_MEM0_RESULT_LOADED: adc = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_0); resistor = (uint16_t)((10000.0f)/(4096.0f/(float)adc - 1.0f)); printf("tmp %d \r\n",tmp6131decr_temp_lookup(resistor)); break; default: break; } } unsigned int tmp(char argc,char ** argv) { DL_ADC12_enableConversions(ADC12_0_INST); DL_ADC12_startConversion(ADC12_0_INST); return 0; }
运行后通过吹风机加热热敏电阻发现温度已经按照预期的采集到。
【公式计算】
除了上述的查表方式外,我们还可以参考官方的计算公式的方法来计算温度值,使用说说明如下:
将上述公式添加到工程中
#include <math.h> float VBias = 3.30 ; // set the VBIAS voltage unsigned int ADC_BITS = 4096 ; // set the number of bits based on you ADC (2^# of ADC Bit Value) float VTEMP = 0; // set up the variable for the measured voltage float THRM_TEMP = 0; // setup the variable for the calculated temperature float Thermistor(int raw_ADC) // send the ADC bit value to the calculation function { // THRM calculations - 4th order polynomial regression VTEMP = 0; // reset these variables to zero in order to recalculate the new factors int THRM_ADC = raw_ADC; float THRM_A0 = -4.232811E+02 ; float THRM_A1 = 4.728797E+02 ; float THRM_A2 = -1.988841E+02 ; float THRM_A3 = 4.869521E+01 ; float THRM_A4 = -1.158754E+00 ; VTEMP = (VBias/ADC_BITS) * THRM_ADC; // calculate volts per bit then multiply that times the ADV value THRM_TEMP = (THRM_A4 * powf( VTEMP,4)) + (THRM_A3 * powf( VTEMP,3)) + (THRM_A2 * powf( VTEMP,2)) + (THRM_A1 * VTEMP) + THRM_A0; // 4th order regression to get temperature return THRM_TEMP; }
添加如下测试代码对比通过公式计算和查表打印的温度信息
void ADC12_0_INST_IRQHandler(void) { uint16_t adc,resistor; switch (DL_ADC12_getPendingInterrupt(ADC12_0_INST)) { case DL_ADC12_IIDX_MEM0_RESULT_LOADED: adc = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_0); resistor = (uint16_t)((10000.0f)/(4096.0f/(float)adc - 1.0f)); printf("lookup table tmp %d \r\n",tmp6131decr_temp_lookup(resistor)); printf("calc tmp %f \r\n",Thermistor(adc)); break; default: break; } } unsigned int tmp(char argc,char ** argv) { DL_ADC12_enableConversions(ADC12_0_INST); DL_ADC12_startConversion(ADC12_0_INST); return 0; }
产看结果公式计算的精度相对查表会高一些,我们可以根据实际需求来选择查表或者公式计算的方法。