请大神看看哪出了问题 显示不是显示温度 而且还会一会变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);
}
}
-
好的 那我试试、我就感觉这样可以显示的 可是就不知道为什么显示不出来
还是不行 你帮我看看我的程序哪里出了问题了 拜托了
这是我自己的读DS18B20部分的代码,你可以参考下,写得很清晰。
- /*
- * @Prototype:extern u8 DS18B20_readByte(void)
- * @Argument:None
- * @Ret-val:reading result
- * @Note:read one byte from DS18B20
- */
- extern u8 DS18B20_readByte(void)
- {
- u8 i = 0x00; /* count variable */
- u8 r = 0x00; /* result variable */
- for(i = 0x00;i< 0x08;i++)
- {
- /* STEP-1:read from LSB */
- r = r >> 0x01;
- /* STEP-2:make bus is in low level status and produce a read signal */
- DS18B20_DQ = 0;
- /* STEP-3:wait for 5us */
- DELAY_5us();
- /* STEP-4:release bus and ready for reading data */
- DS18B20_DQ = 1;
- /* STEP-5:wait about 5us again */
- DELAY_5us();
- /* STEP-6:read bus status */
- if(DS18B20_DQ)
- {
- r = r | 0x80;
- }
- /* STEP-7:following the timing,it requires wait about 30us */
- DELAY_30us();
- /* STEP-8:pull up bus for next use */
- DS18B20_DQ = 1;
- }
- return r;
- }
复制代码一周热门 更多>