#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar Key_Push,Key_Value=7,Key_Times=0,Key;
sbit K1 = P1^0;
sbit K2 = P1^1;
sbit K3 = P1^2;
void delay1ms(uint x)
{
uchar i;
while(x--)
for(i=0;i<125;i++)
;
}
uchar Button_Scan()
{
Key_Push=0x00;
Key_Push|=K3;
Key_Push <<= 1;
Key_Push|=K2;
Key_Push <<= 1;
Key_Push|= K1;
return(Key_Push^Key_Value);
}
void Button_Proc() //取按键值
{
EA = 0;
if((Key_Value&0x01)==0) // K1按下,取键值1
{
Key=1;
}
else if((Key_Value&0x02)==0) // K2按下,取键值2
{
Key=2;
}
else if((Key_Value&0x04)==0) // K3按下,取键值3
{
Key=3;
}
EA = 1;
}
void Button() //取键值,并且记录按键次数
{
if(Button_Scan())
{
delay1ms(10);
if(Button_Scan())
{
Key_Times++;
if(Key_Times==8)
Key_Times=0;
Key_Value=Key_Push;
Button_Proc();
}
}
}
void main()
{
while(1)
{
Button();
P3=Key_Times;
}
}
按键扫描程序调试过程当中,发现Key_Times怎么每次加2,没有找出问题!
|