Jitter elimination method for controlling multiple keys based on AD detection
By acquiring initial value, simulated discharge and filtering processing at the AD detection end of the microcontroller, combined with the relaxation of the voltage detection range and the accumulation of the number of times, the error touch and miss detection problems of the multi-key AD detection method under external interference is solved, and higher detection accuracy and reliability are achieved.
Patent Information
- Application Number
- CN202411734497.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-29
- Publication Date
- 2025-03-28
AI Technical Summary
In the development of microcontrollers, the AD detection method of multi-buttons is easily affected by external interference, resulting in unstable voltage changes, false touch and missed detection phenomena are more common, and existing hardware design solutions are difficult to modify on produced PCB boards.
By obtaining the initial AD value of the AD detection end of the microcontroller, performing analog discharge and filtering processing, determining the final AD value, and relaxing the voltage detection range, combining the accumulation of preset times to ensure the accuracy of the key signal.
It effectively reduces the impact of external interference on key detection, improves the accuracy and reliability of key detection, avoids mist touch and missed detection, and is suitable for PCB boards that have been produced.
Smart Images

Figure CN119727719A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of chip application technology, and in particular to a debouncing method for controlling multiple keys based on AD detection. Background Art
[0002] In the development of single-chip microcomputers, when multiple buttons are needed, the problem of insufficient pins is often encountered. Common solutions include port multiplexing and AD detection. In the actual application of the AD detection method, there will be a large external interference problem. When a button is pressed, the voltage value will undergo a change process from large to small. If the external interference is large, this change process will be prolonged and the voltage will become unstable; if the number of debounces in the program is insufficient, false triggering may occur, that is, the system mistakenly judges that other buttons are pressed; conversely, if the voltage judgment range is set too small, the button press action may not be detected.
[0003] To solve this problem, the existing technology usually relies on pre-planning in the hardware design stage, such as adding capacitors to the AD detection port to filter out interference. However, this method is particularly inconvenient when the PCB has been produced, especially when the product faces a large interference source (such as a motor, etc.), and the problem can no longer be solved by hardware modification. In addition, even if capacitors are added in the design stage, it is impossible to completely eliminate all types of interference, especially in complex electromagnetic environments. Summary of the invention
[0004] The purpose of the present invention is to provide a debouncing method for controlling multiple keys based on AD detection to solve the problems existing in the above-mentioned prior art.
[0005] To achieve the above object, the technical solution adopted by the present invention is: In a first aspect, the present invention provides a debouncing method for controlling multiple keys based on AD detection, the method being applicable to a circuit for controlling multiple keys based on AD detection, the circuit comprising at least a microcontroller and multiple keys, the method comprising: Get the initial AD value of the AD detection terminal of the microcontroller; Performing simulated discharge on the AD detection terminal of the microcontroller; Performing filtering on the initial AD value to determine a final AD value; The final AD value is subjected to a button judgment, and the voltage detection range of the AD detection terminal is relaxed. In response to the button being pressed at this time, the final AD value sets up a flag bit indicating that the button is initially established within the range of the corresponding button; In response to the flag of the key being initially established, a preset number of times is accumulated until the key flag is established, and the key is waited for to be released; the accumulated number of times is the number of times it is determined that the final AD value is within the voltage detection range of the AD detection end; In response to the key being released, it is detected that the corresponding key flag is established, that is, it is determined that the key is pressed, and the output port corresponding to the key outputs a corresponding key signal.
[0006] In a possible implementation, the obtaining of the initial AD value of the AD detection terminal of the microcontroller includes: The port voltage is collected through the AD detection terminal of the microcontroller, and each voltage data collected is accumulated, and the accumulation is performed four times to obtain four collected data; Remove the maximum and minimum values of the four collected data; In response to removing the maximum value and the minimum value of the four collected data, the remaining two data are averaged to obtain the initial AD value of the AD detection end of the microcontroller.
[0007] In a possible implementation, the simulating discharge of the AD detection terminal of the microcontroller includes: In response to each acquisition of voltage data, the AD detection terminal of the microcontroller is set to output a low level, maintained for 40-60us, and then switched back to the acquisition mode to continue to acquire the port voltage through the AD detection terminal of the microcontroller to achieve simulated discharge.
[0008] In a possible implementation, filtering the initial AD value to determine the final AD value includes: An operation relationship is established between the current AD value ad_sum_now and the previous AD value ad_sum_last to determine the final AD value ad_sum_new.
[0009] Furthermore, the operation relation is: ad_sum_new=(ad_sum_now-ad_sum_last*3) / 4.
[0010] In a possible implementation manner, filtering the initial AD value to determine a final AD value further includes: The anti-misjudgment operation includes: The final AD value ad_sum_new and the previous AD value ad_sum_last are judged, and the difference ad_diff between the previous AD value ad_sum_last and the final AD value ad_sum_new is calculated, and the difference ad_diff=ad_sum_last-ad_sum_new. If the difference ad_diff is greater than or equal to 30, it means that the key is being pressed. At this time, no judgment is made until the difference ad_diff is less than 30, and then the next step is performed.
[0011] In a possible implementation, the circuit for controlling multiple keys based on AD detection includes: A microcontroller having a first pin connected to a positive power source, a second pin as an AD detection terminal, third to seventh pins as output terminals, and an eighth pin connected to a negative power source; A plurality of buttons are connected to the second pin of the microcontroller.
[0012] In a possible implementation manner, a voltage dividing resistor is connected between the second pin of the microcontroller and the multiple buttons.
[0013] In a possible implementation manner, a pull-up resistor is further connected between the second pin of the microcontroller and the positive electrode of the power supply.
[0014] In a possible implementation manner, the first pin of the microcontroller is further connected to the eighth pin of the microcontroller via a filter capacitor.
[0015] The beneficial effects brought about by the technical solution provided by the present invention include at least: By obtaining the initial AD value of the AD detection end of the microcontroller; simulating discharge of the AD detection end of the microcontroller; filtering the initial AD value to determine the final AD value; performing button judgment on the final AD value, relaxing the voltage detection range of the AD detection end, in response to the key being pressed at this time, the final AD value sets up a flag position indicating that the key is initially established within the range of the corresponding key; in response to the initial establishment of the key flag position, a preset number of times is accumulated until the key flag position is established, waiting for the key to be released; in response to the key being released, the corresponding key flag position is detected to be established, that is, it is determined that the key is pressed, and the output port corresponding to the key outputs the corresponding key signal. In this case, a method is provided that can effectively solve the key interference problem at the software level, which can not only adapt to the PCB boards that have been produced, but also improve the accuracy and reliability of key detection, and avoid false triggering and missed detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] The accompanying drawings are used to provide further understanding of the present invention and constitute a part of the specification. They are used to explain the present invention together with the embodiments of the present invention and do not constitute a limitation of the present invention.
[0017] Figure 1 A circuit diagram of controlling multiple keys based on AD detection provided by an exemplary embodiment of the present invention is shown.
[0018] Figure 2 A flow chart of a method for debouncing multiple keys based on AD detection control provided by an exemplary embodiment of the present invention is shown.
[0019] Figure 3 A logic block diagram of a method for debouncing multiple keys based on AD detection control provided by an exemplary embodiment of the present invention is shown. DETAILED DESCRIPTION
[0020] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0021] The present invention will be further described below in conjunction with the accompanying drawings and embodiments.
[0022] Figure 1 A schematic diagram of a circuit for controlling multiple keys based on AD detection provided by an exemplary embodiment of the present invention is shown. The circuit for controlling multiple keys based on AD detection includes: a microcontroller U1, a first key KEY1, a second key KEY2, a third key KEY3, and a fourth key KEY4; the microcontroller U1 has a first pin pin1 connected to the positive pole of the power supply VCC, a second pin pin2 as an AD detection end, an eighth pin pin8 connected to the negative pole of the power supply GND, and a third pin pin3, a fourth pin pin4, a fifth pin pin5, a sixth pin pin6 and a seventh pin pin7 as output ends, and the third to seventh pins as output ends are used to output corresponding signals when the keys are pressed.
[0023] In detail, a voltage-dividing resistor is connected between the second pin pin2 of the microcontroller U1 and the multiple buttons, specifically: the first end of the first button KEY1 is connected to the second end of the second resistor R2, the second end of the first button KEY1 is connected to the negative power supply GND, and the first end of the second resistor R2 is connected to the second pin pin2 of the microcontroller U1 as an AD detection end. The first end of the second button KEY2 is connected to the second end of the third resistor R3, the second end of the second button KEY2 is connected to the negative power supply GND, and the first end of the third resistor R3 is connected to the second pin pin2 of the microcontroller U1 as an AD detection end. The first end of the third button KEY3 is connected to the second end of the fourth resistor R4, the second end of the third button KEY3 is connected to the negative power supply GND, and the first end of the fourth resistor R4 is connected to the second pin pin2 of the microcontroller U1 as an AD detection end. The first end of the fourth button KEY4 is connected to the second end of the fifth resistor R5, the second end of the fourth button KEY4 is connected to the negative power supply GND, and the first end of the fifth resistor R5 is connected to the second pin pin2 of the microcontroller U1 as an AD detection end.
[0024] Furthermore, a pull-up resistor is connected between the second pin pin2 of the microcontroller U1 and the positive power supply VCC to ensure that the AD detection terminal remains in a known high level state when there is no external signal input. The pull-up resistor can prevent the pin from being suspended, reduce noise interference, and improve the stability and accuracy of AD conversion, so that when an external signal is connected, the AD detection terminal can respond faster and accurately read the signal change. Specifically, the first end of the first resistor R1 is connected to the positive power supply VCC, and the second end of the first resistor R1 is connected to the second pin pin2 of the microcontroller U1 as the AD detection terminal.
[0025] Furthermore, the first pin pin1 of the microcontroller U1 is connected to the eighth pin pin8 of the microcontroller U1 through a filter capacitor to filter out noise and ripples on the power line and provide a more stable power supply voltage, which helps to reduce the impact of power supply fluctuations on the microcontroller and its internal circuits (such as AD converters) and improve the stability and measurement accuracy of the system. Specifically, the first end of the capacitor C1 is connected to the first pin pin1 of the microcontroller U1, and the second end of the capacitor C1 is connected to the eighth pin pin8 of the microcontroller U1.
[0026] In the embodiment of the present application, when different buttons are pressed, different voltage values will be obtained after the corresponding resistor voltage is divided. The microcontroller converts the voltage into a digital signal through AD, and the program determines whether the corresponding button is pressed.
[0027] It is worth mentioning that when a key is pressed, the voltage value will go from large to small. If the external interference is large, this change process will be enlarged and the voltage will be unstable. At this time, if the number of debounce times in the program is not enough, false triggering will occur (that is, it will be judged as other keys); or the voltage judgment range is too small, and the key will not be detected. Therefore, this application provides a debounce method based on AD detection and control of multiple keys, specifically: Figure 2 A flowchart of a method for debouncing multiple keys based on AD detection control provided by an exemplary embodiment of the present invention is shown. Figure 3 A logic block diagram of a debounce method for controlling multiple keys based on AD detection is shown according to an exemplary embodiment of the present invention. The method is applicable to the circuit for controlling multiple keys based on AD detection as described above. The method comprises the following steps: Step 201, obtaining an initial AD value of an AD detection terminal of a microcontroller.
[0028] In the embodiment of the present application, the above-mentioned acquisition of the initial AD value of the AD detection terminal of the microcontroller includes: collecting the port voltage through the AD detection terminal of the microcontroller, accumulating each voltage data collected, accumulating four times, and obtaining four collected data; removing the maximum value and the minimum value of the four collected data; in response to removing the maximum value and the minimum value of the four collected data, taking the average value of the remaining two data to obtain the initial AD value of the AD detection terminal of the microcontroller. This process effectively reduces the influence of noise and interference through multiple sampling and removal of extreme values, and improves the accuracy and stability of the initial AD value.
[0029] Step 202, simulate discharging the AD detection terminal of the microcontroller.
[0030] In the embodiment of the present application, the above-mentioned simulated discharge of the AD detection terminal of the microcontroller includes: in response to each acquisition of voltage data, the AD detection terminal of the microcontroller is set to output a low level, maintained for 40-60us, and then switched back to the acquisition mode to continue to acquire the port voltage through the AD detection terminal of the microcontroller to achieve simulated discharge. This process effectively eliminates the residual charge and external interference on the AD detection terminal through simulated discharge, ensuring that the voltage data acquired next time is more accurate and reliable.
[0031] Step 203: filter the initial AD value to determine the final AD value.
[0032] In the embodiment of the present application, the above filtering process of the initial AD value to determine the final AD value includes: establishing an operation relationship between the current AD value ad_sum_now and the last AD value ad_sum_last to determine the final AD value ad_sum_new. Specifically, the operation relationship is: ad_sum_new=(ad_sum_now-ad_sum_last*3) / 4. This operation relationship introduces the last AD value to perform weighted average processing on the current AD value, effectively filtering out instantaneous noise and interference, and improving the stability and accuracy of the AD value.
[0033] It should be noted that the current AD value ad_sum_now is interpreted as the current initial AD value, and the last AD value ad_sum_last is interpreted as the last final AD value.
[0034] Furthermore, the above filtering process of the initial AD value to determine the final AD value also includes: anti-misjudgment operation; when the key is pressed, the AD value will have a curve change, which can be judged by the program to filter this change to prevent other voltages from being judged during the pressing process. Specifically, the anti-misjudgment operation includes: judging the final AD value ad_sum_new and the previous AD value ad_sum_last, and making a difference ad_diff between the last AD value ad_sum_last and the final AD value ad_sum_new, the difference ad_diff=ad_sum_last-ad_sum_new, if the difference ad_diff is greater than or equal to 30, it means that the key is being pressed, and no judgment is made at this time until the difference ad_diff is less than 30 and tends to be stable, and then proceed to the next step. This can effectively avoid misjudgment caused by changes in AD values during the key pressing process.
[0035] Step 204, perform button judgment on the final AD value, relax the voltage detection range of the AD detection end (further eliminate small interference), and in response to the key being pressed at this time, the final AD value sets up a flag bit indicating that the key is initially established within the range of the corresponding key.
[0036] In one example, the voltage detection range of the AD detection terminal is relaxed as follows: for example, if the theoretical voltage value is 2457, the voltage detection range of the AD detection terminal is relaxed to 2300-2600. It is worth noting that the relaxation cannot be too large, and the relaxation should be controlled to be no more than 200 above and below the theoretical voltage value to prevent voltage misjudgment. This setting reduces the impact of small interference by expanding the voltage detection range, ensuring the accuracy and reliability of key judgment.
[0037] Step 205, in response to the initial establishment of the key flag (debouncing), a preset number of accumulations is performed until the key flag is established, and the key is waited for to be released; wherein the number of accumulations is: the number of times it is determined that the final AD value is within the voltage detection range of the AD detection terminal.
[0038] In the embodiment of the present application, by increasing the number of de-jittering, it is ensured that the corresponding key is pressed to prevent misjudgment caused by interference. That is, through multiple judgments and accumulations, key jitter and external interference are effectively eliminated, ensuring the accuracy and reliability of key detection.
[0039] In the embodiment of the present application, after the flag bit in response to the key is initially established (de-bouncing is performed), a preset number of accumulations is performed: the preset number of accumulations mentioned is generally 40-50 times.
[0040] Step 206, in response to the key being released, it is detected that the corresponding key flag is established, that is, it is determined that the key is pressed, and the output port corresponding to the key outputs the corresponding key signal.
[0041] In the embodiment of the present application, this step ensures that the correct key signal is output only after the key is fully pressed and released, effectively avoiding misjudgment caused by key jitter or brief contact.
[0042] In summary, the present application obtains the initial AD value of the AD detection end of the microcontroller; simulates the discharge of the AD detection end of the microcontroller; filters the initial AD value to determine the final AD value; performs button judgment on the final AD value, relaxes the voltage detection range of the AD detection end, and in response to the key being pressed at this time, the final AD value sets up a flag position indicating that the key is initially established within the range of the corresponding key; after the flag position of the key is initially established, a preset number of times is accumulated until the key flag position is established, and waits for the key to be released; in response to the key being released, the corresponding key flag position is detected to be established, that is, it is determined that the key is pressed, and the output port corresponding to the key outputs the corresponding key signal. In this case, a method is provided that can effectively solve the problem of key interference at the software level, which can not only adapt to the PCB boards that have been produced, but also improve the accuracy and reliability of key detection, and avoid false triggering and missed detection.
[0043] It should be understood that the specific examples herein are only intended to help those skilled in the art to better understand the present disclosure, rather than to limit the scope of the present invention.
[0044] It is understood that in the various implementations of this specification, the size of the sequence number of each process does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the present disclosure.
[0045] It can be understood that the various implementation modes described in this specification can be implemented individually or in combination, and the present disclosure is not limited to this.
[0046] Unless otherwise stated, all technical and scientific terms used in this disclosure have the same meaning as those generally understood by those skilled in the art of the technical field of this specification. The terms used in this specification are only for the purpose of describing specific embodiments and are not intended to limit the scope of this specification. The term "and / or" used in this specification includes any and all combinations of one or more related listed items. The singular forms of "a", "above", and "the" used in this disclosure and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings.
[0047] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this specification.
[0048] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method implementation methods and will not be repeated here.
[0049] In the several embodiments provided in this specification, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device implementation described above is only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0050] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0051] In addition, each functional unit in each embodiment of the present specification may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0052] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this specification, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the method described in each embodiment of this specification. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc., various media that can store program codes.
[0053] The above is only a specific implementation of this specification, but the protection scope of the present invention is not limited thereto. Any person skilled in the art who is familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in this specification, which should be included in the protection scope of this specification. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A debounce method for controlling multiple keys based on AD detection, characterized in that: The method is applicable to a circuit for controlling multiple buttons based on AD detection, wherein the circuit at least includes a microcontroller and multiple buttons. The method includes: Get the initial AD value of the AD detection terminal of the microcontroller; Performing simulated discharge on the AD detection terminal of the microcontroller; Performing filtering on the initial AD value to determine a final AD value; The final AD value is subjected to a button judgment, and the voltage detection range of the AD detection terminal is relaxed. In response to the button being pressed at this time, the final AD value sets up a flag bit indicating that the button is initially established within the range of the corresponding button; In response to the flag of the button being initially established, a preset number of times is accumulated until the button flag is established, and the button is waited for to be released; the number of times is accumulated for judging that the final AD value is within the voltage detection range of the AD detection terminal; In response to the key being released, it is detected that the corresponding key flag is established, that is, it is determined that the key is pressed, and the output port corresponding to the key outputs a corresponding key signal.
2. The debounce method for controlling multiple keys based on AD detection according to claim 1, characterized in that: The step of obtaining the initial AD value of the AD detection terminal of the microcontroller includes: The port voltage is collected through the AD detection terminal of the microcontroller, and each voltage data collected is accumulated, and the accumulation is performed four times to obtain four collected data; Remove the maximum and minimum values of the four collected data; In response to removing the maximum value and the minimum value of the four collected data, the remaining two data are averaged to obtain the initial AD value of the AD detection end of the microcontroller.
3. The debounce method for controlling multiple keys based on AD detection according to claim 2, characterized in that: The simulating discharge of the AD detection terminal of the microcontroller comprises: In response to each acquisition of voltage data, the AD detection terminal of the microcontroller is set to output a low level, maintained for 40-60us, and then switched back to the acquisition mode to continue to acquire the port voltage through the AD detection terminal of the microcontroller to achieve simulated discharge.
4. The debounce method for controlling multiple keys based on AD detection according to claim 1, characterized in that: The filtering process on the initial AD value to determine the final AD value includes: An operation relationship is established between the current AD value ad_sum_now and the previous AD value ad_sum_last to determine the final AD value ad_sum_new.
5. The debounce method for controlling multiple keys based on AD detection according to claim 4, characterized in that: The operation relation is: ad_sum_new=(ad_sum_now-ad_sum_last*3) / 4.
6. The debounce method for controlling multiple keys based on AD detection according to claim 5, characterized in that: The filtering process is performed on the initial AD value to determine the final AD value, and then the following steps are further included: The anti-misjudgment operation includes: The final AD value ad_sum_new and the previous AD value ad_sum_last are judged, and the difference ad_diff between the previous AD value ad_sum_last and the final AD value ad_sum_new is calculated, and the difference ad_diff=ad_sum_last-ad_sum_new. If the difference ad_diff is greater than or equal to 30, it means that the key is being pressed. At this time, no judgment is made until the difference ad_diff is less than 30, and then the next step is performed.
7. The debouncing method for controlling multiple keys based on AD detection according to any one of claims 1 to 6, characterized in that: The circuit for controlling multiple buttons based on AD detection includes: A microcontroller having a first pin connected to a positive power source, a second pin as an AD detection terminal, third to seventh pins as output terminals, and an eighth pin connected to a negative power source; A plurality of buttons are connected to the second pin of the microcontroller.
8. The debounce method for controlling multiple keys based on AD detection according to claim 7, characterized in that: A voltage dividing resistor is connected between the second pin of the microcontroller and the multiple buttons.
9. The debounce method for controlling multiple keys based on AD detection according to claim 7, characterized in that: A pull-up resistor is also connected between the second pin of the microcontroller and the positive electrode of the power supply.
10. The debounce method for controlling multiple keys based on AD detection according to claim 7, characterized in that: The first pin of the microcontroller is also connected to the eighth pin of the microcontroller via a filter capacitor.