怎么通过按键来控制数码管的其中一位依次闪烁啊???求帮助 万分感谢 我想用延时来控制数码管位选置1和0来控制,为什么不行呢?附带程序
#include<reg51.h>
typedef unsigned char u8;//重定义
typedef unsigned int u16;
sbit LED_bj=P0^0;//报警灯
sbit s1=P2^7;//数码管位选
sbit s2=P2^6;
sbit s3=P2^5;
sbit s4=P2^4;
sbit k1=P2^0;
sbit k2=P2^1;
sbit k3=P2^2;
sbit k4=P2^3;
sbit LE_SMG=P1^6;//锁存
sbit LE_LED=P1^7;
u16 flag_10,flag_10bj,flag_5,flag_5bj,flag_a,miss,num,count=12,d=10000;//标志位:10s标志位,10报警,5s标志位,5s报警,
u8 code smgduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void delay(u16 x)//延时
{
u16 i,j;
for(i=0;i<x;i++)
{
for(j=0;j<1000;j++);
}
}
void display1(u16 x)//显示
{
s1=0;
P0=smgduan[x%10];
delay(1);
P0=0xff;
s1=1;
s2=0;
if(x/10==0)
{
P0=0xff;
}
else
{
P0=smgduan[x/10%10];
}
delay(1);
P0=0xff;
s2=1;
s3=0;
if(x/100==0)
{
P0=0xff;
}
else
{
P0=smgduan[x/100%10];
}
delay(1);
P0=0xff;
s3=1;
s4=0;
if(x/1000==0)
{
P0=0xff;
}
else
{
P0=smgduan[x/1000%10];
}
delay(1);
P0=0xff;
s4=1;
}
void led()
{
if(flag_10)
{
LE_LED=1;
if(flag_10bj)
{
LED_bj=0;
}
else
{
LED_bj=1;
}
delay(1);
LE_LED=0;
display1(count);
}
else
{
LED_bj=1;
}
if(flag_5)
{
LE_LED=1;
if(flag_5bj)
{
LED_bj=0;
}
else
{
LED_bj=1;
}
delay(1);
LE_LED=0;
display1(count);
}
// else
// {
// ;
// }
}
void menu1()
{
LED_bj=1;
LE_LED=0;
while(1)
{
// LE_SMG=1;
display1(d);
// LE_SMG=0;
delay(500);
s4=~s4;
}
}
void main()
{
IT1=1;
EX1=1;
TMOD|=0X01;//定时器初始化
TR0=1;
TH0=0xDC;
TL0=0x00;
ET0=1;
EA=1;
LE_LED=0;
if(k1==0)
{
delay(1);
if(k1==0)
{
delay(1);
if(k1==0)
{
flag_a=1;
if(flag_a==1)
{
while(1)
{
menu1();
}
}
}
}
}
while(1)
{
led();
display1(count);
if(k1==0) break;
}
}
void timer0() interrupt 1//中断
{
num++;
if(num==100)
{
num=0;
if(count==0)
{
miss++;
count=12;
// flag_10=~flag_10;
// flag_5=~flag_5;
}
else
{
count--;
}
if(count>5&&count<=10)
{
flag_10=1;
}
else
{
flag_10=0;
}
if(count<=5)
{
flag_5=1;
}
else
{
flag_5=0;
}
}
if(num<50)//每0.5s闪
{
flag_10bj=1;
}
else
{
flag_10bj=0;
}
if(num%10==0)//每0.1s闪
{
flag_5bj=1;
}
else
{
flag_5bj=0;
}
TH0=0xDC;
TL0=0x00;
}
void int1() interrupt 2
{
if(k2==0)
{
delay(1);
if(k2==0)
{
delay(1);
if(k2==0)
{
count=12;
}
}
}
}
|