#define key_input PIND.7 // Key input port
#define key_state_0 0
#define key_state_1 1
#define key_state_2 2
char read_key(void)
{
static char key_state = 0;
char key_press, key_return = 0;
key_press = key_input; // Read key I/O level
switch (key_state)
{
case key_state_0: // Key initial state
if (!key_press) key_state = key_state_1; // Key is pressed, state changes to key confirmation state
break;
case key_state_1: // Key confirmation state
if (!key_press)
{
key_return = 1; // Key is still pressed, key confirmation output is "1"
key_state = key_state_2; // State changes to key release state
}
else
key_state = key_state_0; // Key is released, change to key initial state
break;
case key_state_2:
if (key_press) key_state = key_state_0; // Key is released, change to key initial state
break;
}
return key_return;
}
Another principle is that the execution time of the designed interrupt service function should be as short as possible. In this way, if multiple interrupts are used in the system, even if the interrupt nesting method is not adopted, it can ensure that the system can respond to the requests of each interrupt in time.
Therefore, I like to use only one flag variable in the timer interrupt, so that the interrupt service function has the fastest execution time and will not block the timely response of other interrupts. In the main program, the flag variable is judged and then the key is scanned. Although the scanning interval is not strictly 10ms at this time, there are more or less delays, and the delay is uncertain, but it will not affect the correctness of the key scanning (the process of one key is at least 300ms).
Set a timer interrupt to scan the key every 10MS. , read the value. . If there are multiple keys and extra timers, you can try this method, which is efficient. It is just right for STM32. cool.
Previous article:MCU receiving PT2272 wireless receiving module problem
Next article:MSP430G2553 control program and error handling with interrupt
Recommended ReadingLatest update time:2024-11-16 13:45
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
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
- Two equivalent circuits of capacitors
- What kind of sensor is this?
- RF System Design Guide
- For CC2640R2f watchdog can be like this
- Please tell me the advantages and disadvantages of these two differential signal amplifications
- What is the four-wire resistance measurement? What are the advantages?
- msp430 contains ADC12 module program implementation
- This circuit for detecting mobile phones is no longer usable, right?
- STM32 stepper motor trapezoidal acceleration and deceleration program
- What is the way to distinguish whether series resonance is good or bad?