【蓝桥杯单片机06】矩阵键盘的基本控制
【蓝桥杯单片机06】矩阵键盘的基本控制
广东职业技术学院 欧浩源
掌握了独立按键的处理思路之后,也不是那么轻易的能够学会矩阵键盘的,两者的处理思路有很大的不同,不过,只要把矩阵键盘的本质思想搞清楚了,不过是4X4的还是8X8的,都能够很轻松的处理。
1、矩阵键盘的扫描思想
与独立按键不同的是,按键的两个引脚都分别连接的单片机的I/O端口,一个作为行信号,另外一个作为列信号。我们以4X4的矩阵键盘为例,试着探讨其工作方式和扫描思路。
在上面的矩阵键盘中,要识别出黄色按键的按下状态,应该怎么做呢?
对与矩阵键盘,我们只能逐行扫描,然后读取列的状态信号。如果R3行输出低电平,那么黄色按键如果有按下动作的话,那读取C2列信号也应该为低电平,而该行上其他没有按下动作的按键的列信号则为高电平。因此,我们可以得到矩阵键盘的基本扫描步骤:
<1> R1输出点电平,R2、R3、R4输出高电平,逐个读取判断列信号,如果都为高电平则R1行上没有按键按下。
<2> R2输出点电平,R1、R3、R4输出高电平,逐个读取判断列信号。
<3> R3输出点电平,R1、R2、R4输出高电平,发现C2列信号为低电平,那么可以判断得R3行的C2列的按键有按下动作。
<4> R4输出点电平,R1、R3、R4输出高电平,逐个读取判断列信号。
如此循环往复,扫描的按键的状态。
我们知道有按键按下动作,那么又怎么知道是哪一个按键按下呢?这时,我们最好定义一个键值全局变量,给矩阵行列上的每一个的按键编一个唯一的号码。当扫描的某一行某一列的按键动作后,把对应的编号复制给键值变量,这样我们判断这个键值,就知道是那个按键有触发动作了。
说得再多,不如一练,看题。
2、单元训练题目
3、实现源码参考
#include "reg52.h" sfr P4 = 0xC0; sbit HC138_A = P2^5; sbit HC138_B = P2^6; sbit HC138_C = P2^7; sbit R1 = P3^0; sbit R2 = P3^1; sbit R3 = P3^2; sbit R4 = P3^3; sbit C1 = P4^4; sbit C2 = P4^2; sbit C3 = P3^5; sbit C4 = P3^4; unsigned char keyNum = 0x10; unsigned char code SMG_NoDot[18] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e, 0xbf,0x7f}; void Delay(unsigned char t) { while(t--); } void Init74HC138(unsigned char n) { switch(n) { case 4: HC138_A = 0; HC138_B = 0; HC138_C = 1; break; case 5: HC138_A = 1; HC138_B = 0; HC138_C = 1; break; case 6: HC138_A = 0; HC138_B = 1; HC138_C = 1; break; case 7: HC138_A = 1; HC138_B = 1; HC138_C = 1; break; case 8: HC138_A = 0; HC138_B = 0; HC138_C = 0; break; } } void ShowKeyNum(unsigned char value) { Init74HC138(6); P0 = 0x01; Init74HC138(7); P0 = value; } void ScanKeys() { keyNum = 16; R1 = 0; R2 = R3 = R4 = 1; C1 = C2 = C3 = C4 = 1; if(C1 == 0) { while(C1 == 0); keyNum = 0; ShowKeyNum(SMG_NoDot[keyNum]); } else if(C2 == 0) { while(C2 == 0); keyNum = 1; ShowKeyNum(SMG_NoDot[keyNum]); } else if(C3 == 0) { while(C3 == 0); keyNum = 2; ShowKeyNum(SMG_NoDot[keyNum]); } else if(C4 == 0) { while(C4 == 0); keyNum = 3; ShowKeyNum(SMG_NoDot[keyNum]); } R2 = 0; R1 = R3 = R4 = 1; C1 = C2 = C3 = C4 = 1; if(C1 == 0) { while(C1 == 0); keyNum = 4; ShowKeyNum(SMG_NoDot[keyNum]); } else if(C2 == 0) { while(C2 == 0); keyNum = 5; ShowKeyNum(SMG_NoDot[keyNum]); } else if(C3 == 0) { while(C3 == 0); keyNum = 6; ShowKeyNum(SMG_NoDot[keyNum]); } else if(C4 == 0) { while(C4 == 0); keyNum = 7; ShowKeyNum(SMG_NoDot[keyNum]); } R3 = 0; R2 = R1 = R4 = 1; C1 = C2 = C3 = C4 = 1; if(C1 == 0) { while(C1 == 0); keyNum = 8; ShowKeyNum(SMG_NoDot[keyNum]); } else if(C2 == 0) { while(C2 == 0); keyNum = 9; ShowKeyNum(SMG_NoDot[keyNum]); } else if(C3 == 0) { while(C3 == 0); keyNum = 10; ShowKeyNum(SMG_NoDot[keyNum]); } else if(C4 == 0) { while(C4 == 0); keyNum = 11; ShowKeyNum(SMG_NoDot[keyNum]); } R4 = 0; R2 = R3 = R1 = 1; C1 = C2 = C3 = C4 = 1; if(C1 == 0) { while(C1 == 0); keyNum = 12; ShowKeyNum(SMG_NoDot[keyNum]); } else if(C2 == 0) { while(C2 == 0); keyNum = 13; ShowKeyNum(SMG_NoDot[keyNum]); } else if(C3 == 0) { while(C3 == 0); keyNum = 14; ShowKeyNum(SMG_NoDot[keyNum]); } else if(C4 == 0) { while(C4 == 0); keyNum = 15; ShowKeyNum(SMG_NoDot[keyNum]); } } void main() { while(1) { ScanKeys(); } }