本文主要是介绍NTC热敏电阻采集温度-单片机通用模板,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
NTC热敏电阻采集温度-单片机通用模板
- 一、NTC热敏电阻转换温度的原理
- 二、AT104Tem.c的实现
- 三、AT104Tem.h的实现
一、NTC热敏电阻转换温度的原理
- ①NTC热敏电阻会随着温度的升高,电阻值R逐渐降低;
- ②硬件搭建电阻分压电路采集ADC逆推热敏电阻当前的阻值,然后根据阻值查表即可得出温度;
二、AT104Tem.c的实现
#include "at104Tem.h"u16 code TemperTable[]={
0x022D, //0
0x0246, //1
0x0260, //2
0x027B, //3
0x0296, //4
0x02B2, //5
0x02CF, //6
0x02ED, //7
0x030B, //8
0x032A, //9
0x034A, //10
0x036A, //11
0x038B, //12
0x03AD, //13
0x03D0, //14
0x03F3, //15
0x0417, //16
0x043B, //17
0x0460, //18
0x0486, //19
0x04AC, //20
0x04D2, //21
0x04F9, //22
0x0521, //23
0x0549, //24
0x0571, //25
0x0599, //26
0x05C2, //27
0x05EB, //28
0x0615, //29
0x063E, //30
0x0668, //31
0x0692, //32
0x06BC, //33
0x06E6, //34
0x0710, //35
0x073A, //36
0x0763, //37
0x078D, //38
0x07B7, //39
0x07E0, //40
0x0809, //41
0x0832, //42
0x085B, //43
0x0884, //44
0x08AC, //45
0x08D3, //46
0x08FB, //47
0x0922, //48
0x0948, //49
0x096F, //50
0x0994, //51
0x09BA, //52
0x09DE, //53
0x0A02, //54
0x0A26, //55
0x0A49, //56
0x0A6C, //57
0x0A8E, //58
0x0AAF, //59
0x0AD0, //60
0x0AF1, //61
0x0B10, //62
0x0B2F, //63
0x0B4E, //64
0x0B6C, //65
0x0B89, //66
0x0BA6, //67
0x0BC2, //68
0x0BDD, //69
0x0BF8, //70
0x0C13, //71
0x0C2D, //72
0x0C46, //73
0x0C5E, //74
0x0C76, //75
0x0C8E, //76
0x0CA5, //77
0x0CBB, //78
0x0CD1, //79
0x0CE6, //80
0x0CFB, //81
0x0D0F, //82
0x0D23, //83
0x0D36, //84
0x0D49, //85
0x0D5B, //86
0x0D6D, //87
0x0D7F, //88
0x0D8F, //89
0x0DA0, //90
0x0DB0, //91
0x0DBF, //92
0x0DCF, //93
0x0DDD, //94
0x0DEC, //95
0x0DFA, //96
0x0E07, //97
0x0E15, //98
0x0E22, //99
0x0E2E, //100
0x0E3A, //101
0x0E46, //102
0x0E52, //103
0x0E5D, //104
0x0E68, //105
0x0E72, //1060x0182, //107
0x018C, //108
0x0197, //109
0x01A1, //110
0x01AC, //111
0x01B7, //112
0x01C2, //113
0x01CE, //114
0x01D9, //115
0x01E5, //116
0x01F1, //117
0x01FD, //118
0x020A, //119
0x0217, //120
0x0223, //121
0x0230, //122
0x023E, //123
0x024B, //124
0x0259, //125
0x0267, //126
0x0275, //127
0x0283, //128
0x0292, //129
0x02A0, //130
0x02AF, //131
0x02BE, //132
0x02CD, //133
0x02DD, //134
0x02EC, //135
0x02FC, //136
0x030C, //137
0x031D, //138
0x032D, //139
0x033E, //140
0x034E, //141
0x035F, //142
0x0370, //143
0x0382, //144
0x0393, //145
0x03A5, //146
0x03B7, //147
0x03C9, //148
0x03DB, //149
0x03ED, //150
0x03FF, //151
0x0412, //152
0x0425, //153
0x0437, //154
0x044A, //155
0x045E, //156
0x0471, //157
0x0484, //158
0x0498, //159
0x04AB, //160
0x04BF, //161
0x04D3, //162
0x04E7, //163
0x04FB, //164
0x050F, //165
0x0523, //166
0x0538, //167
0x054C, //168
0x0560, //169
0x0575, //170
0x058A, //171
0x059E, //172
0x05B3, //173
0x05C8, //174
0x05DD, //175
0x05F1, //176
0x0606, //177
0x061B, //178
0x0630, //179
0x0645, //180
0x065A, //181
0x066F, //182
0x0684, //183
0x0699, //184
0x06AE, //185
0x06C3, //186
0x06D8, //187
0x06ED, //188
0x0702, //189
0x0717, //190
0x072C, //191
0x0741, //192
0x0756, //193
0x076A, //194
0x077F, //195
0x0794, //196
0x07A9, //197
0x07BD, //198
0x07D2, //199
0x07E6, //200
0x07FB, //201
0x080F, //202
0x0823, //203
0x0837, //204
0x084B, //205
0x085F, //206
0x0873, //207
0x0887, //208
0x089B, //209
0x08AE, //210
0x08C2, //211
0x08D5, //212
0x08E8, //213
0x08FC, //214
0x090F, //215
0x0922, //216
0x0934, //217
0x0947, //218
0x095A, //219
0x096C, //220
0x097E, //221
0x0991, //222
0x09A3, //223
0x09B5, //224
0x09C7, //225
0x09D8, //226
0x09EA, //227
0x09FB, //228
0x0A0C, //229
0x0A1D, //230
0x0A2E, //231
0x0A3F, //232
0x0A50, //233
0x0A60, //234
0x0A71, //235
0x0A81, //236
0x0A91, //237
0x0AA1, //238
0x0AB1, //239
0x0AC0, //240
0x0AD0, //241
0x0AE0, //242
0x0AEF, //243
0x0AFE, //244
0x0B0D, //245
0x0B1C, //246
0x0B2A, //247
0x0B38, //248
0x0B47, //249
0x0B55, //250
0x0B63, //251
0x0B71, //252
0x0B7F, //253
0x0B8D, //254
0x0B9A, //255
0x0BA7, //256
0x0BB4, //257
0x0BC1, //258
0x0BCE, //259
0x0BDB, //260
0x0BE8, //261};TemTypedef Tem;//=============================================================================
//函数名称:CT0FloatHz
//输 入:无
//输 出:无
//功 能:CT0高阻态,CT1低电平
//=============================================================================
void CT0FloatHz(void)
{CT0Clo();CT1Open();P3MODH &= 0xC3;P3MODH |= 0x18; // P36高阻态 P35 输出低电平
}//=============================================================================
//函数名称:CT1FloatHz
//输 入:无
//输 出:无
//功 能:分压切换为680R+51k
//=============================================================================
void CT1FloatHz(void)
{CT1Clo();CT0Open();P3MODH &= 0xC3;P3MODH |= 0x24; // P35高阻态 P36 输出低电平
}//=============================================================================
//函数名称:F_TemperADCWork
//输 入:无
//输 出:无
//功 能:获取温度数据
//=============================================================================
void F_TemperADCWork(void)
{//u8 TemSumCount=4;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) //Tem.InTmp=50;{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;} //TemperatureCompensation(); //温度补偿//Tem.InFah=Tem.InTmp*1.8+32; //华氏度转换}
- ①根据阻值表和分压计算出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 AdcSum;//u16 AdcAvr;//s16 InTmp;//s16 InFah;u16 InTmp; }xdata TemTypedef;extern TemTypedef Tem;
extern void F_TemperADCWork(void);#endif
这篇关于NTC热敏电阻采集温度-单片机通用模板的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!