#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit duan=P2^6;
sbit wei=P2^7;
sbit en=P3^4;
sbit rs=P3^5;
sbit DQ=P2^2;
//uchar code table[]={"ds18b20"};
//uchar code table1[]={"0123456789"};
void delay(uint i)
{
while(i--);
}
void delay1(uchar t)
{
uchar x,y;
for(x=t;x>0;x--)
{
for(y=110;y>0;y--);
}
}
init_ds18b20(void)
{
uchar x=0;
DQ=1;
delay(8);
DQ=0;
delay(80);
DQ=1;
delay(14);
x=DQ;
delay(20);
}
readonebyte(void)
{
uchar i=0;
uchar dat=0;
for(i=8;i>0;i--)
{
DQ=0;
dat=dat>>1;
DQ=1;
if(DQ==1)
{
dat=dat|0x80;
}
delay(4);
}
return(dat);
}
writeonebyte(uchar dat)
{
uchar i=0;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
delay(5);
DQ=1;
dat=dat>>1;
}
delay(4);
}
readtemper(void)
{
uchar a=0;
uchar b=0;
uchar t=0;
init_ds18b20();
writeonebyte(0xcc);
writeonebyte(0x44);
delay1(750);
init_ds18b20();
writeonebyte(0xcc);
writeonebyte(0xbe);
delay1(750);
a=readonebyte();
b=readonebyte();
t=(b<<4)|(a>>4);
if(t>128)
{
t=~t+1;
}
return(t);
}
/****************************************/
1602程序
/*****************************************/
uchar com,inf;
uchar code table[]="ds18b20";
uchar code table1[]="0123456789";
void write_com(uchar com)
{
rs=0;
P0=com;
delay1(1);
en=1;
delay1(2);
en=0;
}
void write_inf(uchar inf)
{
rs=1;
P0=inf;
delay1(2);
en=1;
delay1(2);
en=0;
}
void init_lcd()
{
duan=0;
wei=0;
en=0;
rs=0;
write_com(0x38);
write_com(0x0f);
write_com(0x06);
write_com(0x01);
}
void main()
{
uchar num;
uchar tp,tp1,tp2;
init_lcd();
delay(5);
write_com(0x80);
for(num=0;num<7;num++)
{
write_inf(table[num]);
delay(10);
}
while(1)
{
tp=readtemper();
tp1=tp%10;
tp2=tp/10;
delay(20);
write_com(0x80+0x0b);
write_inf(table1[tp2]);
write_com(0x80+0x0c);
write_inf(table1[tp1]);
write_com(0x80+0x0d);
write_inf(0xdf);
write_com(0x80+0x0e);
write_inf('c');
}
} |