2020-03-21/矩阵键盘
2020-03-21 本文已影响0人
LH大牛
矩阵键盘
//矩阵键盘,采用行列扫描
#include<reg51.h>
#define gpio_key P1
#define smg P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
unsigned char num [16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char value;//为什么不能用static在按键函数内部定义
void delay(int i)
{
while(i--);
}
void key(void)
{
//static unsigned char value;
int a=0;
gpio_key=0x0f;//按键不按下,都是高电位,所以用0f判断,而不是f0;
if(gpio_key!=0x0f)
{
delay(1000);//消抖
if(gpio_key!=0x0f)
{ switch(gpio_key)
{
case(0x07): value=4;break;
case(0x0b): value=3;break;
case(0x0d): value=2;break;
case(0x0e): value=1;break;
}
gpio_key=0xf0;
if(gpio_key!=0xf0)
{
switch(gpio_key)
{
case(0x70): value;break;
case(0xb0): value+=4;break;
case(0xd0): value+=8;break;
case(0xe0): value+=12;break;
}
}
while((a<50)&&(gpio_key!=0x0f))
{
delay(1000);
a++;
}
}
}
}
int main()
{
while(1){
LSA=0;
LSB=0;
LSC=0;
key();
smg=num[value];
}
return 0;
}