#include
#define uchar unsigned char
#define uint unsigned int
sbit lcd_rs=P2^0;
sbit lcd_rw=P2^1;
sbit lcd_en=P2^2;
sbit tempt=P3^4;
uchar code disp[]="0123456789";
uchar code disp2[]="Temperature:";
uchar code disp3[]="^C";
uint u_tempt;
float f_tempt;
void delay(uint x)
{
uint i,j;
for(i=0;ifor(j=0;j<130;j++);
}
void write_command(uchar com)
{
lcd_rs=0;
P0=com;
delay(5);
lcd_en=1;
delay(5);
lcd_en=0;
delay(5);
}
void write_data(uchar date)
{
lcd_rs=1;
P0=date;
delay(5);
lcd_en=1;
delay(5);
lcd_en=0;
delay(5);
}
void lcd_init()
{
lcd_en=0;
lcd_rw=0;
write_command(0x38);
write_command(0x0c);
write_command(0x06);
write_command(0x01);
}
void display_tempt(uint date)
{
uchar i,j,k;
i=date/100;
j=date%100/10;
k=date%100%10;
write_command(0xc0+5);
write_data(disp[i]);
write_data(disp[j]);
write_data('.');
write_data(disp[k]);
}
void reset_tempt()
{
uint i;
tempt=0;
i=90;
while(i>0)
i--;
tempt=1;
i=4;
while(i>0)
i--;
}
bit read_bit()
{
uint i;
bit dat;
tempt=0;
i++;
tempt=1;
i++;
i++;
dat=tempt;
i=8;
while(i>0)
i--;
return dat;
}
uchar read_tempt()
{
uchar i,j,dat;
dat=0;
for(i=1;i<=8;i++)
{
j=read_bit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
void write_byte(uchar dat)
{
uint i;
uchar j;
bit testbit;
for(j=1;j<=8;j++)
{
testbit=dat&0x01; //每次写一位数据
dat=dat>>1;
if(testbit)
{
tempt=0;
i++;
tempt=1;
i=8;
while(i>0)i--;
}
else
{
tempt=0;
i=8;
while(i>0)i--;
tempt=1;
i++;
i++;
}
}
}
void convert_tempt()
{
reset_tempt();
delay(1);
write_byte(0xcc);
write_byte(0x44);
}
uint get_tempt()
{
uchar a=0,b=0;
//uint t=0;
reset_tempt();
write_byte(0xcc);
write_byte(0x44);
reset_tempt();
write_byte(0xcc);
write_byte(0xbe);
a=read_tempt();
b=read_tempt();
u_tempt=b;
u_tempt<<=8;
u_tempt=u_tempt|a;
f_tempt=u_tempt*0.0625;
u_tempt=f_tempt*10;
return u_tempt;
}
void display()
{
uchar i,j;
write_command(0x80+3);
for(i=0;i<11;i++)
{
write_data(disp2[i]);
}
write_command(0xc0+10);
for(j=0;j<2;j++)
{
write_data(disp3[j]);
}
}
void main()
{
uchar i;
lcd_init();
display();
while(1)
{
convert_tempt();
for(i=0;i<10;i++)
{
display_tempt(get_tempt());
}
}
}
http://zhidao.baidu.com/question/239791875.html
18B20问题,我先帮你解决了。
怎么联系不到你。。。东西做的差不多
呵呵,很简单的项目。