请大神看看哪出了问题 显示不是显示温度 而且还会一会变8一会变0 ;
如图所示:
程序如下:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit DS=P2^2;
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit lcdrw=P3^6;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]="temperature:";
void delayus(uint z)
{while(--z);}
void delayms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
void Write_1602com(uchar com)
{
lcdrs=0;
P0=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void Write_1602date(uchar date)
{
lcdrs=1;
P0=date;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void init()
{
dula=0;
wela=0;
lcden=0;
lcdrw=0;
Write_1602com(0x38);
Write_1602com(0x01);
Write_1602com(0x0c);
Write_1602com(0x06);
}
void Write_sfm(uchar add,uchar date) //1250
{
uchar qian,bai,shi,ge;
qian=date/1000;
bai=date%1000/100;
shi=date%1000%100/10;
ge=date%1000%100%10;
Write_1602com(0x80+0x40+add);
Write_1602date(0x30+qian);
Write_1602date(0x30+bai);
Write_1602date(0x30+shi);
Write_1602date(0x30+ge);
}
bit DS18B20_init() // DS18b20初始函数
{
bit i;
DS=1;
_nop_();
DS=0;
delayus(75); //75*6.5+12=500
DS=1;
delayus(2);//2*6.5+12=25
i=DS;
delayus(20);//20*6.5+12=132
DS=1;
_nop_();
return i;
}
void Write_DS18B20byte(uchar date)//写
{
uchar i;
for(i=0;i<8;i++)
{
DS=0;
_nop_();
DS=date&0x01;
delayus(15);//6.5*15=110
DS=1;
_nop_();
date>>=1;
}
}
uchar Read_DS18B20byte() //读
{
uchar i,j,date;
for(i=0;i<8;i++)
{
DS=0;
_nop_();
DS=1;
delayus(1);//6.5+12=18.5
j=DS;
delayus(10);//6.5*10+12=72
DS=1;
_nop_();
date=(j<<7)|(date>>1);
return date;
}
}
DS18B20_init();
Write_DS18B20byte(0xcc);
Write_DS18B20byte(0x44);
DS18B20_init();
Write_DS18B20byte(0xcc);
Write_DS18B20byte(0xbe);
L= Read_DS18B20byte();
M= Read_DS18B20byte();
i=M;
i<<=8;
i|=L;
i=i*0.0625*10+0.5;
void main()
{
uint i,num,a;
uchar L,M;
init();
Write_1602com(0x80);
for(num=0;num<12;num++)
{
Write_1602date(table[num]);
delayms(5);
}
while(1)
{
DS18B20_init();
Write_DS18B20byte(0xcc);
Write_DS18B20byte(0x44);
DS18B20_init();
Write_DS18B20byte(0xcc);
Write_DS18B20byte(0xbe);
L= Read_DS18B20byte();
M= Read_DS18B20byte();
i=M;
i<<=8;
i|=L;
i=i*0.0625*10+0.5;
Write_sfm(5,a);
}
}
-
完成啦啦啦啦
一周热门 更多>