#include<reg52.h>
#include<math.h>
#define uint unsigned int
#define uchar unsigned char
#define lcddata P0
sbit rs=P2^0;
sbit rw=P2^1;
sbit e=P2^2;
sbit DQ=P2^3;
sbit key=P3^7;
uchar temp1[]={"T : "};
uchar temp2[]={"T : "};
/*uchar temp3[9]={"T3: "};
uchar temp4[9]={"T4: "};
uchar temp5[9]={"T5: "};
uchar temp6[9]={"Ta: "};*/
uchar code ds_rom1[]={0x28,0x22,0x22,0x22,0x00,0x00,0x00,0xca};//U2ROM
uchar code ds_rom2[]={0x28,0x33,0x33,0x33,0x00,0x00,0x00,0xa0};//U3ROM
uchar code ds_rom3[]={0x28,0x44,0x44,0x44,0x00,0x00,0x00,0xAF};//U4ROM
uchar code ds_rom4[]={0x28,0x55,0x55,0x55,0x00,0x00,0x00,0xC5};//U5ROM
uchar code ds_rom5[]={0x28,0x66,0x66,0x66,0x00,0x00,0x00,0x7B};//U6ROM
int temp;//温度
bit temp_flag,value_flag;//正负标志位
int t1,t2,t3,t4,t5,num=1;
/*1602LCD函数*/
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void writelcd(uchar dat,bit x)//写操作
{
e=0;
lcddata=dat;
rs=x; //0指令,1数据
rw=0;
e=1;
delay(1);
e=0;
}
void initlcd()
{
writelcd(0x38,0); // 设置为8-bit接口,2行显示,5x8文字大小
writelcd(0x0c,0); // 设置为开显示,光标不显示,不闪烁
writelcd(0x06,0); // 设置为完成一个字符码传送后,AC自动加1,,右移位
writelcd(0x01,0); // 清显示
}
/*DS18B20*/
void delayus(uchar x)// 延时1us
{
while(--x);
}
bit init_DS18B20()//初始化
{
bit Status_DS18B20;
DQ=1;
DQ=0;
delayus(250);
DQ=1;
delayus(20);
if(!DQ)
Status_DS18B20=0;
else
Status_DS18B20=1;
delayus(250);
DQ=1;
return Status_DS18B20;
}
uchar read_DS18B20()//读数据
{
uchar i=0,dat=0;
for(i=0;i<8;i++)
{
DQ=1;
DQ=0;
dat>>=1; //八位全部都是0,向右移一位后,仍然全是0
DQ=1;
if(DQ) //如果DQ为1,readat进行或运算,如果为0,则子语句不执行,直接for循环此时采集的数据是0
dat|=0x80; //此时最高位为1,然后再进行for循环 最高位成为第七位,依次往复
DQ=1;
delayus(25);
}
return(dat);
}
void write_DS18B20(uchar dat)//写数据
{
uchar i;
for(i=0;i<8;i++)
{
DQ=1;
dat>>=1;
DQ=0;
DQ=CY;
delayus(25);
DQ=1;
}
}
void Match_rom(uchar a) //匹配ROM
{
uchar j;
write_DS18B20(0x55); //发送匹配ROM命令
if(a==1)
{
for(j=0;j<8;j++)
write_DS18B20(ds_rom1[j]); //发送18B20的序列号,先发送低字节
}
if(a==2)
{
for(j=0;j<8;j++)
write_DS18B20(ds_rom2[j]); //发送18B20的序列号,先发送低字节
}
if(a==3)
{
for(j=0;j<8;j++)
write_DS18B20(ds_rom3[j]); //发送18B20的序列号,先发送低字节
}
if(a==4)
{
for(j=0;j<8;j++)
write_DS18B20(ds_rom4[j]); //发送18B20的序列号,先发送低字节
}
if(a==5)
{
for(j=0;j<8;j++)
write_DS18B20(ds_rom5[j]); //发送18B20的序列号,先发送低字节
}
}
void gettemp(uchar z)/*读取温度值并转换*/
{
uchar a,b;
while(init_DS18B20());
if(z==1)
{
Match_rom(1); //匹配ROM 1
}
if(z==2)
{
Match_rom(2); //匹配ROM 2
}
if(z==3)
{
Match_rom(3); //匹配ROM 3
}
if(z==4)
{
Match_rom(4); //匹配ROM 4
}
if(z==5)
{
Match_rom(5); //匹配ROM 5
}
write_DS18B20(0x44);//*启动温度转换*/
while(init_DS18B20());
if(z==1)
{
Match_rom(1); //匹配ROM 1
}
if(z==2)
{
Match_rom(2); //匹配ROM 2
}
if(z==3)
{
Match_rom(3); //匹配ROM 3
}
if(z==4)
{
Match_rom(4); //匹配ROM 4
}
if(z==5)
{
Match_rom(5); //匹配ROM 5
}
write_DS18B20(0xbe);//*读取温度*/
a=read_DS18B20();
b=read_DS18B20();
temp=b;
temp<<=8;
temp=temp|a;
if(b>=8)
{
temp=~temp+1;
temp_flag=1;
}
else
{
temp_flag=0;
}
if(temp_flag==1)
temp=temp*0.625*-1;
if(temp_flag==0)
temp=temp*0.625;
if(z==1)
t1=temp;
if(z==2)
t2=temp;
if(z==3)
t3=temp;
if(z==4)
t4=temp;
if(z==5)
t5=temp;
}
void display(uchar i)//显示函数
{
char y,value;
temp=abs(temp);
if(i==1)
{
temp1[1]=i+0x30;
if(temp<100)
temp1[4]=' ';
else
temp1[4]=temp%1000/100+0x30;
temp1[5]=temp%100/10+0x30;
temp1[6]='.';
temp1[7]=temp%10+0x30;
temp1[8]=0xdf;
temp1[9]='C';
writelcd(0x80,0);
for(y=0;y<10;y++)
writelcd(temp1[y],1);
}
if(i==2)
{
temp2[1]=i+0x30;
if(temp<100)
temp2[4]=' ';
else
temp2[4]=temp%1000/100+0x30;
temp2[5]=temp%100/10+0x30;
temp2[6]='.';
temp2[7]=temp%10+0x30;
temp2[8]=0xdf;
temp2[9]='C';
writelcd(0xc0,0);
for(y=0;y<10;y++)
writelcd(temp2[y],1);
}
if(i==3)
{
temp1[1]=i+0x30;
if(temp<100)
temp1[4]=' ';
else
temp1[4]=temp%1000/100+0x30;
temp1[5]=temp%100/10+0x30;
temp1[6]='.';
temp1[7]=temp%10+0x30;
temp1[8]=0xdf;
temp1[9]='C';
writelcd(0x80,0);
for(y=0;y<10;y++)
writelcd(temp1[y],1);
}
if(i==4)
{
temp2[1]=i+0x30;
if(temp<100)
temp2[4]=' ';
else
temp2[4]=temp%1000/100+0x30;
temp2[5]=temp%100/10+0x30;
temp2[6]='.';
temp2[7]=temp%10+0x30;
temp2[8]=0xdf;
temp2[9]='C';
writelcd(0xc0,0);
for(y=0;y<10;y++)
writelcd(temp2[y],1);
}
if(i==5)
{
temp1[1]=i+0x30;
if(temp<100)
temp1[4]=' ';
else
temp1[4]=temp%1000/100+0x30;
temp1[5]=temp%100/10+0x30;
temp1[6]='.';
temp1[7]=temp%10+0x30;
temp1[8]=0xdf;
temp1[9]='C';
writelcd(0x80,0);
for(y=0;y<10;y++)
writelcd(temp1[y],1);
}
if(i==6)
{
value=(t1+t2+t3+t4+t5)/5;
if(value<0)
{
value_flag=1;
value=value*-1;
}
else
value_flag=0;
if(value_flag==1)
temp2[3]='-';
else
temp2[3]=' ';
temp2[1]='a';
if(temp<100)
temp2[4]=' ';
else
temp2[4]=value%1000/100+0x30;
temp2[5]=value%100/10+0x30;
temp2[6]='.';
temp2[7]=value%10+0x30;
temp2[8]=0xdf;
temp2[9]='C';
writelcd(0xc0,0);
for(y=0;y<10;y++)
writelcd(temp2[y],1);
}
if(temp_flag==1)
{
temp1[3]='-';
temp2[3]='-';
}
else
{
temp1[3]=' ';
temp2[3]=' ';
}
}
void initT0()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void keyscan()
{
if(key==0)
{
delay(5);
if(key==0)
{
TR0=0;
while(!key);
TR0=1;
}
}
}
void main()
{
initlcd();
initT0();
while(1)
{
keyscan();
if(num==1)//第一页
{
gettemp(1);
display(1);
gettemp(2);
display(2);
}
if(num==2)//第二页
{
gettemp(3);
display(3);
gettemp(4);
display(4);
}
if(num==3)//第三页
{
gettemp(5);
display(5);
display(6);
}
}
}
void timer_0() interrupt 1
{
uchar us,s;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
us++;
if(us==20)
{
us=0;
s++;
if(s==1)
num=1;
if(s==2)
num=2;
if(s==3)
{
s=0;
num=3;
}
}
}
|