Hello, everyone.
Today I will teach you to drive the matrix keypad by DWIN T5L ASIC.
1 The internal diagram of the matrix keyboard.
First of all, let me introduce the matrix keyboard used in this tutorial.
There are sixteen keys on the matrix keyboard, in the order of the first key number is 1, the remaining keys are 2, 3, 4, 5, 6, 7, 8. The number of key B is 8.
When the key number 1 is pressed, C1 and R1 are connected.
So how do you know that the key is pressed? I will introduce two concepts: row scan and column scan.
Row scan to determine which row is in the pressed state by the code, and the same for column scan.
For row scanning, set R1, R2, R3, and R4 as inputs, while set
C1, C2, C3, and C4 to output low voltage.
Then when reading the voltage of the IO pins of the four rows, if it reads low, it means that a key is pressed in this line.

2 Matrix keyboard wiring diagram.
The eight pins on the matrix keyboard are connected to the P1.0-P1.7 of the DWIN screen, via Dupont lines.
You may wonder why use P1 port?
That is because the pins of P1 port are together and it is convenient to write the driver.
Note that eight pull-up resistors with a resistance of 10K ohms need to be connected.
3.C51 code
First, open the sys.c file, we need to add a wait command to prevent bugs that can occur in special cases.
Then, we open the HARDWARE folder and create a new folder named KEY.
Open the new folder and create two new text documents named key.h and key.c.
Next, use Keil to open the key.c file, and then add the search path of the headfile in C51.
Open the headfile, enter the template content, and add the system headfile.
According to the principle of row scan and column scan to determine the key state, we first set up a function to scan the key state.
Then we define the two states of this function: row scan state and column scan state.
Then we define the 8 pins of the 4*4 matrix keyboard.

The DWIN screen used in this tutorial is EKT043B, and the eight pins P1.0-P1.7 need to be soldered with pull-up resistors, and I have soldered the resistors here.
Define the keypad port as P1 here.
Then we will write the function to scan the key state. First, let's judge that when it belongs to row scan, the row pins should be set as input and the column pin should be set as output low voltage at the same time; similarly settings for column scan.
For more details to write the input code, please refer to the DWIN ASIC Development Guide.
The lower four bits are set to input, and the upper four bits output low voltage. (P1MDOUT = 0xF0;)
Then set the high four bits output low. (KEY_PORT = 0x0F)
Because we will use this value later, we define 0xF0 as the mask for the row pins and define 0x0F as the mask for the column pins. This value should be bitwise NOT (using ~ instead of !).

Copy this code for the column scan. At this time, set the column as input and the row as output low voltage.
Now we are done setting the scan state.

Next, write a function that scans the matrix keys for key values.
The return value is reserved and named as key first. this represents which numbered key was pressed.

Then open the headfile to assert the KEY.
Define KEY_NONE as none of the keys were pressed.
KEY_1 means that the No.1 key was pressed.
There are 16 keys in total, so write down all the keys in order.
Next, I will open the picture of the matrix keyboard to explain the arrangement of key numbers.
The number of key 1 is 1.
234, which means that the number of key A is 4. Then 5678, in the order of left to right and top to bottom, then the number of D is 16.
......

(The article is not complete due to word limit, refer to the url for video tutorial)