一,C51程序,我的是 用LCD1602显示的温度计,另外 我给的图是AT89C2051 你可以按程序给的接口换做51
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define lcd P0
#define n 5
sbit lcd_e=P2^6;
sbit lcd_rw=P2^5;
sbit lcd_rs=P2^4;
sbit ds18b20_io=P1^7;
uchar code table[]=
{
'0','1','2','3','4','5','6','7','8','9'
};
uchar temper=0;
void delayms(uint cc)
{
uint x=cc;
uint y=110;
for(;x>0;x--)
for(;y>0;y--);
}
void lcd_com(uchar com)
{
lcd_e=0;
lcd_rw=0;
lcd_rs=0;
delayms(n);
lcd=com;
lcd_e=1;
delayms(n);
lcd_e=0;
}
void lcd_data(uchar date)
{
lcd_e=0;
lcd_rw=0;
lcd_rs=1;
delayms(n);
lcd=date;
lcd_e=1;
delayms(n);
lcd_e=0;
}
void lcd_str(char*s)
{
char*str;
str=s;
while(*str)
{
lcd_data(*str);
str++;
}
}
void rest_ds18b20() //复位18b20
{
uchar delay;
ds18b20_io=1;
ds18b20_io=0;
delay=250;
while(delay--);
ds18b20_io=1;
delay=15;
while(delay--);
}
void acs_18b20()
{
uchar delay=15;
while(ds18b20_io);
while(!(ds18b20_io));
while(delay--);
}
//发送
void write (uchar date)
{
uchar i=0,delay=30;
for(;i<8;i++)
{
ds18b20_io=0;
_nop_();
_nop_();
if(date&0x01)
{
ds18b20_io=1;
while(delay--);
}
else
{
ds18b20_io=0;
while(delay--);
ds18b20_io=1;
_nop_();
_nop_();
}
date>>=1;
}
}
//接受
uchar read_ds18b20()
{
uchar date=0,delay=8,i=0;
for(;i<8;i++)
{
date>>=1;
ds18b20_io=0;
_nop_();
ds18b20_io=1;
while(delay--);
if(ds18b20_io) date|=0x80;
delay=30;
while(delay--);
}
return date;
}
//启动温度转换
void convert_ds18b20(void)
{
rest_ds18b20();
acs_18b20();
write(0xcc);
write(0x44);
}
//读取温度
void read_te()
{
uchar temper_low,temper_hig;
uchar delay=250;
rest_ds18b20();
acs_18b20();
while(delay--);
delay=250;
while(delay--);
write(0xcc);
write(0xbe);
temper_low=read_ds18b20();//读取温度低字节
temper_hig=read_ds18b20();//读取温度高字节
temper=((temper_low>>4)|(temper_hig<<4));
}
void main()
{
char*str1="2011-5-28";
char*str2="temper:";
char*str3="'C";
lcd_com(0x38);
lcd_com(0x0c);
lcd_com(0x06);
lcd_com(0x01);
lcd_com(0x80+0x03);
lcd_str(str1);
lcd_com(0x80+0x40);
lcd_str(str2);
while(1)
{
read_te();
lcd_com(0x80+0x40+0x07);
lcd_data(table[temper/100]);
lcd_data(table[temper/10%10]);
lcd_data(table[temper%10]);
lcd_str(str3);
delayms(500);
}
}
二,图