1. Basic knowledge
1.Key classification and input principle
According to the structural principle, the keys are divided into two categories: one is the contact switch key, such as mechanical switch, conductive rubber switch light; the other is the non-contact switch key, such as electrical key, magnetic induction key, etc. The former is low in cost, and the latter has a long life. At present, the most common type of switch key in the microcomputer system is the contact switch key.
In the single-chip microcomputer application system, except for the reset button which has a dedicated reset circuit and a dedicated reset function, other buttons are used to set control functions or input data in a switch state. When the set function key or number key is pressed, the computer application system should complete the function set by the button. Key information input is a process closely related to the software structure.
For a group of keys or a keyboard, there is always an interface circuit connected to the CPU. The CPU can use query or interrupt mode to know whether a key is input, check which key is pressed, send the key number to the accumulator, and then jump to the function program that executes the key through a jump instruction, and return to the main program after execution.
2.Key structure and features
Microcomputer keyboards usually use mechanical contact key switches, whose main function is to convert mechanical on and off into electrical logical relationships. In other words, it can provide standard TTL logic levels to facilitate the compatibility of logic levels of general digital systems. When a mechanical key is pressed or released again, due to the influence of mechanical elasticity, it is usually accompanied by a certain period of mechanical contact jitter before its contact stabilizes. The jitter process is shown in Figure 1 below. The length of the jitter time is related to the mechanical characteristics of the switch, generally 5-10ms. Detecting the on and off of the key during the contact jitter may lead to misjudgment, that is, a single press or release of the key is mistakenly considered as multiple operations, which is not allowed. In order to overcome the misjudgment caused by the mechanical jitter of the key contact, de-jittering measures must be taken. When there are fewer keys, hardware de-jittering can be used; when there are more keys, software de-jittering can be used.
Figure 1 Mechanical jitter of key contacts
(1)Key coding
A group of keys or keyboards must query the switch status of the keys through the I/O port line. Different encodings are used according to the keyboard structure. Regardless of whether there is encoding or not, and what encoding is used, it must be converted into a key value corresponding to the value in the accumulator in the end to realize the jump of the key function program.
(2) Keyboard Program
A complete keyboard control program should have the following functions:
a. Detect whether any key is pressed and take hardware or software measures to eliminate jitter.
b. Have a reliable logic processing method. Only one key is processed at a time, and any key operation during the process will not affect the system. No matter how long a key is pressed, the system will only execute the key function program once.
c. Accurately output the key value (or key number) to meet the jump instruction requirements.
3. Independent buttons and matrix keyboard
(1) Independent buttons
In a single-chip microcomputer control system, if only a few function keys are required, an independent key structure can be used.
Independent key type is a single key circuit directly constructed by I/O port line. Its characteristics are that each key occupies one I/O port line separately, and the operation of each key will not affect the status of other I/O port lines. The typical application of independent keys is shown in the figure. The independent key circuit has flexible configuration and simple software structure, but each key must occupy one I/O port line. Therefore, when there are many keys, the I/O port line is wasted a lot and it is not suitable to use it. Independent keys are shown in Figure 2.
Figure 2 Independent keyboard
The software for independent buttons often adopts a query structure. First, the input status of the I/O port lines is queried bit by bit. If the input of an I/O port line is low, it can be confirmed that the button corresponding to the I/O port line has been pressed, and then the function processing program of the key is turned to.
(2) Matrix keyboard
In a single-chip microcomputer system, if a large number of keys are used, such as an electronic password lock, a telephone keypad, etc., there are generally at least 12 to 16 keys, and a matrix keyboard is usually used.
Matrix keyboard is also called row-column keyboard. It is a keyboard composed of four I/O lines as row lines and four I/O lines as column lines. A key is set at each intersection of row lines and column lines. In this way, the number of keys on the keyboard is 4*4. This row-column keyboard structure can effectively improve the utilization rate of I/O ports in the single-chip microcomputer system.
How Matrix Keyboard Works
The most common keyboard layout is shown in Figure 3. It is generally composed of 16 keys. In the single-chip microcomputer, one P port can be used to realize the functions of 16 keys. This is also the most commonly used form in the single-chip microcomputer system. The internal circuit of the 4*4 matrix keyboard is shown in Figure 4.
Figure 3 Matrix keyboard layout
Figure 4 Internal circuit diagram of matrix keyboard
When no key is closed, there is an open circuit between P3.0~P3.3 and P3.4~P3.7. When a key is closed, the two I/O port lines connected to the closed key are short-circuited. The method to determine whether a key is pressed is: the first step is to set the column lines P3.4~P3.7 to the input state, output low level from the row lines P3.0~P3.3, read the column line data, if a column line is low level, then a key is closed on the column line. The second step is that the row lines output low level in turn, read data from the column lines P3.4~P3.7, if a column is low level, then a key is pressed on the corresponding row line. Combining the results of the first and second steps, the key number can be determined. However, the key function operation can only be performed once when the key is closed, so it is necessary to wait until the key is released before performing the key function operation, otherwise pressing the key once may cause the same key operation to be performed multiple times in a row.
There are many ways to identify keys, among which the most common method is scanning.
When a key is pressed, the row line and column line connected to this key are connected, and the row line is at a high level when no key is pressed. If all column lines are at a high level, whether a key is pressed or not will not cause the row line level to change, so all column lines must be at a level. In this way, when a key is pressed, the row level where the key is located will change from high to low. Only then can it be determined that a key is pressed in the corresponding row.
The number of independent keys is small, and they can be flexibly encoded according to actual needs. In a matrix keyboard, the position of a key is uniquely determined by the row number and column number, so the row number and column number can be binary encoded separately, and then the two values are combined into a byte, with the upper 4 bits being the row number and the lower 4 bits being the column number.
4. How the keyboard works
The response to the keyboard depends on the working mode of the keyboard. The working mode of the keyboard should be determined according to the working condition of the CPU in the actual application system. The principle of selection is to ensure that the CPU can respond to key operations in time without taking up too much CPU working time. Usually there are three working modes of the keyboard: programmed scanning, timed scanning and interrupt scanning.
(1) Programming scanning method
The program scanning method uses the idle time of the CPU to complete other tasks and calls the keyboard scanning subroutine to respond to keyboard input requirements. When executing the key function program, the CPU will no longer respond to key input requirements until the CPU rescans the keyboard.
(2) Timing scanning mode
The timing scanning mode is to scan the keyboard once every certain period of time. It uses the internal timer of the microcontroller to generate a certain time (for example, 10ms) timing. When the timing is reached, a timer overflow interrupt is generated. After the CPU responds to the interrupt, it scans the keyboard, identifies the key when it is pressed, and then executes the function program of the key.
(3) Interrupt scanning mode
In the above two keyboard scanning modes, the CPU will scan the keyboard regularly regardless of whether a key is pressed or not. However, when the microcontroller application system is working, keyboard input is not often required. Therefore, the CPU is often in an empty scanning state.
In order to improve the CPU working efficiency, the interrupt scanning working mode can be adopted. Its working process is as follows: when no key is pressed, the CPU handles its own work, and when a key is pressed, an interrupt request is generated, and the CPU switches to execute the keyboard scanning subroutine and identify the key number.
Previous article:Really useful! Getting started from 51 to stm32 development
Next article:51 MCU program download, ISP and serial port basics
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
- TI C5000 compiler error message list
- TI Award-winning Live Broadcast: Talking about "packaged antenna" smart millimeter wave sensors, even novices can handle industrial robots
- The voltage of the TPS73033 buck chip drops to 0.7V after the load is connected
- Wi-Fi 6 is here to provide network coverage for the entire home. Will you adopt it?
- Common APDU instruction error codes for CPU cards
- [GigaDevice GD32F310 Review] +ADC Data Collection
- [Mill Edge AI Computing Box FZ5 Review] Development Board Target Classification Demo
- [Domestic RISC-V Linux Board Fang·Starlight VisionFive Trial Report] GPIO Development Basics: From Principle to Practice
- Please ask senior programmers to help look at this problem
- How to use MSP430 to implement PWM signal