The idea of ​​independent key detection and debounce of 51 single-chip microcomputer based on state machine

Publisher:诗意世界Latest update time:2020-01-21 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

0.png 

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.

  1. //If PRESHAKE == KEY_Status && 1 == TF1, condition 2 is true

  2. if (PRESHAKE == KEY_Status && 1 == TF1)

  3. {

  4. KEY_Status = PRESS; //Execute status migration

  5. TF = 0; // Clear overflow flag

  6. TR1 = 0; // Turn off the timer

  7. }


(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.

  1. //KEY_Status == PRESS && KEY_Value == 1, then condition 3 is established

  2. if (PRESS == KEY_Status && 1 == KEY_Value)

  3. {

  4. KEY_Status = TAILSHAKE; //Execute status migration

  5. n++; //Perform the desired operation

  6. timer(); //Use timer overflow to simulate the end event of trailing edge jitter

  7. }


(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.


Reference address:The idea of ​​independent key detection and debounce of 51 single-chip microcomputer based on state machine

Previous article:51 MCU timer output PWM routine
Next article:Programming to implement single chip microcomputer ds18b20 temperature measurement and display system

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号