Key detection ideas of state machine 2
Let's review the definition first:
the state machine has the following properties
The conditions for state transition of the key state machine
How to accurately detect whether the conditions occur and perform state transition
Ensure that the state machine can migrate stably
The principle of key debounce: state switching
The timing of key operation trigger
Let's review the definition first:
the key from not pressed to pressed, and then to bounced, can be regarded as a finite state machine with 4 states. They are not pressed, front edge jitter, pressed, and trailing edge jitter.
That is, the key only migrates between these 4 states, and when the conditions are met, it migrates in a certain order: Not pressed (condition 1) > front edge jitter (condition 2) > pressed (condition 3) > trailing edge jitter (condition 4) > not pressed When and only when the conditions are met, the state transition is performed. When the conditions are not met, the state machine will remain in the current state unchanged.
The state machine has the following properties
1. The state machine will transfer the state when the conditions are met. Since each state will last for a period of time, and we need the key to be ideal, that is, we need a momentary key event, not a continuous key event.
Summary:
(1) When the state transition condition is met, the state transition must be performed.
(2) The state transition is instantaneous, so we use a state transition as a key.
(3) When no state transition is performed, the state machine maintains the previous state.
The condition for the key state machine to transition
is how to accurately determine the transition condition of the state machine.
How to accurately detect whether the condition occurs and perform state transition
Note: The initial state of the key is not pressed
// Initialize the key state to not pressed during definition
unsigned char KEY_Status = NOPRESS;
(1) Condition 1: The leading edge jitter starts
from the unpressed state to the leading edge jitter start event, and the level changes from 1 to 0,
that is, in the unpressed state, a low level is suddenly detected, which means that the transition condition is met, and the state is immediately migrated to the leading edge jitter, or other operations are performed.
//KEY_Status == NOPRESS && KEY_Value == 0, equivalent to condition 1
if (NOPRESS == KEY_Status && 0 == KEY_Value)
{
KEY_Status = PRESHAKE; //Execute state transition
timer(); //Set a 10ms timer and use the timer overflow event to simulate the front edge jitter end event.
}
(2) Condition 2: Front edge jitter end
It takes about 10 milliseconds from the front edge jitter state to the front edge jitter end event. Although there is no specific event, the timer interrupt can be used to simulate the front edge jitter end event. In the front edge jitter state, once the timer interrupts, it means that the transition condition is met, and the state is immediately transitioned to pressed, or other operations are performed.
//If PRESHAKE == KEY_Status && 1 == TF1, condition 2 is true
if (PRESHAKE == KEY_Status && 1 == TF1)
{
KEY_Status = PRESS; //Execute status migration
TF = 0; // Clear overflow flag
TR1 = 0; // Turn off the timer
}
(3) Condition 3: Trailing edge jitter starts. When
the event from the pressed state to the trailing edge jitter starts occurs, the level changes from 0 to 1. That is, as long as the level is detected as 1 in the pressed state, the migration condition is considered to be met, and the state is immediately migrated to trailing edge jitter.
//KEY_Status == PRESS && KEY_Value == 1, then condition 3 is established
if (PRESS == KEY_Status && 1 == KEY_Value)
{
KEY_Status = TAILSHAKE; //Execute status migration
n++; //Perform the desired operation
timer(); //Use timer overflow to simulate the end event of trailing edge jitter
}
(4) Condition 4: End of back-edge jitter
Although there is no specific event, the duration from the back-edge jitter state to the end of the back-edge jitter event is certain, which is basically about 10ms. The end of the back-edge jitter event can be simulated by the timer overflow. Once the timer overflows (or the timer interrupt occurs), it means that the migration condition is met, and the state is immediately migrated to not pressed.
//TF1 == 1 is the end of the back-edge jitter flag.
//The timer should be turned off at this time to prevent repeated judgments.
if (1 == TF1)
{
KEY_Status = NOPRESS;
TF1 = 0; //Clear overflow flag
TR1 = 0; //Turn off the timer
}
Ensure that the state machine can migrate stably
If the state can migrate stably, that is, if we can accurately judge the conditions for the state migration,
the state machine will achieve stable migration. Other operations can be performed on this basis without worrying about the state definition errors of the state machine, because the state machine can migrate stably.
Therefore, the key is to accurately determine the conditions for state migration so that the states do not cross, that is, stable migration.
We need to repeatedly check whether the above definition of the state machine migration conditions is accurate. Once we are sure it is accurate, we can do something else based on it.
You can simulate the execution of the program in your mind to verify whether the idea is correct.
The principle of key debounce:
In the state switching (key) state machine, since the state switching is carried out by judging the conditions, once the conditions are met, the state is switched instantly. Each state will last for a period of time. During this period, the loop may be executed many times, but since the state switching conditions are not met, it will not enter the if statement. So if any state switches to the next state as a key event, then the key will only be detected once. This is the principle of key debounce. The actual test
of the timing of key operation triggering
found that placing the response operation to the key event at the beginning of the trailing edge jitter (that is, the moment when the state machine
migrates to the trailing edge jitter state) has a good effect, and it will not appear too sensitive or too slow.
Previous article:51 MCU timer output PWM routine
Next article:Programming to implement single chip microcomputer ds18b20 temperature measurement and display system
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- CC3200 Kit - OURS-SDK-WFB Getting Started
- Initial understanding of R329 development board - system
- BearPi-HM Nano 3 + Analysis of source code writing process under vscode
- STC15W104 power-off wake-up can not be done, please help
- Shift register based frequency divider
- DSP C6000 Architecture
- Application of millimeter wave radar technology in barriers
- (Bonus 3) GD32L233 Review - Unboxing and showing off the big box
- CPLD
- Help, after the load power supply has been working for a period of time, the microcontroller will shut down automatically