用ds18b20,LCD1602显示,可是什么都显示不了;下面是我的我的程序,帮我看看哪里错了。
#include<at89x52.h>
#define uchar unsigned char
#define uint unsigned int
uchar diplay[2];
uint temp;
sbit RS=P2^5;
sbit RW=P2^6;
sbit E=P2^7;
sbit DQ=P1^1;
uchar code tab[]="temperature:";
uchar code tab1[]="00oC";
void delay1ms(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<100;j++);
}
void delay1us(unsigned int i)
{
while(i--);
}
void write_com(uchar com)
{
RS=0;
RW=0;
E=0;
P2=com;
delay1ms(5);
E=1;
delay1ms(5);
E=0;
}
void write_dat(uchar dat)
{
RS=1;
RW=0;
E=0;
P2=dat;
delay1ms(5);
E=1;
delay1ms(5);
E=0;
}
void init()
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void ds18b20init()
{
DQ=0;
delay1us(480);
DQ=1;
delay1us(80);
DQ=0;
delay1us(480);
}
void ds18b20write_dat(uchar dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ=0;
delay1us(15);
DQ=dat;
dat<<=1;
delay1us(60);
DQ=1;
}
}
uchar ds18b20read_dat( )
{
uchar i,bi,byte;
uchar dat;
for(i=8;i>0;i--)
{
DQ=0;
delay1us(1);
DQ=1;
delay1us(10);
bi=dat;
byte=(byte>>1)|(bi<<7);
delay1us(45);
}
return dat;
}
void ds18b20Changtemp()
{
ds18b20init();
ds18b20write_dat(0xcc);
ds18b20write_dat(0x44);
}
uint ds18b20readtemp()
{
uchar tmh,tml;
temp=0;
ds18b20init();
ds18b20write_dat(0xcc);
ds18b20write_dat(0xbe);
tml=ds18b20read_dat();
tmh=ds18b20read_dat();
temp=tmh;
temp<<=8;
temp=temp|tml;
return temp;
}
void dispplay()
{
uchar i,tp;
tp=temp*0.625;
diplay[0]=tp/10+0x30;
diplay[1]=tp/1+0x30;
write_com(0x80);
for(i=0;i<14;i++)
{
write_dat(tab[i]);
}
write_com(0x80+0x40);
for(i=0;i<5;i++)
{
write_dat(tab1[i]);
}
}
void main()
{
init();
while(1)
{
ds18b20readtemp();
dispplay();
}
}
|