为什么显示是0,求哪位大神帮忙看哈!
时间:10-02
整理:3721RD
点击:
#include < reg51.h >
#include < intrins.h >
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P2^7; //定义DS18B20端口DQ
sbit LCD_RS=P3^4;
sbit LCD_RW=P3^6;
sbit LCD_EN=P3^7;
sbit ST=P0^0;
sbit SH=P0^1;
sbit Sjia=P3^2;
sbit Sjian=P3^3;
sbit WBJ=P2^6;
sbit HBJ=P2^5;
uchar code str1[]={"wend:"};
uchar code str2[]={"shid:"};
uchar data disdata1[6];
uchar data disdata2[4];
uint tvalue;//温度值
uchar tflag;//温度正负标志
uint tem0,tem1;
uint temp0,temp1;
uint f=0;
int hhhh=0;
int tsheding=20;//设置温度值
int hsheding=40;//设置湿度值
void zhongd0() interrupt 0//外部中断0:加设置
{
if(ST==1)
{
tsheding++;//温度值加1
}
if(SH==1)
{
hsheding++;//湿度值加1
}
}
void zhongd1() interrupt 2//外部中断1:减设置
{
if(ST==1)
{
tsheding--;//温度值减1
}
if(SH==1)
{
hsheding--;//湿度值减1
}
}
void timer0() interrupt 1 //T0定时中断
{
TR0=0;
TR1=0;
TL0=0xB0;
TH0=0x3C;
tem0=TL1;
tem1=TH1;
TL1=0x00;
TH1=0x00;
TR0=1;
TR1=1;
}
void timer1() interrupt 3 //T1定时中断
{
TR0=0;
TR1=0;
TL0=0xB0;
TH0=0x3C;
TL1=0x00;
TH1=0x00;
TR0=1;
TR1=1;
}
void init_timer()
{
TMOD=0x51;
TL0=0xB0;
TH0=0x3C;
TL1=0x00;
TH1=0x00;
TR0=1;
TR1=1;
}
void TPanDuan()
{
uint k=tvalue/(10);
if(tflag==0)
{
if((k>(tsheding*1.2))||(k<(tsheding*0.8)))
{
WBJ=1;
}
else
{
WBJ=0;
}
}
else
{
if((k>tsheding*(-1.2))||(k<tsheding*(-0.8)))
{
WBJ=1;
}
else
{
WBJ=0;
}
}
}
void delay1ms(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<110;j++);
}
void wr_com(uchar com)
{
delay1ms(1);
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
P1=com;
delay1ms(1);
LCD_EN=1;
delay1ms(1);
LCD_EN=0;
}
void wr_dat(uchar dat)
{
delay1ms(1);
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
P1=dat;
delay1ms(1);
LCD_EN=1;
delay1ms(1);
LCD_EN=0;
}
void lcd_init()
{
wr_com(0x38);delay1ms(1);
wr_com(0x08);delay1ms(1);
wr_com(0x01);delay1ms(1);
wr_com(0x06);delay1ms(1);
wr_com(0x0c);delay1ms(1);
}
void display(uchar*p)
{
while(*p!='\0')
{
wr_dat(*p);
p++;
delay1ms(1);
}
}
init_play()
{
lcd_init();
wr_com(0x80);
display(str1);
wr_com(0xc0);
display(str2);
//return(tvalue);
}
/****************DS18B20程序***************************************************/
void delay_18B20(uint i)
{
while(i--);
}
void ds18B20rst()
{
uchar x=0;
DQ=1;
delay_18B20(4);
DQ=0;
delay_18B20(100);
DQ=1;
delay_18B20(40);
}
uchar ds18B20rd()
{
uchar i=0;
uchar dat=0;
for(i=8;i>0;i--)
{
DQ=0;
dat>>=1;
DQ=1;
if(DQ)
dat|=0x80;
delay_18B20(10);
}
return(dat);
}
void ds18B20wr(uchar wdata)
{
uchar i=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=wdata&0x01;
delay_18B20(10);
DQ=1;
wdata>>=1;
}
}
void read_temp()
{
uchar a,b;
ds18B20rst();
ds18B20wr(0xcc);
ds18B20wr(0x44);
ds18B20rst();
ds18B20wr(0xcc);
ds18B20wr(0xbe);
a=ds18B20rd();
b=ds18B20rd();
tvalue=b;
tvalue<<=8;
tvalue=tvalue|a;
if(tvalue<0x0fff)
{
tflag=0;
}
else
{
tvalue=~tvalue+1;
tflag=1;
}
tvalue=tvalue*(0.625);
}
void ds18B20disp1()
{
uchar flagdat;
uchar flagtsheding;
disdata1[0]=tvalue/1000+0x30;
disdata1[1]=tvalue%1000/100+0x30;
disdata1[2]=tvalue%100/10+0x30;
disdata1[3]=tvalue%10+0x30;
if(tsheding<hhhh)
{
flagtsheding='-';
disdata1[4]=(~tsheding+1)/10+0x30;
disdata1[5]=(~tsheding+1)%10+0x30;
}
else
{
flagtsheding=' ';
disdata1[4]=tsheding/10+0x30;
disdata1[5]=tsheding%10+0x30;
}
if(tflag==0)
flagdat=0x20;
else
flagdat=0x2d;
if(disdata1[0]==0x30)
{
disdata1[0]=0x20;
if(disdata1[1]==0x30)
{
disdata1[1]=0x20;
}
}
wr_com(0x85);
wr_dat(flagdat);
wr_com(0x86);
wr_dat(disdata1[0]);
wr_com(0x87);
wr_dat(disdata1[1]);
wr_com(0x88);
wr_dat(disdata1[2]);
wr_com(0x89);
wr_dat(0x2e);
wr_com(0x8a);
wr_dat(disdata1[3]);
wr_com(0x8b);
wr_dat('(');
wr_com(0x8c);
wr_dat(flagtsheding);
wr_com(0x8d);
wr_dat(disdata1[4]);
wr_com(0x8e);
wr_dat(disdata1[5]);
wr_com(0x8f);
wr_dat(')');
}
void ds18B20disp2()
{
f=tem1*256+tem0;
f=f*20;
if((6033<=f)&&(f<=7351))
{
if((7224<f)&&(f<=7351))
{temp0=0;temp1=(7351-f)*10/127;}
if((7100<f)&&(f<=7224))
{temp0=1;temp1=(7224-f)*10/124;}
if((6976<f)&&(f<=7100))
{temp0=2;temp1=(7100-f)*10/124;}
if((6853<f)&&(f<=6976))
{temp0=3;temp1=(6976-f)*10/123;}
if((6728<f)&&(f<=6853))
{temp0=4;temp1=(6853-f)*10/125;}
if((6600<f)&&(f<=6728))
{temp0=5;temp1=(6728-f)*10/128;}
if((6468<f)&&(f<=6600))
{temp0=6;temp1=(6600-f)*10/132;}
if((6330<f)&&(f<=6468))
{temp0=7;temp1=(6468-f)*10/138;}
if((6186<f)&&(f<=6330))
{temp0=8;temp1=(6330-f)*10/144;}
if((6033<f)&&(f<=6186))
{temp0=9;temp1=(6186-f)*10/153;}
}
else
{
temp0=0;temp1=0;
}
disdata2[0]=temp0+0x30;
disdata2[1]=temp1+0x30;
disdata2[2]=hsheding/10+0x30;
disdata2[3]=hsheding%10+0x30;
wr_com(0xc8);
wr_dat(disdata2[0]);
wr_com(0xc9);
wr_dat(disdata2[1]);
wr_com(0xca);
wr_dat('%');
wr_com(0xcb);
wr_dat('(');
wr_com(0xcc);
wr_dat(disdata2[2]);
wr_com(0xcd);
wr_dat(disdata2[3]);
wr_com(0xce);
wr_dat('%');
wr_com(0xcf);
wr_dat(')');
}
void main()
{
//Sjia=0;
//Sjian=0;
WBJ=0;
HBJ=0;
init_play();
delay1ms(50);
EA=1;
EX0=1;
EX1=1;
ET0=1;
ET1=1;
IT0=1;
IT1=1;
init_timer();
while(1)
{
read_temp();
TPanDuan();
ds18B20disp1();
delay1ms(50);
ds18B20disp2();
delay1ms(50);
}
}