1. Principle Introduction
The keyboard interface circuit is a very important part of the single-chip system design. As the most commonly used input device in the human-computer interaction interface, we can input data or commands through the keyboard to achieve simple human-computer communication. Before designing the keyboard circuit and program, we need to understand some knowledge about the keyboard and the keys that make up the keyboard.
1. Classification of buttons
Generally speaking, buttons can be divided into two categories according to their structural principles. One is contact switch buttons, such as mechanical switches, conductive rubber switches, etc.; the other is non-contact switch buttons, such as electrical buttons, magnetic induction buttons, etc. The former is low in cost, and the latter has a long life. At present, the most common type of switch button in the microcomputer system is the contact switch button (such as the button used in this learning board).
According to the interface principle, keys can be divided into two categories: coded keyboards and non-coded keyboards. The main difference between these two types of keyboards is the method of identifying key symbols and giving corresponding key codes. Coded keyboards mainly use hardware to realize key recognition, while non-coded keyboards mainly use software to realize keyboard recognition.
The fully coded keyboard uses a special chip to recognize keys and output the corresponding codes. It generally has de-jitter and multi-key and key-channel protection circuits. This keyboard is easy to use, but has high hardware costs. It is rarely used in general small embedded application systems. Non-coded keyboards can be divided into independent and matrix types according to the connection method. Other work is mainly completed by software. Because it is economical and practical, it is widely used in single-chip microcomputer systems (this learning board also uses non-coded keyboards).
2. Key input principle
In the single-chip microcomputer application system, mechanical contact key switches are usually used, and their main function is to convert mechanical on and off into electrical logical relationships. In other words, it can provide standard TTL logic levels to be compatible with the logic levels of general digital systems. In addition, 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 switch states. When the set function key or number key is pressed, the computer application system should complete the function set by the key. Therefore, key information input is a process closely related to the software structure. For a group of keys or a keyboard, it is connected to the single-chip microcomputer through an interface circuit. The single-chip microcomputer can use query or interrupt mode to understand whether there is key input and check which key is pressed. If a key is pressed, it jumps to the corresponding keyboard processing program to execute, and if no key is pressed, it continues to execute other programs.
3. Key features and debounce
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 the contact stabilizes. The jitter process is shown in Figure 1 (a). The length of the jitter time is related to the mechanical characteristics of the switch, generally 5 to 10 ms. As can be seen from the figure, detecting the on and off state of the key during the contact jitter may lead to misjudgment. That is, a single press or release of the key is mistakenly regarded as multiple operations, which is not allowed. In order to overcome the misjudgment caused by the mechanical jitter of the key contact, de-jitter measures must be taken, which can be considered from both hardware and software aspects. Generally speaking, when the number of keys is small, hardware de-jitter can be used, and when the number of keys is large, software de-jitter can be used. (This learning board uses software de-jitter). The flowchart of software de-jitter is shown in Figure 1 (b).
Figure 1
From the debounce flow chart of the key, we can know that when a key is detected to be pressed, it should be delayed for a period of time (a 5ms~10ms delay subroutine can be called), and then it is judged again whether the key is pressed. If it is judged that the key is still pressed at this time, it is considered that the key is valid. If it is judged that the key is not pressed at this time, it means that the key is jittering or interfering, and it should return to re-judgment. The corresponding processing procedure can only be performed when the keyboard is actually pressed. At this time, the key input is basically realized, and further, it can be judged whether the key is released. [page]
2. Circuit Detail
The circuit diagram is shown in Figure 2.
Figure 2
As shown in Figure 2, the independent key adopts a structure in which each key occupies an I/O port line. When the key is pressed and released, the level input to the microcontroller I/O port is different, so it is possible to determine whether a key is pressed and which key is pressed based on the changes in the levels of different ports. As can be seen from Figure 2 (a), the key and the microcontroller pin are connected and a pull-up resistor is added, so that when no key is pressed, the I/O input level is high, and when a key is pressed, the I/O input level is low.
Although the independent key circuit configuration is flexible and the software structure is simple, each key must occupy an I/O port line. Therefore, when there are many keys, the I/O port line is wasted. For more complex systems or occasions with more keys, a matrix keyboard can be used. The one shown in Figure 2 (b) is a 4×4 matrix keyboard. The design methods of other matrix keyboards are similar.
The 4×4 matrix keyboard is composed of 4 row lines and 4 column lines intersecting, and the keys are located at the intersection of the rows and columns, thus forming 16 keys. The row and column lines at the intersection are not connected. When the key is pressed, the row and column lines at this intersection are turned on. Figure 2 (b) The row line is connected to VCC through a pull-up resistor. When no key is pressed, the row line is in a high level state; when a key is pressed, the row and column lines are turned on at the intersection. At this time, the row line level will be determined by the column line level connected to this row line. This is the key to identifying whether the key is pressed. However, each row line in the matrix keyboard intersects with 4 column lines. Whether the key at the intersection is pressed or not affects the level of the row and column lines where the key is located. The keys will affect each other. When analyzing the key, the row and column line signals must be combined and properly processed to determine the position of the closed key.
It is worth noting that the matrix keyboard introduced in this article adds a four-input AND gate chip 74HC21 to the output end of the traditional matrix keyboard. When one of the four inputs is low, the output is low. Connect the output end of 74HC21 to the external interrupt 0 (P32 pin) of the microcontroller. In this way, when the real-time requirement is high, set P00~P03 to all low to wait for the key trigger. When any key is pressed, the system will enter the interrupt service program, which improves the keyboard response time and is very practical when the system real-time requirement is high. The full source code of this article can be found at www.ele169.com.
3. Programming
The key procedures of the design example in this article are as follows.
Independent key program
…
#define keyio P0 (1)
#define key1 P0_3 (2)
…
keyio|=0X0F; (3)
if (key1 == 0) (4)
{
delay_nms(20); (5)
if (key1 == 0) (6)
{
while(key1==0); (7)
return 1; (8)
}
}
[page]
Program Description:
(1) Define the button pins.
(2) Define the button connection pins.
(3) Connect the button to the pin to output a high level, thereby receiving input.
(4) If the button connected to the pin is pressed at this time.
(5) Delay for a period of time to de-bounce the operation.
(6) If the button is still pressed, it is valid.
(7) Wait for the key to be released, an infinite loop. If the key is pressed, the waiting process continues.
(8) Return the key value. Matrix keyboard program
…
#define KEYIO P0 (1)
…
code ksp[4]={0x7F,0xBF,0xDF,0xEF}; (2)
unsigned char keypad_scan() (3)
{
char key,i; (4)
KEYIO=0xF0; (5)
if (KEYIO!=0xF0) (6)
{
for (i=0;i<=3;i++) (7)
{
delayKey(10); (8)
KEYIO=ksp[i]; (9)
delayKey(10); (10)
if (KEYIO!=ksp[i]) (11)
{
delayKey(10); (12)
key=KEYIO; (13)
while(KEYIO==key); (14)
return (key); (15)
}
}
}
}
[page]
Program Description:
(1) Define the matrix keyboard pins.
(2) Define the four output level states of the pins used during scanning as an array.
(3) Key scanning program.
(4) Define two temporary variables key and i.
(5) Let the upper four bits of the keyboard pin output high level and the fourth bit be low level, ready for scanning keys.
(6) If the pin status level changes at this time.
(7) Assign the values in the previously defined array to the pins and start scanning one by one.
(8) Delay for a period of time to eliminate jitter.
(9) Output the scan button voltage level.
(10) Delay for a while longer.
(11) If the key pin level is still not the default output level at this time, it means that a key is pressed.
(12) Delay for a while to allow the voltage level to stabilize.
(13) Read the current key pin level, that is, the key value.
(14) Wait for the key to be released, an infinite loop. If the key is pressed, the waiting process continues.
(15) Return the key value.
4. Debugging points and experimental phenomena
After the hardware is connected, download the hex file generated by the program to the MCU through cold start, open the serial port debugging assistant software, set the baud rate to 9600, reset the MCU, and then press any of the 4×4 buttons on the board, and pay attention to the display on the serial port debugging assistant. (See Figure 3), you can observe that the data of the key is displayed in the receiving window.
Figure 3 Pressing the button to display the interface through the serial port debugging assistant
In addition, in the experimental program attached to this article, the send character function and the send string function are called in the serial communication. When there is no simulator and some prompt information needs to be displayed, the serial port printing method can be used, which is not only intuitive and convenient but also does not increase other costs.
V. Conclusion
This article introduces the working principle of the single-chip external keyboard and gives an example. Through this article, we can know that a complete keyboard control program should have the following functions:
(1) Detect whether a key is pressed and take hardware or software measures to eliminate the impact of mechanical contact jitter of keyboard keys.
(2) There is a reliable logic processing method. Only one key is processed at a time. The operation of any key during this period will not affect the system. No matter how long a key is pressed, the system will only execute the key function program once.
(3) Accurately output the key value (or key number) to meet the key function requirements. For a matrix keyboard, the row and column line signals must be combined and properly processed to determine the position of the closed key.
In addition, there are many ways to scan the keys. This article describes the program scanning method. Other common methods include the timing scanning method and the interrupt scanning method. These methods can all be implemented on this learning board. Therefore, readers are expected to combine the knowledge of the previous lectures to write and debug the program by themselves. The next lecture will describe the principle and example of the microcontroller dynamically driving the digital tube, so stay tuned.
Previous article:Design of signal generator based on single chip microcomputer and MAX038
Next article:Learn 51 MCU from me (V): MCU dynamic scanning drive digital tube
- Popular Resources
- Popular amplifiers
- Enter the Matrix MS-DOS screensaver.Sample of using bios functions (by int 10h) for text data out
- Multi-channel pressure measuring instrument based on C8051F020 single chip microcomputer
- Design of automatic player for work and rest signal based on 51 single chip microcomputer
- AVR243 Matrix Keyboard Decoder
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
- Brain-electromechanical control robot arm (robot arm for elderly care and disabled assistance)
- [NXP Rapid IoT Review] + Mobile Synchronizer 5
- EEWORLD University Hall----Live Replay: Detailed Explanation of Ultra-Low Power RSL10 Bluetooth SoC Development Board
- 51 single chip digital tube display
- EEWORLD University ---- Training series for BQ76952 & BQ76942, 3-16S & 3-10S battery monit
- How to deploy Wi-Fi connectivity for grid protection and control?
- Common knowledge of inductors
- Three design challenges that the new generation of SimpleLink Wi-Fi devices will help you solve
- How to use the internal LDO of CC1310
- 【TGF4042 Signal Generator】+ Actual Project Application