#include<reg51.h>
#define GPIO_DIG P0
#define GPIO_KEY P3
unsigned char code DIG_CODE[17]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的显示码
unsigned char KeyValue;
//用来存放读取到的键值
void Delay10ms(); //延时10ms
void KeyDown(); //检测按键函数
void main(void)
{
while(1)
{
KeyDown();
GPIO_DIG=~DIG_CODE[KeyValue];
}
}
void KeyDown(void)
{
char a=0;
GPIO_KEY=0xff;
if(GPIO_KEY!=0xff)//读取按键是否按下
{
Delay10ms();//延时10ms进行消抖
if(GPIO_KEY!=0xff)//再次检测键盘是否按下
{
switch(GPIO_KEY)
{
case(0X77): KeyValue=0;break;
case(0X7b): KeyValue=1;break;
case(0X7d): KeyValue=2;break;
case(0X7e): KeyValue=3;break;
case(0Xb7): KeyValue=4;break;
case(0Xbb): KeyValue=5;break;
case(0Xbd): KeyValue=6;break;
case(0Xbe): KeyValue=7;break;
case(0Xd7): KeyValue=8;break;
case(0Xdb): KeyValue=9;break;
case(0Xdd): KeyValue=10;break;
case(0Xde): KeyValue=11;break;
case(0Xe7): KeyValue=12;break;
case(0Xeb): KeyValue=13;break;
case(0Xed): KeyValue=14;break;
case(0Xee): KeyValue=15;break;
}
while((a<50)&&(GPIO_KEY!=0xff)) //检测按键松手检测
{
Delay10ms();
a++;
}
}
}
}
void Delay10ms(void)
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
} |