#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define GPIO_KEY P1
uchar keynum = 0;
uchar KeyValue;
bit flag = 0;
uchar code ascii[]=
{0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
/*4*4矩阵键盘按键扫描*/
void KeyDown()
{
char a=0;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)//读取按键是否按下
{
delay(10);//延时10ms进行消抖
if(GPIO_KEY!=0x0f)//再次检测键盘是否按下
{
//测试列
GPIO_KEY=0X0F;
switch(GPIO_KEY)
{
case(0X07): KeyValue=0;break;
case(0X0b): KeyValue=1;break;
case(0X0d): KeyValue=2;break;
case(0X0e): KeyValue=3;break;
}
//测试行
GPIO_KEY=0XF0;
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue;break;
case(0Xb0): KeyValue=KeyValue+4;break;
case(0Xd0): KeyValue=KeyValue+8;break;
case(0Xe0): KeyValue=KeyValue+12;break;
}
while((a<50)&&(GPIO_KEY!=0xf0)) //检测按键松手检测
{
delay(10);
a++;
}
}
}
}
void main() //对应键盘键值显示 *=14 #=15
{
EA = 1;
EX0 = 1;
IT0 = 1;
while(1)
{
GPIO_KEY=0x0f;
if(flag==1)
{
KeyDown();
keynum = LED[KeyValue];
flag = 0;
}
}
}
void interrupt_0() interrupt 0
{
EX0 = 0;
flag = 1;
EX0 = 1;
} |