熱敏電阻3950的測溫單片機里程序
code uint VOL[]和code uint Temper[]中的數(shù)據(jù)是怎么計算查表得到的。
result=(uint)((res/2048.0-1.0)*500.0);
temp1=abs(result-VOL[0]);
for(i=1;i<150;i++)
{
temp2=abs(result-VOL);
if(temp1>=temp2)
{
temp1=temp2;
flag=i;
}
這一段程序是什么意思呢?
:#include <reg52.h>
#include <intrins.h>
#include <math.h>
typedef unsigned char uchar;
typedef unsigned int uint;
code uint VOL[]={ 343, 339, 339, 335, 332, 332, 328, 324, 320, 320,
316, 312, 312, 308, 304, 300, 300, 296, 292, 292,
289, 285, 285, 281, 277, 273, 273, 269, 265, 265,
261, 257, 257, 253, 250, 250, 246, 242, 242, 238,
234, 234, 230, 230, 226, 222, 222, 218, 218, 214,
210, 210, 207, 207, 203, 199, 199, 195, 195, 191,
191, 187, 187, 183, 179, 179, 175, 175, 171, 171,
167, 167, 164, 164, 160, 160, 160, 156, 156, 152,
152, 148, 148, 144, 144, 140, 140, 140, 136, 136,
132, 132, 132, 128, 128, 125, 125, 125, 121, 121,
121, 117, 113, 109, 109, 105, 101, 101, 97, 97,
93, 89, 89, 85, 85, 82, 82, 78, 78, 74,
74, 70, 70, 66, 66, 66, 62, 62, 58, 58,
58, 54, 54, 54, 50, 50, 50, 46, 46, 46,
42, 42, 42, 42, 39, 39, 39, 39, 35, 35};
code uint Temper[]={ 100, 150, 200, 250, 300, 350, 400, 450, 500, 550,
600, 650, 700, 750, 800, 850, 900, 950, 1000, 1050,
1100, 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500, 1550
1600, 1650, 1700, 1750, 1800, 1850, 1900, 1950, 2000, 2050,
2100, 2150, 2200, 2250, 2300, 2350, 2400, 2450, 2500, 2550,
2600, 2650, 2700, 2750, 2800, 2850, 2900, 2950, 3000, 3050,
3100, 3150, 3200, 3250, 3300, 3350, 3400, 3450, 3500, 3550,
3600, 3650, 3700, 3750, 3800, 3850, 3900, 3950, 4000, 4050,
4100, 4150, 4200, 4250, 4300, 4350, 4400, 4450, 4500, 4550,
4600, 4650, 4700, 4750, 4800, 4850, 4900, 4950, 5000, 5050,
5100, 5200, 5300, 5400, 5500, 5600, 5700, 5800, 5900, 6000,
6100, 6200, 6300, 6400, 6500, 6600, 6700, 6800, 6900, 7000,
7100, 7200, 7300, 7400, 7500, 7600, 7700, 7800, 7900, 8000,
8100, 8200, 8300, 8400, 8500, 8600, 8700, 8800, 8900, 9000,
9100, 9200, 9300, 9400, 9500, 9600, 9700, 9800, 9900, 10000};
sbit STS=P1^0;
sbit CE = P1^1;
sbit CS=P1^2;
sbit A0=P1^3;
sbit RC=P1^4;
sbit RS=P1^5 ;
sbit RW=P1^6 ;
sbit EN=P1^7 ;
void delay_ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uint AD1674_Read(void)
{
uint temp;
uchar temp1,temp2;
CS=1; //片選信號
CE=0; //初始化,關(guān)閉數(shù)據(jù)采集
CS=0;
A0=0;
RC=0;
CE=1;//CE=1,CS=0,RC=0,A0=0啟動12位溫度轉(zhuǎn)換
_nop_();
while(STS==1); //等待數(shù)據(jù)采集結(jié)束
CE=0; //芯片使能關(guān)閉
RC=1;
A0=0;
CE=1;//CE=1,CS=0,RC=1,12/8=1,A0=0 允許高八位數(shù)據(jù)并行輸出
_nop_();
temp1=P0; //讀取轉(zhuǎn)換結(jié)果的高八位
CE=0; //芯片使能關(guān)閉
RC=1;
A0=1;
CE=1;//CE=1,CS=0,RC=1,12/8=0,A0=1 允許低四位數(shù)據(jù) 并行輸出
_nop_();
temp2=P0; //讀取轉(zhuǎn)換結(jié)果的第四位
temp=((temp1<<4)|(temp2&0X0F)); //高位和低位合成實際溫度,temp2為PO口的高四位
return (temp); //還回轉(zhuǎn)換結(jié)果,右移四位是因為temp2為P0口的高四位
}
/*** 寫數(shù)據(jù)***/
void w_dat(unsigned char dat)
{
RS = 1;
//EN = 0;
P2 = dat;
delay_ms(5);
RW = 0;
EN = 1;
EN = 0;
}
/*** 寫命令***/
void w_cmd(unsigned char cmd)
{
RS = 0;
// EN = 0;
P2 = cmd;
delay_ms(5);
RW = 0;
EN = 1;
EN = 0;
}
/*** 發(fā)送字符串到LCD***/
void w_string(unsigned char addr_start, unsigned char *p)
{
unsigned char *pp;
pp = p;
w_cmd(addr_start);
while (*pp != '\0')
{
w_dat(*pp++);
}
}
/*** 初始化1602****/
void Init_LCD1602(void)
{
EN = 0;
w_cmd(0x38); // 16*2顯示,5*7點陣,8位數(shù)據(jù)接口
w_cmd(0x0C); // 顯示器開、光標(biāo)開、光標(biāo)允許閃爍
w_cmd(0x06); // 文字不動,光標(biāo)自動右移
w_cmd(0x01); // 清屏
}
void process(uint date,uchar add)
{
uchar A[7];
A[0]=date/1000%10+'0';
A[1]=date/100%10+'0';
A[2]='.';
A[3]=date/10%10+'0';
A[4]=date%10+'0';
A[5]='C';
w_string(add,A);
}
void main()
{
uchar i,flag=0;
uint result,temp1,temp2;
float res;
Init_LCD1602();
w_string(0x80,"Temper:");
while (1)
{
res=(float)(AD1674_Read());
result=(uint)((res/2048.0-1.0)*500.0);
temp1=abs(result-VOL[0]);
for(i=1;i<150;i++)
{
temp2=abs(result-VOL);
if(temp1>=temp2)
{
temp1=temp2;
flag=i;
}
}
process(Temper[flag],0x80+7);
} |