我在用中断读取温度值的时候,送到数码管中显示的时候会闪烁,源程序如下
#include<reg52.h>
#include<intrins.h>
typedef unsigned int ui;
typedef unsigned char uc;
sbit dq=P3^7;
sbit a1=P1^7;
sbit a2=P1^6;
sbit a3=P1^5;
bit flag;
uc num;
ui m,c=0;
uc code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uc code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,
0xed,0xfd,0x87,0xff,0xef};
void delay(uc x)
{
uc a,b;
for(a=x;a>0;a--)
for(b=10;b>0;b--);
}
void delay1(uc x)
{
while(--x);
}
void delay2(ui x)
{
ui a,b;
for(a=x;a>0;a--)
for(b=500;b>0;b--);
}
void inti() //初始化
{
uc i;
dq=1;
_nop_();
_nop_();
dq=0;
delay(20); //大概延时640us
dq=1;
while((dq)&&(i<200))
i++;
delay1(50); //延时114us让ds18b20保持低电平
dq=1;
}
void write_time(uc dat) //写操作
{
uc i,a;
for(i=0;i<8;i++)
{
a=dat&0x01;
dat>>=1;
if(a==0) //判断是否写0;
{
dq=0;
delay1(27); //延时64us,至少60us
dq=1;
}
else //否则写1
{
dq=0;
_nop_();
_nop_();
dq=1;
delay1(27);//延时了64us,从拉低总线开始到这儿至少60us
}
}
dq=1;
}
uc read_time() //读操作
{
uc i,date=0,a=0;
for(i=0;i<8;i++)
{
dq=0;
_nop_();
dq=1;
_nop_();
_nop_();
a=dq;
date=(date>>1) | (a<<7);
delay1(27); //延时64us
}
return date;
}
ui duwendu()
{
uc a=0,b=0;
float k=0;
inti();
delay(35);
write_time(0xcc);
write_time(0x44);
delay2(300);
inti();
delay(35);
write_time(0xcc);
write_time(0xbe);
a=read_time();
b=read_time();
c=b; //C为整形数据,一般占用2个字节
c<<=8;
c=c|a;
k=c*0.0625;
c=k*10+0.5;
return c;
}
void display(uc bai,uc shi,uc ge)
{
a1=1;
a2=1;
a3=0;
P0=table[bai];
delay(5);
a1=0;
a2=0;
a3=1;
P0=table1[shi];
delay(5);
a1=1;
a2=0;
a3=1;
P0=table[ge];
delay(5);
}
void main()
{
EA=1;
ET1=1;
TMOD=0X10;
TH1=0X4C;
TL1=0X00;
TR1=1;
while(1)
{
// duwendu();
display(c/100,c%100/10,c%10);
}
}
void zd1() interrupt 3 //中断
{
TH1=0x4c;
TL1=0x00;
num++;
if(num==10)
{
num=0;
duwendu();
}
}
|