我的温度 显示不能随传感器温度的改变而变化是为什么?
#include <reg52.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit ds=P3^5; //温度传感器
sbit beep=P3^7;
sbit rs=P2^4; //控制发光二极管
sbit rw=P2^3;
sbit lcde=P2^2;
uchar tp,tpx,num,count,miao,fen,shi;
uchar code table[]="happy everyday!";
uchar code table1[]=": :";
uchar code table2[]="weather:cold";
uchar code table3[]="weather:hot";
void delay(uint z) //延时
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay2(uint i)
{
while(i--);
}
////***ds18b20*****///
void init_ds18b20() //DS18B20复位,初始化函数
{
ds=1;
delay2(8);
ds=0;
delay2(90);
ds=1;
delay2(4);
if(ds==0)
delay2(14);
ds=1;
}
uchar read_ds18b20_byte() //读1字节数据函数(MSB)
{
uchar i,dat;
dat=0;
for(i=0;i<8;i++)
{
ds=0;
dat=dat>>1;
ds=1;
if(ds==1)
{
dat=dat|0x80;//最高位或上一个1,使得最高位为1
}
delay2(4);
}
return dat;//循环8次完成一个字节,并返回数据
}
void write_ds18b20_byte(uchar date)//向ds8b20写1字节数据函数
{
uchar j=0;
for(j=0;j<=8;j++)
{
ds=0;
ds=date&0x01; //取出数据最低位送到数据线
delay2(5);
ds=1;
date=date>>1;//字节右移一位,接着取第二位
delay2(5);
}
}
int read_ds18b20_temp() //DS18B20开始获取温度并转换
{
uchar a=0;//低8位
uint b=0;//高8位
uint t=0;//组成新温度
init_ds18b20();
write_ds18b20_byte(0xcc);
write_ds18b20_byte(0x44);
init_ds18b20();
write_ds18b20_byte(0xcc);
write_ds18b20_byte(0xbe);
a=read_ds18b20_byte();
b=read_ds18b20_byte();
a=a&0x00ff;
b=(b&0x000f)<<8;
t=a|b;
return t;
}
void writecom(uchar com)
{
rs=0;
rw=0;
lcde=0;
delay(5);
lcde=0;
P1=com;
delay(5);
lcde=1;
delay(5);
lcde=0;
}
void writedate(uchar date)
{
rs=1;
rw=0;
delay(5);
lcde=0;
P1=date;
delay(5);
lcde=1;
delay(5);
lcde=0;
}
void wendudisplay()//显示温度数值函数,t传递的是整型的温度值
{
uint tp,tpz;
uchar tp2,tpshi,tpge,tpxiao;
tp=read_ds18b20_temp();
tp2=tp&0x000f;
tpxiao=tp2*0.0625;
tpz=(tp&0x0ff0)>>4;
tpz=tpz*0.0625;
tpshi=tpz/10;
tpge=tpz%10;
writecom(0x80+0x40+10);
writedate(tpshi+0x30);
writedate(tpge+0x30);
writedate('.') ;
writedate(tpxiao+0x30);
writedate(0xdf);
writedate('C');
if(tpz<15)
{
writecom(0x80);
for(num=0;num<16;num++)
{
writedate(table2[num]);
delay(5);
beep=0;
}
}
if((tpz<25)&&(tpz>=15))
{
writecom(0x80);
for(num=0;num<15;num++)
{
writedate(table[num]);
delay(5);
beep=1;
}
}
if(tpz>25)
{
writecom(0x80);
for(num=0;num<16;num++)
{
writedate(table3[num]);
delay(5);
}
}
}
void writesfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
writecom(0x80+0x40+add);
writedate(0x30+shi);
writedate(0x30+ge);
writecom(0x80+0x40+add);
}
void init()
{
lcde=0;
writecom(0x38);
writecom(0x0c);
writecom(0x06);
writecom(0x01);
delay(5);
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
miao=15;
fen=12;
shi=12;
}
void main()
{
init();
while(1)
{
wendudisplay();
writesfm(0,shi);
writesfm(3,fen);
writesfm(6,miao);
writecom(0x80+0x40+2);
writedate(':');
writecom(0x80+0x40+5);
writedate(':');
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
shi++;
fen=0;
if(shi==24)
shi=0;
}
}
}
}
|