NTC热敏电阻采集温度-单片机通用模板
- 一、NTC热敏电阻转换温度的原理
- 二、AT104Tem.c的实现
- 三、AT104Tem.h的实现
一、NTC热敏电阻转换温度的原理
- ①NTC热敏电阻会随着温度的升高,电阻值R逐渐降低;
- ②硬件搭建电阻分压电路采集ADC逆推热敏电阻当前的阻值,然后根据阻值查表即可得出温度;
二、AT104Tem.c的实现
#include "at104Tem.h"u16 code TemperTable[]={
0x022D,
0x0246,
0x0260,
0x027B,
0x0296,
0x02B2,
0x02CF,
0x02ED,
0x030B,
0x032A,
0x034A,
0x036A,
0x038B,
0x03AD,
0x03D0,
0x03F3,
0x0417,
0x043B,
0x0460,
0x0486,
0x04AC,
0x04D2,
0x04F9,
0x0521,
0x0549,
0x0571,
0x0599,
0x05C2,
0x05EB,
0x0615,
0x063E,
0x0668,
0x0692,
0x06BC,
0x06E6,
0x0710,
0x073A,
0x0763,
0x078D,
0x07B7,
0x07E0,
0x0809,
0x0832,
0x085B,
0x0884,
0x08AC,
0x08D3,
0x08FB,
0x0922,
0x0948,
0x096F,
0x0994,
0x09BA,
0x09DE,
0x0A02,
0x0A26,
0x0A49,
0x0A6C,
0x0A8E,
0x0AAF,
0x0AD0,
0x0AF1,
0x0B10,
0x0B2F,
0x0B4E,
0x0B6C,
0x0B89,
0x0BA6,
0x0BC2,
0x0BDD,
0x0BF8,
0x0C13,
0x0C2D,
0x0C46,
0x0C5E,
0x0C76,
0x0C8E,
0x0CA5,
0x0CBB,
0x0CD1,
0x0CE6,
0x0CFB,
0x0D0F,
0x0D23,
0x0D36,
0x0D49,
0x0D5B,
0x0D6D,
0x0D7F,
0x0D8F,
0x0DA0,
0x0DB0,
0x0DBF,
0x0DCF,
0x0DDD,
0x0DEC,
0x0DFA,
0x0E07,
0x0E15,
0x0E22,
0x0E2E,
0x0E3A,
0x0E46,
0x0E52,
0x0E5D,
0x0E68,
0x0E72, 0x0182,
0x018C,
0x0197,
0x01A1,
0x01AC,
0x01B7,
0x01C2,
0x01CE,
0x01D9,
0x01E5,
0x01F1,
0x01FD,
0x020A,
0x0217,
0x0223,
0x0230,
0x023E,
0x024B,
0x0259,
0x0267,
0x0275,
0x0283,
0x0292,
0x02A0,
0x02AF,
0x02BE,
0x02CD,
0x02DD,
0x02EC,
0x02FC,
0x030C,
0x031D,
0x032D,
0x033E,
0x034E,
0x035F,
0x0370,
0x0382,
0x0393,
0x03A5,
0x03B7,
0x03C9,
0x03DB,
0x03ED,
0x03FF,
0x0412,
0x0425,
0x0437,
0x044A,
0x045E,
0x0471,
0x0484,
0x0498,
0x04AB,
0x04BF,
0x04D3,
0x04E7,
0x04FB,
0x050F,
0x0523,
0x0538,
0x054C,
0x0560,
0x0575,
0x058A,
0x059E,
0x05B3,
0x05C8,
0x05DD,
0x05F1,
0x0606,
0x061B,
0x0630,
0x0645,
0x065A,
0x066F,
0x0684,
0x0699,
0x06AE,
0x06C3,
0x06D8,
0x06ED,
0x0702,
0x0717,
0x072C,
0x0741,
0x0756,
0x076A,
0x077F,
0x0794,
0x07A9,
0x07BD,
0x07D2,
0x07E6,
0x07FB,
0x080F,
0x0823,
0x0837,
0x084B,
0x085F,
0x0873,
0x0887,
0x089B,
0x08AE,
0x08C2,
0x08D5,
0x08E8,
0x08FC,
0x090F,
0x0922,
0x0934,
0x0947,
0x095A,
0x096C,
0x097E,
0x0991,
0x09A3,
0x09B5,
0x09C7,
0x09D8,
0x09EA,
0x09FB,
0x0A0C,
0x0A1D,
0x0A2E,
0x0A3F,
0x0A50,
0x0A60,
0x0A71,
0x0A81,
0x0A91,
0x0AA1,
0x0AB1,
0x0AC0,
0x0AD0,
0x0AE0,
0x0AEF,
0x0AFE,
0x0B0D,
0x0B1C,
0x0B2A,
0x0B38,
0x0B47,
0x0B55,
0x0B63,
0x0B71,
0x0B7F,
0x0B8D,
0x0B9A,
0x0BA7,
0x0BB4,
0x0BC1,
0x0BCE,
0x0BDB,
0x0BE8, };TemTypedef Tem;
void CT0FloatHz(void)
{CT0Clo();CT1Open();P3MODH &= 0xC3;P3MODH |= 0x18;
}
void CT1FloatHz(void)
{CT1Clo();CT0Open();P3MODH &= 0xC3;P3MODH |= 0x24;
}
void F_TemperADCWork(void)
{u16 i=0;u16 AdcSum=0,AdcAvr=0;CT1FloatHz();nopDelay(2500);AdcAvr = getChannelVal(AD11);CT0Clo();Tem.InTmp = 0;for(i=0;i<=107;i++){ if(AdcAvr>=TemperTable[i]) Tem.InTmp++;else break;}if(Tem.InTmp>=107) {CT0FloatHz();AdcSum=0;nopDelay(2500);AdcAvr = getChannelVal(AD11);CT1Clo();Tem.InTmp = 107;for(i=107;i<261;i++){ if(AdcAvr>=TemperTable[i]) Tem.InTmp++;else break;}if(Tem.InTmp>=260) Tem.InTmp=260;} }
- ①根据阻值表和分压计算出ADC_AT引脚的电压,再根据参考电阻逆推出ADC数值;
- ②测量温度0 ~ 260℃,阻值跨度较大,可通过修改分压电阻的从而提高温度的精度。CT0低电平,CT1高阻态测量0 ~ 106℃范围内的温度,如超出范围控制CT0高阻态,CT1低电平测量107~261℃的温度(切换引脚注意延时给C1电容充电);
- ③非温度测量周期CT0、CT1设置高阻态进行低功耗处理;
三、AT104Tem.h的实现
#ifndef __AT104TEM_H__
#define __AT104TEM_H__#include "board.h"sbit CT0IO = P3^6;
sbit CT1IO = P3^5;#define CT0Open() (CT0IO = 0)
#define CT0Clo() (CT0IO = 1)
#define CT1Open() (CT1IO = 0)
#define CT1Clo() (CT1IO = 1)typedef struct
{u16 InTmp; }xdata TemTypedef;extern TemTypedef Tem;
extern void F_TemperADCWork(void);#endif