A key scanning debounce method taking into account real-time performance
By using counters instead of delay in key scanning and combining the minimum time slice of task cycle, the impact of traditional delay debounce on real-time and consistency is solved, and stable and flexible key scanning in high real-time occasions are achieved .
Patent Information
- Application Number
- CN202411360901.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-27
- Publication Date
- 2024-12-10
- Estimated Expiration
- 2044-09-27
AI Technical Summary
在实时性要求较高的场合,传统按键扫描消抖方法中的延时会对程序循环的实时性和时间一致性造成影响,尤其是在最小时间片小于1ms时,延时消抖会导致时间 不确定性。
Use a button high-level counter and a low-level counter, combined with the minimum time slice of the task cycle, and replace delay by counting the value to realize de-jittering of the button state, and set a flexible scanning method at the rising and falling edges to avoid delays. time on real-time and consistency.
Effectively avoid the impact of delay on real-time and consistency, ensuring the stability and flexibility of the key scanning function under high real-time requirements, especially in the high-speed communication detection within the minimum time chip, it is not affected by delay. .
Smart Images

Figure CN118890035B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of key input processing of electronic equipment, and in particular to a key scanning debouncing method taking into account real-time performance. Background Art
[0002] Key scanning generally requires anti-shake processing, and a certain delay is required after the signal change is detected to make a de-shake judgment. However, in situations where real-time requirements are high, the de-shake delay is unacceptable.
[0003] When the real-time requirements of the debouncing program loop are high, such as the minimum time slice of a program loop is only 1ms or less, delayed debouncing will bring great time uncertainty to the program loop time, thereby affecting the real-time performance of the program loop and the time consistency of each cycle.
[0004] Based on this background, the present invention designs a key scanning solution that can set a delay range and ensure real-time performance, and also includes configurable rising edge and falling edge triggered scanning modes. Summary of the invention
[0005] The purpose of the present invention is to provide a key scanning debouncing method that takes into account real-time performance. The method can solve the problem that in some special cases where high requirements are placed on the real-time performance and consistency of the program cycle, delayed debouncing will affect the real-time performance and time consistency of the program cycle.
[0006] A key scanning debouncing method taking into account real-time performance is provided, wherein a key high level counter and a key low level counter are set, and a minimum time slice of a task cycle is also set. The minimum time slice is the time for executing a task cycle once. A task cycle includes the following steps:
[0007] SK. Start the loop;
[0008] S0. Detect the key state. If the key state is a high level, the key high level counter increases by 1 and transfers to S11, S12, and S13. If it is a low level, the key low level counter increases by 1 and transfers to S21, S22, and S23.
[0009] S11. Compare the key high level counter value with the debounce delay count value. If it is greater than the debounce delay count value, change the key status bit and set the key low level counter to 0. Whether it is greater than, less than or equal to the debounce delay count value, transfer to SJ.
[0010] S12. Compare the key high level counter value with the maximum limit count value. If it is greater than the maximum limit count value, the key high level counter will no longer add 1 to avoid reentry. Whether it is greater than, less than or equal to the maximum limit count value, it will be transferred to SJ;
[0011] S13. When the key high level counter value is equal to a certain value, it is recorded as a rising edge, and it will be transferred to SJ regardless of whether it is equal or not;
[0012] S21. Compare the key low level counter value with the debounce delay count value. If it is greater than the debounce delay count value, change the key status bit and set the key high level counter to 0. Whether it is greater than, less than or equal to the debounce delay count value, transfer to SJ.
[0013] S22. Compare the key low level counter value with the maximum limit count value. If it is greater than the maximum limit count value, the key low level counter will no longer add 1 to avoid reentry. Whether it is greater than, less than or equal to the maximum limit count value, it will be transferred to SJ;
[0014] S23. When the key low level counter value is equal to a certain value, it is recorded as a falling edge, and it will be transferred to SJ regardless of whether it is equal or not;
[0015] SJ. End the cycle.
[0016] Furthermore, the task loop is in a thread task, and the task loop is a thread of the operating system or a while (1) loop with the operating system.
[0017] Furthermore, the status bit represents the computer's representation of the current key state, which has only two states: high level and low level.
[0018] Furthermore, the minimum time slice is 1 ms, 2 ms, 3 ms or other time slices that meet usage requirements.
[0019] Furthermore, the rising edge or falling edge is cleared by the user after being used by other functions.
[0020] Furthermore, a medical device includes a memory for storing instructions; a processor for executing the instructions, so as to implement a key scanning debouncing method that takes real-time into account.
[0021] Furthermore, a computer device includes a memory for storing instructions and a processor for executing the instructions, so as to implement a key scanning debouncing method that takes real-time performance into consideration.
[0022] Furthermore, a computer-readable storage medium stores a computer program, which, when executed, can implement a key scanning debouncing method that takes into account real-time performance.
[0023] The present invention has the beneficial effects:
[0024] 1. If there is a function with high real-time requirements in the minimum time slice, and the function and the present invention are in the same loop block, the present invention replaces the delay with a counting value to avoid the function being affected, and takes into account the real-time performance of the function while realizing the key debouncing function.
[0025] 2. At the same time, the rising edge and falling edge can be set once or multiple times, which brings great flexibility to the subsequent processing of the program. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] Figure 1 It is a flow chart of the cycle program;
[0027] Figure 2 It is a schematic diagram of multiple task cycles;
[0028] Figure 3 This is the waveform change diagram when the key is pressed. DETAILED DESCRIPTION
[0029] The present invention will be further described in detail below in conjunction with the embodiments and drawings, but the implementation manner of the present invention is not limited thereto.
[0030] Example 1
[0031] See also Figure 1 to Figure 2 As shown,
[0032] A key scanning debouncing method taking into account real-time performance is provided, wherein a key high level counter and a key low level counter are set, and a minimum time slice of a task cycle is also set. The minimum time slice is the time for executing a task cycle once. A task cycle includes the following steps:
[0033] SK. Start the loop;
[0034] S0. Detect the key state. If the key state is a high level, the key high level counter increases by 1 and transfers to S11, S12, and S13. If it is a low level, the key low level counter increases by 1 and transfers to S21, S22, and S23.
[0035] S11. Compare the key high level counter value with the debounce delay count value. If it is greater than the debounce delay count value, change the key status bit and set the key low level counter to 0. Whether it is greater than, less than or equal to the debounce delay count value, transfer to SJ.
[0036] S12. Compare the key high level counter value with the maximum limit count value. If it is greater than the maximum limit count value, the key high level counter will no longer add 1 to avoid reentry. Whether it is greater than, less than or equal to the maximum limit count value, it will be transferred to SJ;
[0037] S13. When the key high level counter value is equal to a certain value, it is recorded as a rising edge, and it will be transferred to SJ regardless of whether it is equal or not;
[0038] S21. Compare the key low level counter value with the debounce delay count value. If it is greater than the debounce delay count value, change the key status bit and set the key high level counter to 0. Whether it is greater than, less than or equal to the debounce delay count value, transfer to SJ.
[0039] S22. Compare the key low level counter value with the maximum limit count value. If it is greater than the maximum limit count value, the key low level counter will no longer add 1 to avoid reentry. Whether it is greater than, less than or equal to the maximum limit count value, it will be transferred to SJ;
[0040] S23. When the key low level counter value is equal to a certain value, it is recorded as a falling edge, and it will be transferred to SJ regardless of whether it is equal or not;
[0041] SJ. End the cycle.
[0042] Traditional key debounce is achieved by detecting the key status twice with a fixed delay set between the two detections. The delay is uncontrollable and the valid value is determined by whether the key status is the same in the two detections. However, when there are functions with high real-time performance in the same loop block, the delay will have an impact.
[0043] The present invention sets a fixed minimum time slice for the task cycle, and the minimum time slice is the time spent by the task cycle once. When the power is turned on, the task cycle starts to run:
[0044] When the switch is pressed, it will jitter due to mechanical jitter and other reasons. Therefore, the key state detected when the jitter occurs is uncertain, which may be a high level or a low level.
[0045] Therefore, the state detected by each key detection is uncertain, so the number of times the key high level counter and the key low level counter add 1 is also uncertain, so the value of the key high level counter and the key low level counter are also uncertain. Then we can only see which of the key high level counter value and the key low level counter value is greater than the de-bounce delay count value first. The counter value greater than the de-bounce delay count value will not be set to 0, and the other will be set to 0. For example: the key high level counter value is greater than the de-bounce delay count value, then the key low level counter value will be set to 0.
[0046] In addition to the above operations, the key high level counter value and the key low level counter value will also be compared with the maximum limit count value. If it is greater than the maximum limit count value, the value of the counter will be locked and no longer added by 1. For example, if the key high level counter value is greater than the maximum limit count value, the value of the key high level counter will be locked and no longer added by 1.
[0047] Regardless of the result of the above judgment, the task cycle will be ended, and the end of the task cycle also represents the beginning of the next task cycle.
[0048] The present invention replaces the delay in traditional key de-bouncing with a counting value, thereby preventing the delay from causing great time uncertainty to the program cycle time, thereby solving the real-time performance of the program cycle and the uncertainty of the cycle time. Specifically: in some cases, functions with high real-time requirements need to be executed within each minimum time slice. For example, high-speed communication requires a communication status detection once every 1ms, and the minimum time slice of the task cycle is 1ms. When the key de-bouncing and high-speed communication functions are in the same loop block, the present invention replaces the delay in traditional key de-bouncing with a counting value, thereby ensuring that the high-speed communication detection once in 1ms is not affected by the delay.
[0049] Furthermore, the task loop is in a thread task, and the task loop is a thread of the operating system or a while (1) loop with the operating system.
[0050] Furthermore, the status bit represents the computer's representation of the current key state, which has only two states: high level and low level.
[0051] Furthermore, the minimum time slice is 1 ms, 2 ms, 3 ms or other time slices that meet usage requirements.
[0052] Furthermore, the rising edge or falling edge is cleared by the user after being used by other functions.
[0053] Furthermore, a medical device includes a memory for storing instructions and a processor for executing the instructions, which can implement a key scanning debouncing method that takes into account real-time performance.
[0054] Furthermore, a computer device includes a memory for storing instructions and a processor for executing the instructions, which can implement a key scanning debouncing method that takes into account real-time performance.
[0055] Furthermore, a computer-readable storage medium stores a computer program, which, when executed, can implement a key scanning debouncing method that takes into account real-time performance.
[0056] Embodiment 2,
[0057] When a key is pressed, due to the vibration of the key or various electronic devices, the key will not be in direct and stable contact with various electronic devices at the moment of pressing, which causes jitter. The same is true when the key is released, so the following occurs: Figure 3 The rebound at the beginning and the rebound at the end;
[0058] When the button is pressed, the button state is detected. The rebound at the beginning and the rebound at the end of the button state will switch back and forth between the high level and the low level, so the detected button state is unstable:
[0059] If the key state is a key high level, the key high level counter is incremented by 1 and compared with the debounce delay count. If the key high level counter value is greater than the debounce delay count, the key state bit is changed and the key low level counter is set to 0. The key state bit is output to the microprocessor input port and the key low level counter is set to 0.
[0060] If the key state is a low level key, the key low level counter is incremented by 1 and compared with the de-bounce delay count. If the key low level counter value is greater than the de-bounce delay count, the key state bit is changed and the key high level counter is set to 0. The key state bit is output to the microprocessor input port and the key high level counter is set to 0.
[0061] At the same time, the key low level counter value or the key high level counter value is compared with the maximum limit count value. After multiple task cycles, if the key low level counter value is greater than the maximum limit count value before the key high level counter value, the key low level counter value is made equal to the maximum limit count value, so that it is no longer added by 1 and the key low level counter value is locked, completing the key scan debounce; if it is less than or equal to the maximum limit count value, the task cycle is terminated and the next task cycle is started at the same time;
[0062] After multiple task cycles, if the key high level counter value is greater than the maximum limit count value before the key low level counter value, the key high level counter value is set equal to the maximum limit count value to lock the value, and no longer add 1, completing the key scan debounce. If it is less than or equal to the maximum limit count value, the task cycle ends and the next task cycle starts.
[0063] It is understood that the above embodiments are merely exemplary embodiments used to illustrate the principles of the present invention, but the present invention is not limited thereto. For those skilled in the art, various modifications and improvements can be made without departing from the spirit and essence of the present invention, and these modifications and improvements are also considered to be within the scope of protection of the present invention.
Claims
1. A key scanning debouncing method taking into account real-time performance, characterized in that a key high level counter and a key low level counter are set, and a minimum time slice of a task cycle is also set, the minimum time slice is the time for a task cycle to be executed once, and a task cycle includes the following steps: SK. Start the loop; S0. Detect the key state. If the key state is a high level, the key high level counter increases by 1 and transfers to S11, S12, and S13. If it is a low level, the key low level counter increases by 1 and transfers to S21, S22, and S23. S11. Compare the key high level counter value with the debounce delay count value. If it is greater than the debounce delay count value, change the key status bit and set the key low level counter to 0. Whether it is greater than, less than or equal to the debounce delay count value, transfer to SJ. S12. Compare the key high level counter value with the maximum limit count value. If it is greater than the maximum limit count value, the key high level counter will no longer add 1 to avoid reentry. Whether it is greater than, less than or equal to the maximum limit count value, it will be transferred to SJ; S13. When the key high level counter value is equal to a certain value, it is recorded as a rising edge, and it will be transferred to SJ regardless of whether it is equal or not; S21. Compare the key low level counter value with the debounce delay count value. If it is greater than the debounce delay count value, change the key status bit and set the key high level counter to 0. Whether it is greater than, less than or equal to the debounce delay count value, transfer to SJ. S22. Compare the key low level counter value with the maximum limit count value. If it is greater than the maximum limit count value, the key low level counter will no longer add 1 to avoid reentry. Whether it is greater than, less than or equal to the maximum limit count value, it will be transferred to SJ; S23. When the key low level counter value is equal to a certain value, it is recorded as a falling edge, and it will be transferred to SJ regardless of whether it is equal or not; SJ. End the cycle.
2. A key scanning debouncing method taking into account real-time performance according to claim 1, characterized in that: Task loop In a threaded task, the task loop is a thread of the operating system or a while (1) loop with the operating system.
3. According to the key scanning debouncing method of claim 1, characterized in that: The status bit represents the computer's representation of the current key status, and there are only two states: high level and low level.
4. According to the key scanning debouncing method of claim 1, characterized in that: The minimum time slice is 1ms, 2ms, 3ms or other time slices that meet the usage requirements.
5. According to the key scanning debouncing method of claim 1, characterized in that: The rising or falling edge is cleared by the user after being used by other functions.
6. A medical device, characterized in that: It includes a memory for storing instructions; a processor for executing the instructions, so that the key scanning debouncing method taking into account real-time performance as described in any one of claims 1-5 can be implemented.
7. A computer device, characterized in that: It includes a memory for storing instructions; a processor for executing the instructions, so that the key scanning debouncing method taking into account real-time performance as described in any one of claims 1-5 can be implemented.
8. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed, it can implement the key scanning debouncing method taking into account real-time performance as described in any one of claims 1 to 5.
Citation Information
Patent Citations
Key trigger event detection method and device, and terminal
CN112073046A
Key detection driving method and system, electronic equipment and storage medium
CN115296673A
Switch de-bouncing device and method
US20080297205A1
