1. Basic supplement
1. Keyboard tasks
(1) Determine whether a key is pressed. If yes, proceed to the next step.
(2) Identify which key is pressed and find the corresponding key value.
(3) Based on the key value, find the handler entry for the corresponding key value.
2. Keyboard recognition
Whether the key is closed or not is reflected in the high or low level of the row line output voltage. The microcontroller can confirm whether the key is pressed or released by detecting the high and low states of the row line level. In order to ensure that the microcontroller only confirms that the key is valid once for a key action (the so-called key validity means that after pressing the key, it must be released), the influence of the jitter period t1 and t3 must be eliminated.
3. How to eliminate key jitter
(1) Use software delay to eliminate key jitter. The basic idea is: when a key is detected to be pressed, the row line corresponding to the key is at a low level. After executing a subroutine with a delay of 10ms, check whether the row line level is still at a low level. If it is still at a low level, it is confirmed that a key has been pressed in the row. When the key is released, the low level of the row line becomes a high level. After executing a subroutine with a delay of 10ms, the row line is detected to be at a high level, indicating that the key has been released.
(2) Use a dedicated keyboard/display interface chip, which has an automatic de-jitter hardware circuit.
4. Non-encoded keyboard and encoded keyboard
(1) A non-encoded keyboard means that when you press a key, the key number information cannot be obtained directly, but must be obtained through software. There are two common structures of non-encoded keyboards: independent keyboards and matrix keyboards.
(2) An encoded keyboard is one that can directly obtain the key number of a key when it is pressed, for example using a dedicated keyboard interface chip.
5. Scanning method of non-encoded keyboard
(1) Query scan:
When the MCU is idle, the keyboard scanning subroutine is called to scan the keyboard repeatedly to respond to the keyboard input request. If the query frequency of the MCU is too high, although it can respond to the keyboard input in time, it will also affect the progress of other tasks. If the query frequency is too low, the keyboard input may be missed. Therefore, the keyboard scanning frequency should be adjusted according to the busyness of the MCU system and the operation frequency of the keyboard.
(2) Scheduled scanning:
The microcontroller can scan the keyboard once at a certain interval, which is called timed scanning. This method usually uses the timer interrupt generated by the timer in the microcontroller to scan the keyboard after entering the interrupt subroutine. When a key is pressed, it identifies the pressed key and executes the corresponding key handler. Since the time for each key press is generally not less than 100ms, in order not to miss a valid key press, the period of the timed interrupt is generally less than 100ms.
(3) Interrupt scanning:
In order to further improve the efficiency of the MCU scanning keyboard, the interrupt scanning method can be used, that is, the keyboard will send an interrupt request signal to the MCU only when a key is pressed. The MCU responds to the interrupt, executes the keyboard scanning interrupt service subroutine, identifies the pressed key, and jumps to the processing program of the key. If no key is pressed, the MCU will ignore the keyboard. The advantage of this method is that it will only be processed when a key is pressed, so it has strong real-time performance and high work efficiency.
(ii) Independent keyboard
1. Schematic diagram of independent keyboard
The characteristic of a stand-alone keyboard is that each key is independent of each other, and each key is connected to an I/O port line. By detecting the level state of the I/O port input line, it is easy to determine which key is pressed.
2. Independent keyboard K1 controls LED1 code implementation
#include sbit led1=P2^0; //because led1 is controlled by port p2^0 sbit k1=P3^1; //The output level of port P31 is controlled by key k1 void delay(int i) { while(i--); } void keyproc() { if(k1==0) { delay(1000); //delay to eliminate jitter if(k1==0) { led1=~led1; } while(!k1) ; } } void main() { while(1) { keyproc(); } } (III) Matrix keyboard 1. Schematic diagram of matrix keyboard Matrix (also known as row-column) keyboards are usually used in situations where there are a large number of keys. It consists of row lines and column lines, and the keys are located at the intersection of rows and columns. Its interface circuit is shown in the figure above. 2. The matrix keyboard corresponds to the digital tube output 0 to code implementation #include "reg52.h" typedef unsigned char uchar; typedef unsigned int uint; #define GPIO_DIG P0 #define GPIO_KEY P1 uchar code smgduan[16]= {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71}; //Static digital tube code value uint KeyColValue; uint KeyLineValue; void delay(uint i) //delay function { while(i --); } void KeyDown() //Keyboard key scanning function { char a; GPIO_KEY = 0x0f; if(GPIO_KEY != 0x0f) // Check which of the 4 rows is pressed { delay(1000); //delay to eliminate jitter if(GPIO_KEY != 0x0f) //Check again which of the 4 rows is pressed { switch(GPIO_KEY) //Determine which row of keys are pressed based on the IO value { case(0x07): KeyColValue = 0; break; case(0x0b): KeyColValue = 1; break; case(0x0d): KeyColValue = 2; break; case(0x0e): KeyColValue = 3; break; } } } GPIO_KEY = 0xf0; if(GPIO_KEY != 0xf0) //Check whether a key in the 4 rows is pressed { delay(1000); //delay to eliminate jitter if(GPIO_KEY != 0xf0) //Check again which column of keys in the 4 rows is pressed { switch(GPIO_KEY) //Determine which column of keys is pressed based on the IO value { case(0x70): KeyLineValue = 0; break; case(0xb0): KeyLineValue = 1; break; case(0xd0): KeyLineValue = 2; break; case(0xe0): KeyLineValue = 3; break; } } while((a < 50) && (GPIO_KEY != 0xf0)) // Delay to ensure no key is pressed { delay(1000); a++; } } } void main() { while(1) { KeyDown(); //Check if the key is pressed GPIO_DIG = smgduan[KeyLineValue*4 + KeyColValue]; //According to the row and column values of the keys, the static digital tube displays the corresponding values } }
Previous article:51 MCU Study Notes Interrupt
Next article:Make the 51 single-chip eight-segment digital tube light up
- Popular Resources
- Popular amplifiers
- USB Complete The Developer\'s Guide 5ed (USB Development Complete 5th Edition)
- 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
- Common terms for E5515 instruments and CMU200 meters
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Prize-winning quiz | Bringing life to buildings ADI Building Smoke Detection Technology
- In a bus communication system, how to automatically assign addresses to slaves.
- Power Management Usage Trends
- What parameters should I look at when selecting a TVS? Can anyone tell me?
- How to become an IC design expert? (Repost)
- Are flow and reflow the same thing?
- What every electronics engineer should know about oscilloscopes
- Hot Must-Read丨20 Questions and Answers from Data Converters (ADI)
- Agilent E3632A Repair
- 【Qinheng RISC-V core CH582】 1 Unpacking test and development environment installation