- UID
- 274269
- 阅读权限
- 1
- 推广
-
- 幽默
-
- 注册时间
- 2012-3-20
- 最后登录
- 1970-1-1
|
注册家电维修技术论坛,与同行畅聊维修技术,享更多技术论坛功能。
您需要 登录 才可以下载或查看,没有帐号?快速注册
x
这个程序是DS18B20读回的温度值显示在数码管的两位上,但效果是十位上的数不停闪烁,个位的数稳定正常显示,怎么处理?谢谢各位了
程序:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit DQ=P3^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar temp,shi,ge;
void delay_4us(uint time)
{
while(time--);
}
void delay_ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uchar DS18b20_reset()
{
uchar i;
DQ=1;
DQ=0;
delay_4us(180);
DQ=1;
delay_4us(10);
i=DQ;
while(!DQ); //等待存在脉冲
return i;
}
void DS18b20_writebyte(uchar dat18w)
{
uchar i;
for(i=0;i<8;i++)
{
DQ=0;
DQ=dat18w&0x01;
delay_4us(4);
DQ=1;
dat18w>>=1;
}
delay_4us(10);
}
uchar DS18b20_readbyte()
{
uchar i,dat18r;
for(i=0;i<8;i++)
{
DQ=1;
DQ=0;
dat18r>>=1;
DQ=1;
if(DQ)
dat18r|=0x80;
delay_4us(10);
}
return dat18r;
}
uchar read1temperature()
{
uchar L,H;
while(DS18b20_reset());
DS18b20_writebyte(0xcc);
DS18b20_writebyte(0x44);
delay_ms(750);
while(DS18b20_reset());
DS18b20_writebyte(0xcc);
DS18b20_writebyte(0xbe);
L=DS18b20_readbyte();
H=DS18b20_readbyte();
H<<=4;
H+=(L&(0xf0))>>4;
while(DS18b20_reset());
return H;
}
void display(uchar shi,uchar ge)
{
P0=table[shi];
P2=0xbf;
delay_ms(10);
P0=table[ge];
P2=0xdf;
delay_ms(10);
}
void main()
{
while(1)
{
temp=read1temperature();
shi=temp/10;
ge=temp%10;
display(shi,ge);
}
} |
|