Arduino与Proteus仿真实例-4x4矩阵键盘驱动仿真

4x4矩阵键盘驱动仿真

键盘是广泛用于各种电子和嵌入式项目的输入设备。 它们用于以数字和字母的形式获取输入,并将其输入系统以进行进一步处理。

矩阵键盘由一组相互连接的按钮组成。 在本次实例中使用 4X4 矩阵键盘,其中四行中的每一行都有 4 个按钮。 按钮端子按下图所示示连接。 在第一行,所有4个按钮的一个端子连接在一起,4个按钮的另一个端子代表4列中的每一列,每行也是如此。 所以一共有 8 根线来连接一个微控制器。

在前面的实例中,对矩阵键盘驱动做了详细的介绍,请参考:

1、仿真电路原理图

在这里插入图片描述

2、仿真代码实现

本次实例使用了如下开源库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "Adafruit\_Keypad.h"
#define R1 2
#define R2 3
#define R3 4
#define R4 5
#define C1 8
#define C2 9
#define C3 10
#define C4 11
// define your specific keypad here via PID
const byte ROWS = 4; // rows
const byte COLS = 4; // columns
// define the symbols on the buttons of the keypads
char keys[ROWS][COLS] = {{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'\*', '0', '#', 'D'}};
byte rowPins[ROWS] = {R1, R2, R3,
R4}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {C1, C2, C3,
C4}; // connect to the column pinouts of the keypad
// define your pins here
// can ignore ones that don't apply


//initialize an instance of class NewKeypad
Adafruit_Keypad customKeypad = Adafruit\_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
Serial.begin(9600);
customKeypad.begin();
}

void loop() {
// put your main code here, to run repeatedly:
customKeypad.tick();

while(customKeypad.available()){
keypadEvent e = customKeypad.read();
Serial.print((char)e.bit.KEY);
if(e.bit.EVENT == KEY_JUST_PRESSED) Serial.println(" pressed");
else if(e.bit.EVENT == KEY_JUST_RELEASED) Serial.println(" released");
}

delay(10);
}

3、仿真结果

在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/121092773