I came up with this program when I was writing a calculator program. I always felt that the commonly used keyboard scanning program was troublesome to use. It always had to scan line by line, and it looked very cumbersome. So I came up with a matrix keyboard scanning program. The principle is very simple!
The basic idea of this program is that two intersecting straight lines have only one intersection point. To put it simply, it detects the rows first and then the columns, or vice versa.
This keyboard scanning subroutine is a parameterless function with a return value, so you just need to calculate an array beforehand!
The following is the implementation of the sub-function. I only use a 3*4 matrix keyboard as an example here. The principle is the same! [page]
==============================================================================
uchar code key_table[]=
{
0x77,
0x7b,
0x7d
, 0xb7 ,
0xbb
, 0xbd
,
0xd7, 0xdb,
0xdd,
0xe7,
0xeb,
0xed
};
/*The above array must be calculated in advance. You can get the value corresponding to the corresponding key by connecting the row and column values together*/
unsigned char key_scan(void )
{
unsigned char i, key_code; /*Variable declaration*/
key_port = 0x0f; /*Here key_port is a GPIO port defined in the previous macro.
First set the row (or column) */
if (key_port ! = 0x0f) /* If a key is pressed, the initial value of GPIO will change,
The following statements will be executed*/
{
delay(5); /*The delay here is to achieve debounce, which will not be introduced here*/
if(key_port != 0x0f) /*Confirm that a key is really pressed*/
{
key_code = key_port; /*Assign the value of the row (or column) to key_code*/
key_port = 0xf0; /*Then set the column (or row)*/
key_code |= key_port; /*Combine the row and column values and assign them to key_code*/
for(i = 0; i < 12; i++) /*Because there is only one key_code corresponding to each key pressed,
Therefore, according to the number of keys on the matrix keyboard, the corresponding FOR loop can be used to determine which key is pressed*/
{
if(key_table[i] == key_code)
{
key = i+1;
break;
}
}
}
}
else key = 0;
return key; /*The returned KEY value is the key at the corresponding position*/
}
Previous article:Use of common LCD screen LCD1602
Next article:Design of digital clock based on stc89c52rc microcontroller (digital tube display)
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- 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
Guess you like
- The first article GD32VF103C START development board unboxing running lights
- How to eliminate the influence of gravity acceleration on accelerometer
- MSP430 programmer and emulator
- MSP430 FLL - Frequency Locked Loop
- 530M power supply quality resources one-click download, limited time free points
- How to obtain a package in an existing PCB diagram
- msp430 library serial keyboard
- PSIM simulation software oscilloscope setting problem + measurement problem, how to set up the oscilloscope to see the output waveform?
- Design RF circuit boards, you need to know these tips!
- Talk to your boss about a pay raise