|
51单片机,用一个独立按键来实现跑表遇到一点问题。
[复制链接]
1.数码管前三位显示一个跑表,从000到999之间以1%秒速度运行,当按下一个独立键盘时跑表停止,松开手后跑表继续运行。(用定时器设计表)。
code:
#include
#define uchar unsigned char
#define uint unsigned int
sbit key1 = P3^4;
sbit dula = P2^6;
sbit wela = P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uint num;
uchar bai, shi, ge, flag = 0,t0 = 0;
void delay(uchar z);
void display(void);
void main(void)
{
num = 0;
P3 = 0xff;
TMOD = 0x01;
TH0 = (65536-10000) / 256;
TL0 = (65536-10000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1) {
if(key1 == 0) {
delay(5);
if(key1 == 0) {
flag = 1;
}
while(!key1);
delay(5);
while(!key1);
}
display();
}
}
void display(void)
{
dula = 1;
P0 = table[num / 100];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
delay(1);
dula = 1;
P0 = table[num % 100 / 10];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
delay(1);
dula = 1;
P0 = table[num % 10];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfb;
wela = 0;
delay(1);
}
void delay(uchar z)
{
uchar x, y;
for(x=110; x>0; x--)
for(y=z; y>0; y--)
;
}
void intp(void) interrupt 1
{
TH0 = (65536-10000) / 256;
TL0 = (65536-10000) % 256;
t0++;
flag = 0;
if(flag == 0) {
TR0 = 1;
if(t0 == 1) {
t0 = 0;
if(num == 999)
num = 0;
num++;
}
}
else
TR0 = 0;
}
问题:
编译,烧写,一切正常,就是按下第一个独立按键时,跑表能够停止松开也能继续跑,但是按下时前面两个数码管熄灭了,只有最后一个还亮着,
请问一下啊如何解决。
|
|