#include <reg52.h>
#define GPIO_KEY P1 //矩阵键盘的接口
#define GPIO_DIG P0
typedef unsigned char u8;
u8 KEY;
sbit SM = P3^1; //输入密码按键
sbit GM = P3^0; //修改密码按键
sbit OK = P3^2; //确认键
sbit LSA = P2^2;
sbit LSB = P2^3;
sbit LSC = P2^4;
u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//显示0~F的值
void delay_ms(unsigned int x)
{
unsigned int i,j;
for(i = 0;i < x; i++)
for(j = 0; j < 110; j++);
}
u8 Keyscan()
{
char a = 0;
u8 KEY;
GPIO_KEY = 0x0f;
if(GPIO_KEY != 0x0f) //判断按键是否有按下
{
delay_ms(10); //延时去抖
if(GPIO_KEY != 0x0f) //再次判断是否有按下
{
//扫描列
GPIO_KEY = 0x0f;
switch(GPIO_KEY)
{
case(0x07):KEY = 0;break;
case(0x0b):KEY = 1;break;
case(0x0d):KEY = 2;break;
case(0x0e):KEY = 3;break;
}
//扫描行
GPIO_KEY = 0xf0;
switch(GPIO_KEY)
{
case(0x70):KEY = KEY;break;
case(0xb0):KEY = KEY+4;break;
case(0xd0):KEY = KEY+8;break;
case(0xe0):KEY = KEY+12;break;
}
while((a<50) && (GPIO_KEY != 0xf0)) //按键松开检查
{
delay_ms(10);
a++;
}
return KEY;
}
}
}
void Keyscan_duli()
{
if(GM == 0)
{
delay_ms(10);
if(GM == 0)
{
KEY = 0x0b;
}
while(!GM);
}
else if(SM == 0)
{
delay_ms(10);
if(SM == 0)
{
KEY = 0x0a;
}
while(!SM);
}
else if(OK == 0)
{
delay_ms(10);
if(OK == 0)
{
KEY = 0x0c;
}
while(!OK);
}
}
void main()
{
LSA = 0;
LSB = 0;
LSC = 0;
while(1)
{
Keyscan_duli();
KEY = Keyscan();
GPIO_DIG = smgduan[KEY];
}
}
想问下各位大佬,为什么不管按GM、SM、OK键数码管都显示A,而如果只有独立键盘扫描函数,即把主函数中的KEY=Keyscan()注释掉,按GM、SM、OK键就能分别显示A、B、C。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>