|
你可以仔細(xì)觀察一下,你這程序按鍵按下計(jì)一次數(shù),抬起再計(jì)一次數(shù)。核心問題在Key_Value=Key_Push;
給你改了一下,你試試:
#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;
bit x=0;//按鍵標(biāo)志位
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);
return Key_Push;
}
/*
void Button_Proc() //取按鍵值
{
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;
}
}
*/
void Button() //取鍵值,并且記錄按鍵次數(shù)
{
if(Button_Scan()!=Key_Value)
{
delay1ms(10);
if(Button_Scan()!=Key_Value)
{
if(x==0)
{
x=1;//防止重復(fù)計(jì)數(shù)
Key_Times++;
if(Key_Times==8)
Key_Times=0;
// Key_Value=Key_Push;
// Button_Proc();
switch(Button_Scan())//取鍵值,3個(gè)鍵共組合有8個(gè)狀態(tài),去除0x07可以產(chǎn)生7個(gè)鍵值
{
case 0x00: Key=0; break;
case 0x01: Key=1; break;
case 0x02: Key=2; break;
case 0x03: Key=3; break;
case 0x04: Key=4; break;
case 0x05: Key=5; break;
case 0x06: Key=6; break;
// case 0x07: Key=7; break;
}
}
}
}
else x=0;
}
void main()
{
while(1)
{
Button();
P3=Key_Times;
}
}
|
|