Realization of Short Press and Long Press Function of Single-Chip Button Based on State Machine

1 Introduction

In my own research practice and teaching to students, the methods for key processing in MCU learning are: detecting buttons, delaying debounce, button execution, waiting for release, and a large number of "delay_1ms" appear in the entire button processing program. ";" and "while (!key);" such a statement, this is obviously asking for trouble, delay debounce and waiting to release need to consume CPU runtime, execution efficiency, and the keystroke program handled by this method It also can not realize the flexible button operation mode such as short press and long press of the button. This paper describes a method of realizing key scan using state machine, which achieves the short press and long press function of the button, and effectively handles the problem of button debounce.

2, state machine

The state machine is very important in software programming. A clear and efficient program must have a state machine figure. For example, in the key command parsing program, it is originally in state 1, after triggering a button, switching to state 2, then triggering another button to switch to state 3, or returning to state 1. The keystroke process of the button is also a state switch, or it can be viewed as a state machine. The keystroke process of a button includes: pressing, shaking, closing, shaking, and releasing. We only need to express the ideas of these state machines in a program.

3, the state machine of the button is implemented

We use the state machine here to solve the problem, then we have to think from the problem itself. In order to realize the key scan, the function of short press and long press of the button can be achieved, and the button can be divided into four states according to the whole process of pressing a button to releasing the button:

S0: Wait for the button to be pressed

S1: button press

S2: Wait for the button to press and release

S3: Wait for the button to press and release

Assume that the button is pressed low for “0” and the button is not pressed for “1”. The whole process of the button can be expressed by the state transition diagram, as shown in Figure 1.

Realization of Short Press and Long Press Function of Single-Chip Button Based on State Machine

Figure 1: State transition diagram of the button

First, the initial state of the button is S0. When it is detected that the input is 1, it indicates that the button is not pressed, and keeps S0; when the input is 0, it indicates that the button is pressed, and the state is shifted to S1. In the S1 state, it is detected whether the input signal is 0. If it is 0, the key program is executed to shift to S2; if it is 1, it indicates that the previous key operation is an interference signal, and returns to S0. In the S2 state, if the input signal is 1, it returns to S0, indicating that the button short press has been released; if the button is not released, the input is 0, the timer starts, and the timer is not finished before the end is at S2, when the timing is over, Transfer to S3, indicating that the button is pressed all the time, for long press function, during the S2 timing, the input changes from 0 to 1 will also return to S0. In the S3 state, the input signal is 1, returning to S0, indicating that the button is pressed and released; the input signal is 0, the corresponding button program is executed, and the time can be counted, and the button program is executed after the timer is finished, and the button combo function is reached. This is the whole process of using the state machine to perform button detection to achieve short press and long press.

Type1 Tethered Cable

Type1 Tethered Cable,16A Type1 Tethered Cable,32A Type1 Tethered Cable,Type 1 Tethered Charging Cable

Yangzhou JERI New Energy Co., Ltd. , https://www.jrevcharging.com