将DS18b20数字温度传感器采集到的温度数据显示在LCD上面
好久前的了~o(╯□╰)o
#include<reg52.h>
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ=P3^3;
sbit dula=P2^0;
sbit wela=P2^1;
sbit dzla=P3^5;
sbit lcden=P2^2;
sbit lcdrs=P2^4;
sbit lcdrw=P2^3;
void DelayUs2x(uchar t)
{
while(--t);
}
void DelayMs(uchar t)
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
void write_com(uchar com)
{
lcden=0;
lcdrs=0;
lcdrw=0;
P1=com;
DelayMs(2);
lcden=1;
DelayMs(2);
lcden=0;
}
void write_data(uchar date)
{
lcden=0;
lcdrs=1;
lcdrw=0;
P1=date;
DelayMs(2);
lcden=1;
DelayMs(2);
lcden=0;
}
void lcd_init()
{
dzla=1;
P1=0xff;
dzla=0;
wela=1;
P1=0xff;
wela=0;
dula=1;
P1=0xff;
dula=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
void init_DS18B20(void)
{
DQ=1;
DelayUs2x(5);
DQ=0;
DelayUs2x(200);
DelayUs2x(200);
DQ=1;
DelayUs2x(200);
DQ=1;
}
uchar DS18B20_read()
{
uchar i;
uchar dat;
for(i=8;i>0;i--)
{
DQ=0;
dat>>=1;
DQ=1;
if(DQ)
dat|=0x80;
DelayUs2x(25);
}
return dat;
}
void DS18B20_write(uchar dat)
{
uchar i;
for(i=8;i>0;i--)
{
DQ=0;
DQ=dat&0x01;
DelayUs2x(25);
DQ=1;
dat>>=1;
}
DelayUs2x(25);
}
uint read_temperature()
{
uint a,b,t;
init_DS18B20();
DS18B20_write(0xCC);
DS18B20_write(0x44);
DelayMs(10);
init_DS18B20();
DS18B20_write(0xCC);
DS18B20_write(0xBE);
a=DS18B20_read();
b=DS18B20_read();
b<<=8;
t=a+b;
return t;
}
void main (void)
{
int temp,i;
float temperature;
char buff[13];
lcd_init();
write_com(0x80+14);
write_data('C');
while(1)
{
temp=read_temperature();
temperature=(float)temp*0.0625;
sprintf(buff," Temp:%7.3f",temperature);
write_com(0x80);
for(i=0;i<13;i++)
{
write_data(buff[i]);
DelayUs2x(50);
}
DelayMs(100);
}
}
学习
亲,有图没···
把锁存去掉~就剩下液晶和温度传感器了~端口前面定义的应该看得出来吧
液晶是1602好像
可不可以麻烦帮我画个图啊,和这个程序匹配的图···万谢~