CT107D竞赛板矩阵键盘的应用
重点
- j5的连接
- 电路变化
- 矩阵按键原理
- 矩阵键盘扫描原理
-
j5的连接
接1,2端口, -
电路变化
原理图中
P3^7 ==P4^4
P3^6 ==P4^2
早期的STC89C52或无定义P4口的
需定义P4口:
sfr P4 =0xc0; 0xc0是端口地址 -
矩阵按键原理
上方为矩阵按键原理图
独立按键与矩阵键盘不同的是左侧的I/O口是接地的
当左边I/O口是低电平时 若按键未按下,右边的I/O口为高电平(接了VCC)
当左边I/O口是低电平时,若按键按下 ,读取右边的I/O口为低电平
通过这样的方式,可读到按键是否按下,这就是矩阵按键的原理 -
矩阵键盘扫描原理
C端为读取端 R端为输出端
R1=0
R2 = R3 = R4 =1
C口全为1
R2=0
R1 = R3 = R4 =1
C口全为1
R3=0
R1 = R2 = R4 =1
C口全为1
R4=0
R1 = R3 = R2 =1
C口全为1
像这样依次扫描 在板子的快速运行下
可达到同时扫描的效果
这样就可识别哪个键被按下
基础实现代码 看不懂的点这矩阵键盘教学
include<STC15F2K60S2.H>
sbit R1 = P3^0;//定义R口
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;
sbit C4 = P3^4;//定义C口
sbit C3 = P3^5;
sbit C2 = P4^2;
sbit C1 = P4^4;
unsigned char code SMG_duanma[18]=//数码管数字数组
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,
0xbf,0x7f};
void SelectHC573(unsigned char channel)//译码器选择
{
switch(channel)
{
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
}
}
void Daley (unsigned char t)//延迟
{
while(t--);
}
void DisplayKeyNum(unsigned char value)
{
SelectHC573(6);//打开Y6 com端
P0 =0x01;
SelectHC573(7);//打开Y7 段码
P0 = value;
}
unsigned char key_num;
void ScanKeysMulti ()
{
R1 = 0;
R2 = R3 = R4 = 1;
C1 = C2 = C3 = C4 =1;
if(C1 == 0)
{
Daley(500);
while(C1 == 0);
key_num = 0;
DisplayKeyNum(SMG_duanma[key_num]);
}
else if(C2 == 0)
{
Daley(500);
while(C2 == 0);
key_num = 1;
DisplayKeyNum(SMG_duanma[key_num]);
}
else if(C3 == 0)
{
Daley(500);
while(C3 == 0);
key_num = 2;
DisplayKeyNum(SMG_duanma[key_num]);
}
else if(C4 == 0)
{
Daley(500);
while(C4 == 0);
key_num = 3;
DisplayKeyNum(SMG_duanma[key_num]);
}
R2 = 0;
R1 = R3 = R4 = 1;
C1 = C2 = C3 = C4 =1;
if(C1 == 0)
{
Daley(500);
while(C1 == 0);
key_num = 4;
DisplayKeyNum(SMG_duanma[key_num]);
}
else if(C2 == 0)
{
Daley(500);
while(C2 == 0);
key_num = 5;
DisplayKeyNum(SMG_duanma[key_num]);
}
else if(C3 == 0)
{
Daley(500);
while(C3 == 0);
key_num = 6;
DisplayKeyNum(SMG_duanma[key_num]);
}
else if(C4 == 0)
{
Daley(500);
while(C4 == 0);
key_num = 7;
DisplayKeyNum(SMG_duanma[key_num]);
}
R3 = 0;
R2 = R1 = R4 = 1;
C1 = C2 = C3 = C4 =1;
if(C1 == 0)
{
Daley(500);
while(C1 == 0);
key_num = 8;
DisplayKeyNum(SMG_duanma[key_num]);
}
else if(C2 == 0)
{
Daley(500);
while(C2 == 0);
key_num = 9;
DisplayKeyNum(SMG_duanma[key_num]);
}
else if(C3 == 0)
{
Daley(500);
while(C3 == 0);
key_num = 10;
DisplayKeyNum(SMG_duanma[key_num]);
}
else if(C4 == 0)
{
Daley(500);
while(C4 == 0);
key_num = 11;
DisplayKeyNum(SMG_duanma[key_num]);
}
R4 = 0;
R2 = R3 = R1 = 1;
C1 = C2 = C3 = C4 =1;
if(C1 == 0)
{
Daley(500);
while(C1 == 0);
key_num = 12;
DisplayKeyNum(SMG_duanma[key_num]);
}
else if(C2 == 0)
{
Daley(500);
while(C2 == 0);
key_num = 13;
DisplayKeyNum(SMG_duanma[key_num]);
}
else if(C3 == 0)
{
Daley(500);
while(C3 == 0);
key_num = 14;
DisplayKeyNum(SMG_duanma[key_num]);
}
else if(C4 == 0)
{
Daley(500);
while(C4 == 0);
key_num = 15;
DisplayKeyNum(SMG_duanma[key_num]);
}
}
void duzzdown ()
{
SelectHC573(5);
P0 =0x00;
SelectHC573(4);
P0 =0xff;
}
void main ()
{
duzzdown ();
while(1)
{
ScanKeysMulti ();
}
}