SCM matrix key and lattice

yangyue12306 注册会员
2023-02-28 02:06

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引脚方向和上下拉电阻

// 初始化点阵显示

// 变量定义
int key_code = -1; // 当前按键编码

// 主循环
while (1) {
    // 检测总开关的状态
    if (!get_switch_input()) { // 如果总开关关闭
        key_code = -1; // 清空上一次按键编码
        continue; // 直接跳过扫

falwyvemmmm 注册会员
2023-02-28 02:06

The following answer is based on ChatGPT and GISer Liu:
Here is a solution using 8051 MCU, including matrix key scanning and LED lattice control.

Suppose that the matrix key uses a 4x4 matrix structure and connects the key to port P0 and port P2, and the lattice uses an 8x8 lattice structure and connects the LED lattice to port P1 and port P3.

① First, we need to define a function to scan the matrix key and return the corresponding value:

unsigned char scan_key(void)
    unsigned char row, col;
    unsigned char key_value = 0;

    // 依次扫描每一行
    for (row = 0; row < 4; row++)
        // 每次只点亮一行,其他行置0
        P0 = ~(1 << row);
        P2 = 0xFF;

        // 检测每一列的状态,如果检测到按键按下,则计算对应数值
        for (col = 0; col < 4; col++)
            if ((P2 & (1 << col)) == 0)
                key_value = row * 4 + col + 1; // 计算对应的数值

        // 如果检测到按键按下,则结束扫描
        if (key_value != 0)

    return key_value;
Then, you need to define a function to display the values on the LED lattice:
unsigned char display_table[16][8] = {
    {0, 126, 129, 129, 126, 0, 0, 0},    // 数值1对应的显示表
    {0, 66, 129, 129, 66, 0, 0, 0},      // 数值2对应的显示表
    // 其他数值对应的显示表
    // ...

void display_number(unsigned char number)
    unsigned char i;
    for (i = 0; i < 8; i++)
        P1 = display_table[number-1][i];
        P3 = ~(1 << i);

Where, display_table is a 16x8 two-dimensional array that stores the LED dot matrix display table corresponding to each value. In the display_number function, the corresponding display table is searched in the display_table according to the current value to be displayed, and then the status of each column is written into the P1 port in turn and the corresponding row is lit.

evilrise 注册会员
2023-02-28 02:06

The answer quote ChatGPT
Lattice display is a two-dimensional lattice structure to display various characters, animations or images, one by one in order to light up or off each bright spot, This constitutes the image of a liquid crystal, LED or LCD display. Combined with the single chip matrix key, can realize the single chip device can control the operation of the key and lattice display, to the user more friendly interface.

duhuafeng2006 注册会员
2023-02-28 02:06

Matrix key Each of the 16 keys must have a symmetric value. It's 0 to 15, right? Or is it the value of the matrix scan? Remember that the scanned values can be rearranged to define any desired values such as the usual 0-15 or any other values such as 1-16. The
LED lattice displays the corresponding results. The values that the matrix is defined for, right? Let's say 0 minus 15. Under
, the code is in the blog, and then get the data of the lattice font.


ctrlqc 注册会员
2023-02-28 02:06

About the Author

Question Info

Publish Time
2023-02-28 02:06
Update Time
2023-02-28 02:06

Related Question




Android Studio形状xml文件-没有透明度的背景


在databricks笔记本中对pandas df的SQL查询结果


Android jetpack撰写TabRow:在TabRow中设置对齐