AT89C51单片机数码管闪烁问题怎么解决
#include<reg51.h>#defineu8unsignedchar#defineu16unsignedintsbitdula=P2^6;//段选信号的锁存器控制...
#include <reg51.h>
#define u8 unsigned char
#define u16 unsigned int
sbit dula=P2^6; //段选信号的锁存器控制
sbit wela=P2^7; //位选信号的锁存器控制
u8 code wei[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//数码管各位的码表
u8 code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x00};//0-f的码表
u8 dis_shu[8]={0};
bit dis_flag=0;
u8 lcd_state=0;
u8 dis_time_count=0;
//******数码管显示函数***********//
void display(long num)
{
long i,weishu;
for(weishu=0,i=num;i;weishu++)
{
i=i/10;
}
for(i=weishu;i;i--)
{
dis_shu[8-i]=num%10;
num=num/10;
}
for(i=weishu;i<8;i++)
{
dis_shu[7-i]=9;
}
if(dis_flag==1)
{
dis_flag=0;
switch(lcd_state)
{
case 0:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 1:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 2:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 3:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 4:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 5:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 6:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 7:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
default:break;
}
lcd_state=(++lcd_state)%8;
}
}
//********定时器0中断处理函数********//
void Timer0() interrupt 1
{
dis_time_count++;
if(dis_time_count==2)
{
dis_time_count=0;
dis_flag=1;
}
TH0=0xfc;
TL0=0x18; //中断10us
}
//********定时器0初始化函数********//
void time0init()
{
TMOD=0x01;
TH0=0xfc;
TL0=0x18; //中断10us
ET0=1; //允许T0中断
TR0=1; //开启定时器
EA=1; //开启总中断
}
void main()
{
time0init();
dula=0;
wela=0;
while(1)
{
display(678);
}
} 展开
#define u8 unsigned char
#define u16 unsigned int
sbit dula=P2^6; //段选信号的锁存器控制
sbit wela=P2^7; //位选信号的锁存器控制
u8 code wei[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//数码管各位的码表
u8 code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x00};//0-f的码表
u8 dis_shu[8]={0};
bit dis_flag=0;
u8 lcd_state=0;
u8 dis_time_count=0;
//******数码管显示函数***********//
void display(long num)
{
long i,weishu;
for(weishu=0,i=num;i;weishu++)
{
i=i/10;
}
for(i=weishu;i;i--)
{
dis_shu[8-i]=num%10;
num=num/10;
}
for(i=weishu;i<8;i++)
{
dis_shu[7-i]=9;
}
if(dis_flag==1)
{
dis_flag=0;
switch(lcd_state)
{
case 0:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 1:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 2:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 3:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 4:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 5:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 6:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
case 7:dula=1;P0=duan[dis_shu[lcd_state]];dula=0;P0=0x00;wela=1;P0=wei[lcd_state];wela=0;break;
default:break;
}
lcd_state=(++lcd_state)%8;
}
}
//********定时器0中断处理函数********//
void Timer0() interrupt 1
{
dis_time_count++;
if(dis_time_count==2)
{
dis_time_count=0;
dis_flag=1;
}
TH0=0xfc;
TL0=0x18; //中断10us
}
//********定时器0初始化函数********//
void time0init()
{
TMOD=0x01;
TH0=0xfc;
TL0=0x18; //中断10us
ET0=1; //允许T0中断
TR0=1; //开启定时器
EA=1; //开启总中断
}
void main()
{
time0init();
dula=0;
wela=0;
while(1)
{
display(678);
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询