#include<reg51.h>
#define KEY P2
unsigned char KeyValue;
unsigned int i;
void Delay10ms(unsigned int c); //延时10ms
void KeyDown(); //检测按键函数
void main(void)
{
while(1)
{ KeyDown();
if(KeyValue==15)
{P0=0xc0;
i=1;
}
if(i==1)
{ KeyDown();
switch(KeyValue)
{
case(1): P0=0XF9;i=0;break;
case(2): P0=0XA4;i=0;break;
case(3): P0=0XB0;i=0;break;
case(4): P0=0X99;i=0;break;
case(5): P0=0X92;i=0;break;
case(6): P0=0X82;i=0;break;
}
}
}
}
void KeyDown(void)
{ KEY=0x0f;
if(KEY!=0x0f)//读取按键是否按下
{ Delay10ms(1);//延时10ms进行消抖
if(KEY!=0x0f)//再次检测键盘是否按下
{ KEY=0X0F; //测试列
switch(KEY)
{ case(0X07): KeyValue=0;break;
case(0X0b): KeyValue=1;break;
case(0X0d): KeyValue=2;break;
case(0X0e): KeyValue=3;break;
}
KEY=0XF0; //测试行
switch(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(KEY!=0xf0); //按键松手检测
}
}
}
void Delay10ms(unsigned int c) //误差 0us
{
unsigned char a, b;
for (;c>0;c--)
{
for (b=38;b>0;b--)
{
for (a=130;a>0;a--);
}
}
}
|