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 Create new folders and projects
Lets start by creating a new folder named Code. Within this new folder, create two new folders named C51 and GUI.
The program of this tutorial will be based on the code of chapter 2 hello world, so copy the code file of that tutorial into the new C51 folder.
Copy the prepared background image from the data folder to the GUI folder.
Open DGUS, set the same resolution as the screen, and set the saving path.
Import the background image. Open DGUS tool, select ICL tool, import the background image, and click Generate ICL.
Name it as 32.icl.
Import the icon files in the Data folder, click Generate ICL, name it 35.ICL, and save it in the DWIN_SET folder of the GUI folder.
Next, select the variable icon control and set the vp to 2000 (vp should be in the range of 0x1000~0xFFFF according to your preference)
Select 35 icon file. Set minimum and maximum and their icon IDs.
Select Data variable control. Also set vp to 2000, consistent with the variable icon control. Set the font size to 32. Note that the variable type should be set to unsigned int(2 bytes).
Click save and generate.
Copy all the configuration files to the DWIN_SET folder of the SD card.
Put the SD card into the screen slot and power the screen on.
After the burning is finished, pull out the SD card and re-power the screen.
The project had been successfully burnt into the screen.
4 Write 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 reason why such a sequencing method is used is that the position of the keys is fixed, but the surface pattern can be changed, and the sequencing method according to the tutorial can be applied to a variety of applications. So the tutorial does not follow the patterns to set the key number.
Next, declare the function.
Then go back to the key.c file.
Next, write the implementation of the key_scan function.
This is the relatively important part.
First, define the default value of KEY as key_none.
Return the key value.
Then the next can write row scan and column scan, here for your convenience to understand, I first write column scan.
The function key_set_sacna_s ta(key_scan_sta _col); is used here.
After the column scan, we should determine whether a column has been pressed, that is, to determine whether the four pins C1-4 shows a low voltage.
By macro definition to read the value of the row pins and the mask for the row pins, (#define ROW_PIN_VAL (KEY_PORT&ROW_P IN_MASK))
The same is done to define the value of the column pins.
Then to determine, if the value of the column pins differs from the mask of the column pins, then a column is pressed.
Note that a static variable is also defined here to mark whether a key has been pressed or not.
Here you can keep the else first.
Then add another if function to avoid the effect of continuous press overlay. Here you need to de-jitter, about ten milliseconds delay, in order to avoid the effect of a false press.
So again to determine, at this point, if the condition is still met, then there is indeed a key pressed.
Next, determine which column has been pressed.
Using switch to pass the value of the column pin, and when the value of the column pin = 0xE0, which is 1110 000, the fourth bit of 0 means the first column was pressed.
Then make the key value equal to 1 first.
Copy this part, because there are four columns in total.
When the value of the column pins = 0xD0, 0xB0 or 0x70, respectively, it means that columns 2, 3 or 4 are pressed.
Next, write the row scan to locate exactly which key was pressed.
Again use key_set_sacna_s ta(key_scan_sta _ROW).
Copy the switch part. Change it to (switch(ROW_PIN _VAL)).
Now look at the value of the lower four bits only. This value is set to E\D\B\7, respectively.
The values of this key are key += 0; key += 4; 8; 12 ......
This variable should also be copied and set to 1.
When no key is pressed, this variable is set to 0
When the key is pressed, we have to wait for a few memory cycles, and then read the IO pin value after it stabilizes.
So we delay 1us.
This way there is no bug in the program.
Main.c
Now call the function.
First remove the description and the delay of 1 second and num+1.
Now use this headfile to drive and then the first key value key
To determine if key is true, it means that a key is pressed. Then we can display this key in the screen.
Now we have completed the code section, click build.
4 Effect
Open download for 8051, download the generated hex file, and you can see the graphics displayed on the screen.
Thanks for watching