473,569 Members | 2,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Drive the matrix keypad by LCD

2 New Member
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
Aug 23 '22 #1
0 4578

Sign in to post your reply or Sign up for a free account.

Similar topics

1
6205
by: RoDzZzZ | last post by:
anyone known how i can get this virtual keypad? access: https://www.secureweb.com.br not is possible copy the keypad virtual full? anyone have one to send for me?
0
1139
by: Sushant Bhatia | last post by:
Hi. I am trying to develop an application that would be able to get keypad key press events even when the focus is not on the application. For instance, if the application is minimized to the system tray, and your using another application, then pressing INSERT would allow my app to get all keypad events. I know about Hotkeys but you...
3
4101
by: Melson | last post by:
Hi May I know is there a way to change the Numeric keypad into mobile phone keypad? Regards Melson
10
6404
by: Petr Jakeš | last post by:
I have a standard 12-key mobile phone keypad connected to my Linux machine as a I2C peripheral. I would like to write a code which allows the text entry to the computer using this keypad (something like T9 on the mobile phones) According to the http://www.yorku.ca/mack/uist01.html dictionary-based disambiguation is coming in the mind. ...
0
1346
by: Claire | last post by:
Ive a modal form that offers message editing for an internal mail system. Its part of a touch screen application. I need to display a keypad form when necessary (some users have no keyboard). My editor is created as modal using frm.ShowDialog(). My keypad is created from the editor form using frm.Show(): My keypad is unable to receive focus....
0
1191
saranjegan
by: saranjegan | last post by:
dudes,i need to access a PC game from a mobile keypad instead of playing via our PC keyboard,the mobile caller may call a particular number its handled by PBX via PBX i can get the input of data from mobile keypad using a serial port connection , now i should implement the key operation by mapping the mobile keypad input with PC Keyboard ,let...
4
3880
by: Dennieku | last post by:
Hi, I have to develop an on-screen keyboard and on-screen numeric keypad for a touchscreen UI. The hardest thing with this is that it has to be multi-lingual. Has anybody have ideas how to handle the strange characters of some languages and how to handle the different number of characters in the alphabet of some languages? I've already...
4
3968
by: sunfeifei | last post by:
Your job is to write a C++ program that reads in a matrix and checks if it has the parity property. If not, your program should check if the parity property can be established by changing only one bit. If this is not possible either, the matrix should be classified as corrupt. 1Stolen from Miguel A. Revilla, Error Correction, http://online-...
10
18181
by: gopishk | last post by:
I have a text box and it can contain only numbers. Similar to a phone number text box while adding a new contact. Programatically how can we open only the numeric keypad instead of both text/numeric keypad. By this the user cannot enter text at anytime. Though validation for symbols can be made later. Can you provide a code snippet for opening...
0
1506
by: mcfly1204 | last post by:
I am looking build an onscreen keyboard similar to the onscreen keyboard included in Windows 7, except I only want the numeric keypad portion. I created a C# form with buttons laid out to mimic a numeric keypad, I have the form set to always be on top, similar to task manager, but I am uncertain how to have an application active, such as Excel,...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.