这串代码实现按1加1功能,为什么快速按按键是可以实现加1,慢点按它就会跳呢?
#include<reg52.h>
unsigned char code Tab[]= //共阴数码管段码表0-F
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
sbit key =P3^1;
void Delay1ms(unsigned int xms) //延时1ms函数
{
unsigned char i,j;
while(xms)
{
i = 2 ;
j = 239 ;
do
{
while (--j);
}
while (--i);
xms--;
}
}
void main () //主函数
{
unsigned char n = 0 ; //局部变量计位数n是段码表的位置
P0= 0x3f; //定义初始值显示0
while(1)
{
if(key == 0) //判断按键是否按下
{
Delay1ms(10); //消抖
if(key==0) //再次判断
{
P0=Tab[n]; //把第n个段码赋值给P0口
Delay1ms(50); //延时50ms
while(key==1) //判断按键是否松开
{
Delay1ms(100);
n++;
}
}
if(n>=16) //控制加的位数别超出段码表了
{
n = 0 ; //如果超出就让他回0去
}
}
}
}