To achieve the MCU matrix key and lattice operation, can be carried out in accordance with the following steps:
Connect hardware: Arrange the 16 keys according to the 4x4 matrix, and connect to the GPIO pins of the single chip microcomputer. Connect the dot matrix display to the GPIO pin of the single chip microcomputer and set the pin mode according to its data manual.
Initialization: In the MCU code, you first need to initialize the orientation of the GPIO pin and the pull-down resistance. Matrix keys generally need to be set to input mode, and LED dot matrix needs to be set to output mode.
Cyclic scanning matrix key: In the main loop, you can use the cyclic scanning method to read the status of the matrix key. You can use a dual loop, with a first layer loop controlling rows and a second layer loop controlling columns. When a key is pressed, the corresponding key code can be calculated based on its row and column number and stored in a variable.
Check the master switch: In the main loop, the state of the master switch needs to be checked before scanning the matrix key. If the main switch is not on, there should be no response when either matrix key is pressed. A separate GPIO pin can be used to control the state of the master switch and to clear the variable encoded by the last key when the master switch is detected to be off.
Generates symmetric values: After scanning keys, you can generate symmetric values based on their row and column numbers. For example, if you press the key in row 0, column 0, then the symmetric key is the key in row 3, column 3, and their encoding should be the same. You can use mathematical formulas to calculate symmetric row and column numbers.
Display results to the lattice: In the main loop, conditional statements can be used to determine what should be displayed on the lattice based on the key code read in the previous step. Bit arithmetic and array indexing can be used to control the state of each LED in the lattice. Finally, the data of the control lattice is sent to the GPIO pin of the lattice controller to display the result.
Here is a simple pseudo-code example to demonstrate the implementation of the above steps:
// 初始化GPIO引脚方向和上下拉电阻
init_gpio();
// 初始化点阵显示
init_led_matrix();
// 变量定义
int key_code = -1; // 当前按键编码
// 主循环
while (1) {
// 检测总开关的状态
if (!get_switch_input()) { // 如果总开关关闭
key_code = -1; // 清空上一次按键编码
continue; // 直接跳过扫