Matrix keyboard interface technology and program design
When there are many buttons in the keyboard of the single-chip computer system, in order to reduce the occupation of the I/O port, the buttons are often arranged in a matrix form, as shown in Figure 1. In a matrix keyboard, each horizontal line and vertical line are not directly connected at the intersection, but are connected through a button. In this way, one port (such as P1 port) can form 4*4=16 buttons, which is twice as many as directly using the port line for the keyboard, and the more lines there are, the more obvious the difference is. For example, adding one more line can form a 20-key keyboard, while directly using the port line can only add one more key (9 keys). It can be seen that when the number of keys required is relatively large, it is reasonable to use the matrix method to make the keyboard.
The matrix keyboard is obviously more complicated than the direct method, and the recognition is also more complicated. In the figure above, the column line is connected to the positive power supply through a resistor, and the I/O port of the microcontroller connected to the row line is used as the output end, while the I/O port connected to the column line is used as the input. In this way, when the button is not pressed, all output ends are high level, indicating that no key is pressed. The row line output is low level. Once a key is pressed, the input line will be pulled low. In this way, by reading the state of the input line, it can be known whether a key is pressed. The specific recognition and programming methods are described as follows.
Button identification method of matrix keyboard
A "row scanning method" is introduced to determine which key is pressed on a matrix keyboard.
Row scanning method Row scanning method is also called row (or column) scanning query method. It is one of the most commonly used button recognition methods. As shown in the keyboard above, the process is introduced as follows.
To determine whether a key is pressed on the keyboard, set all row lines Y0-Y3 to low level, and then detect the status of the column lines. As long as the level of one column is low, it means that a key is pressed on the keyboard, and the closed key is located in the 4 buttons where the low-level line intersects with the 4 row lines. If all column lines are high, no key is pressed on the keyboard.
After confirming that a key is pressed, the process of determining the specific closed key can be started. The method is: set the row lines to low level in sequence, that is, when a row line is set to low level, the other lines are high level. After determining that a row line is at a low level, the level state of each column line is detected row by row. If a column is low, the button at the intersection of the column line and the row line set to low level is the closed button.
Here is a specific example:
The diagram is still as shown above. The P1 port of the 8031 microcontroller is used as the keyboard I/O port. The column line of the keyboard is connected to the lower 4 bits of the P1 port, and the row line of the keyboard is connected to the upper 4 bits of the P1 port. The column lines P1.0-P1.3 are respectively connected to the positive power supply +5V with 4 pull-up resistors, and the column lines P1.0-P1.3 are set as input lines, and the row lines P1.4-P.17 are set as output lines. The 4 row lines and the 4 column lines form 16 intersections.
Detect whether any key is currently pressed. The detection method is to output all "0" from P1.4-P1.7, read the status of P1.0-P1.3, if P1.0-P1.3 are all "1", no key is closed, otherwise a key is closed.
Remove key jitter. When a key is detected to be pressed, a delay is made before the next step of detection and judgment.
If a key is pressed, it should be identified which key is closed. The method is to scan the rows of the keyboard. P1.4-P1.7 outputs in the following 4 combinations:
P1.7 1 1 1 0
P1.6 1 1 0 1
P1.5 1 0 1 1
P1.4 0 1 1 1
Read P1.0-P1.3 when each group of rows is output. If all are "1", it means that the row "0" has no key closure, otherwise the key is closed. The row value and column value of the closed key are obtained, and then the row value and column value of the closed key can be converted into the defined key value by calculation or table lookup.
In order to ensure that the CPU only processes once each time the key is closed, the jitter when the key is released must be eliminated.
"MCU Matrix Keyboard Interface Technology and Programming"
Keyboard Scanner:
The flowchart of the MCU keyboard scanning program obtained from the above analysis is shown in Figure 2. The program is as follows
SCAN: MOV P1,#0FH
MOV A,P1
ANL A,#0FH
CJNE A,#0FH,NEXT1
SJMP NEXT3
NEXT1: ACALL D20MS
MOV A,#0EFH
NEXT2: MOV R1,A
MOV P1,A
MOV A,P1
ANL A,#0FH
CJNE A,#0FH,KCODE;
MOV A,R1
SETB C
RLC A
JC NEXT2
NEXT3: MOV R0,#00H
RIGHT
KCODE: MOV B,#0FBH
NEXT4: RRC A
INC B
JC NEXT4
MOV A,R1
SWAP A
NEXT5: RRC A
INC B
INC B
INC B
INC B
JC NEXT5
NEXT6: MOV A,P1
ANL A,#0FH
CJNE A,#0FH,NEXT6
MOV R0,#0FFH
RIGHT
This is just a brief introduction to the keyboard handler. In fact, keyboard and display processing are very complex. They generally account for most of the code of an application, which shows its importance. However, this complexity does not come from the microcontroller itself, but from the operator's habits and other issues. Therefore, before writing the keyboard handler, it is best to sort it out logically first, then express it with an appropriate algorithm, and finally write the code. In this way, the code can be written quickly and effectively.
Previous article:51 MCU Tutorial from Scratch—— 26 MCU Keyboard Interface Programming
Next article:51 MCU tutorial from scratch - some basic concepts about MCU
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Sub-library: Summary of official Chinese technical documents of accelerometers
- Re-study the comparator of MSP430
- [RTT & Renesas high performance CPK-RA6M4] 6. Software simulation I2C driver PCF8574 evaluation
- LPC1768 information release
- Design of high-speed communication system between FPGA and DSP based on SRIO
- Filter capacitor value after bridge rectification
- Please look at this circuit, why is the output signal like this, the DC operating point is fine
- [GD32E503 Evaluation] mig29_Step1: Summary of development environment construction issues
- Mobile station development board CC26X2R1 LaunchPad recommended!
- Embedded System Basics: What is a Microcontroller MCU?