//按下
#include <reg52.h>
sbit ADDR0=P1^0;
sbit ADDR1=P1^1;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
sbit KEY1=P2^4;
sbit KEY2=P2^5;
sbit KEY3=P2^6;
sbit KEY4=P2^7;
unsigned char LedChar[]={//数码管
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90
};
void delay()//延时
{
unsigned int i=1000;
while(i--);
}
void main()
{
bit keybuf=1;//按键值暂存
bit backup=1;//键值备份
unsigned char cnt=0;//按键计数
ENLED=0;
ADDR3=1;
ADDR2=0;
ADDR1=0;
ADDR0=0;
P2=0xF7;
P0=LedChar[cnt];//初始显示0
while(1)//按下时执行一次循环,弹起在执行一次循环
{
keybuf=KEY4; 把当前值扫描暂存
if(keybuf !=backup) //当前值与前一次值不同说明按键有动作
{
delay();
if(keybuf==KEY4) //判断按键值发没发生改变。防止抖动
{
if(backup==1)//如果前次值位1,说明当前动作是按下
{
cnt++;//按键次数加1
if(cnt>=10)
{
cnt=0;
}
P0=LedChar[cnt];
}
}
backup=keybuf; //更新备份,下一次作比较
}
}
}
//弹起
#include <reg52.h>
sbit ADDR0=P1^0;
sbit ADDR1=P1^1;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
sbit KEY1=P2^4;
sbit KEY2=P2^5;
sbit KEY3=P2^6;
sbit KEY4=P2^7;
unsigned char LedChar[]={
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90
};
void delay()
{
unsigned int i=1000;
while(i--);
}
void main()
{
bit keybuf=1;
bit backup=1;
unsigned char cnt=0;
ENLED=0;
ADDR3=1;
ADDR2=0;
ADDR1=0;
ADDR0=0;
P2=0xF7;
P0=LedChar[cnt];
while(1)
{
keybuf=KEY4;
if(keybuf !=backup)
{
delay();
if(keybuf==KEY4)
{
if(backup==0)//只是这里改为0
{
cnt++;
if(cnt>=10)
{
cnt=0;
}
P0=LedChar[cnt];
}
}
backup=keybuf;
}
}
}