作者:人对方啥地位 | 来源:互联网 | 2023-10-12 12:33
#include #include "../delay/delay.h"
#include "matrix.h"
/*=====================================================
按键扫描函数,返回扫描键值
======================================================*/
unsigned char key_scan(void)
{
unsigned char keyvalue; KEYPORT = 0xf0; //高四位置高,低四位拉低
if(0xf0 != keyvalue)
{
delay_ms(10); //去抖
if(0xf0 != keyvalue) //有按键按下
{
KEYPORT = 0xfe; //检测第一行
if(0xfe != KEYPORT)
{
keyvalue = KEYPORT;
keyvalue &= 0xf0; //过滤干扰
keyvalue += 0x0e; //过滤干扰
while(0xfe != KEYPORT);
delay_ms(10); //去抖
while(0xfe != KEYPORT);
return keyvalue;
}
KEYPORT = 0xfd; //检测第二行
if(0xfd != KEYPORT)
{
keyvalue = KEYPORT;
keyvalue &= 0xf0; //过滤干扰
keyvalue += 0x0d; //过滤干扰
while(0xfd != KEYPORT);
delay_ms(10); //去抖
while(0xfd != KEYPORT);
return keyvalue;
}
KEYPORT = 0xfb; //检测第三行
if(0xfb != KEYPORT)
{
keyvalue = KEYPORT;
keyvalue &= 0xf0; //过滤干扰
keyvalue += 0x0b; //过滤干扰
while(0xfb != KEYPORT);
delay_ms(10); //去抖
while(0xfb != KEYPORT);
return keyvalue;
}
KEYPORT = 0xf7; //检测第四行
if(0xf7 != KEYPORT)
{
keyvalue = KEYPORT;
keyvalue &= 0xf0; //过滤干扰
keyvalue += 0x07; //过滤干扰
while(0xf7 != KEYPORT);
delay_ms(10); //去抖
while(0xf7 != KEYPORT);
return keyvalue;
}
}
}
return 0xff;
}
/*=====================================================
按键值处理函数,返回扫描键值
======================================================*/
unsigned char key_pro(void)
{
unsigned char keynum;
keynum = key_scan();
/*按下相应的键显示相对应的码值*/
switch(keynum)
{
case 0x7e:
{
return 0; //0
break;
}
case 0xbe:
{
return 1; //1
break;
}
case 0xde:
{
return 2; //2
break;
}
case 0xee:
{
return 3; //3
break;
}
case 0x7d:
{
return 4; //4
break;
}
case 0xbd:
{
return 5; //5
break;
}
case 0xdd:
{
return 6; //6
break;
}
case 0xed:
{
return 7; //7
break;
}
case 0x7b:
{
return 8; //8
break;
}
case 0xbb:
{
return 9; //9
break;
}
case 0xdb:
{
return 10; //10
break;
}
case 0xeb:
{
return 11; //11
break;
}
case 0x77:
{
return 12; //12
break;
}
case 0xb7:
{
return 13; //13
break;
}
case 0xd7:
{
return 14; //14
break;
}
case 0xe7:
{
return 15; //15
break;
}
default:
{
return 0xff;
break;
}
}
}