目录
1矩阵键盘的识别
2相关c语言
3实践编程
1矩阵键盘的识别
假设按列扫描按下S6P30:0P34:1然后高流向低,P34:0,刚开始是0xf0:1111 0000
后面是0xe0:1110 0000 ,当是0xe0能确定是第一列被按下,再按行扫描,S6:0x0e
综合:0xee
2相关c语言
略
3实践编程
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit DU = P2^6;
sbit WE = P2^7;
uchar num;//数码管的值
uchar KeyValue = 20;//按键值,初始值是-
//code固定table的值,code很大8k,ram区小
//共阴数码管段选值
uchar code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71,0x76,0x38,0x37,0x3e,0x40,0x00};
void delay(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void KeyScan()
{
//4*4
//列扫描
P3=0xf0;
if(P3!=0xf0)
{
//消抖
delay(10);
if(P3!=0)
{
switch(P3)//判断哪一列
{
case 0xe0: KeyValue = 0; break;
case 0xd0: KeyValue = 1; break;
case 0xb0: KeyValue = 2; break;
case 0x70: KeyValue = 3; break;
}
//行扫描
P3=0x0f;
switch(P3)//判断哪一行
{
case 0x0e: KeyValue = KeyValue; break;
case 0x0d: KeyValue = KeyValue + 4; break;
case 0x0b: KeyValue = KeyValue + 8; break;
case 0x07: KeyValue = KeyValue + 12; break;
}
//松手(当我们松手后,电流无法流到地,通过上拉电阻回到初始状态
while(P3 != 0x0f);
}
//独立按键
P3=0xff;
if(P3!=0xff)
{
delay(10);
if(P3!=0xff)
{
switch(P3)//判断哪一行
{
case 0xfe: KeyValue = 16; break;
case 0xfd: KeyValue = 17; break;
case 0xfb: KeyValue = 18; break;
case 0xf7: KeyValue = 19; break;
}
//松手(当我们松手后,电流无法流到地,通过上拉电阻回到初始状态
while(P3 != 0xff);
}
}
}
}
}
void main()
{
WE = 1;
P0=0xfb;//位选给0
WE = 0;//锁存器锁存
while(1)
{
KeyScan();
DU = 1;
P0 = table[KeyValue];//0000 0110 共阴极给1亮
DU = 0;
}
}