Device and method for detecting multiple keys through single pin of MCU (Microprogrammed Control Unit)
By using a single-pin detection method for MCUs with a series resistor of decreasing resistance and a button connected in parallel, the problem of excessive pin occupation when MCU chips detect multiple buttons is solved, realizing independent button status detection, saving hardware resources and reducing costs.
Patent Information
- Application Number
- CN202411150040.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-21
- Publication Date
- 2024-11-12
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
In existing technologies, MCU chips require a large number of pins to detect multiple keys, which increases costs, while matrix keyboard detection methods cannot independently determine the state of each key.
This device uses a single pin of an MCU to detect multiple buttons. It connects the buttons in parallel with a series resistor of decreasing resistance and combines it with the MCU's ADC pin to achieve independent detection of button status.
By using a single ADC pin of the MCU to detect the status of multiple buttons, hardware resources are saved, costs are reduced, and the status of each button can be accurately and independently determined.
Smart Images

Figure CN118937985A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of key detection, and in particular to a device and method for detecting multiple keys using a single pin of an MCU. Background Art
[0002] Nowadays, many home appliances and electronic products are equipped with key inputs, most of which are controlled by single-chip microcomputers. In the circuit design process of these home appliances and electronic products, each key needs to be tested. The general detection method is to use MCU chips for detection. To detect one key, one pin of the MCU chip is required. If dozens of keys are to be detected, dozens of pins of the MCU chip will be occupied. Too many pins are consumed, resulting in increased costs. Another detection method is to use a matrix keyboard, which uses fewer pins to detect more keys. However, this detection method cannot detect each key independently, and cannot independently determine the status of each key. Summary of the invention
[0003] The purpose of the present invention is to provide a device and method for detecting multiple keys with a single pin of an MCU to solve the above technical problems.
[0004] The present invention provides a device for detecting multiple keys by using a single pin of an MCU, comprising a power supply Vcc, an MCU, n keys and n+1 resistors.
[0005] All resistors are connected in series between the power supply Vcc and GND. From the power supply Vcc to GND, the resistance of the resistors decreases in sequence and the resistance of the latter resistor is less than or equal to half of the resistance of the adjacent previous resistor.
[0006] The ADC pin of the MCU is connected between the first resistor and the second resistor in the direction of power supply Vcc to GND.
[0007] In the direction from power supply Vcc to GND, starting from the second resistor, n buttons are connected in parallel with n resistors respectively.
[0008] Preferably, the n buttons are K1 to Kn, and the n+1 resistors are R0, R1 to Rn,
[0009] From the power supply Vcc to GND, the resistance of the latter resistor is equal to half of the resistance of the adjacent previous resistor.
[0010] The ADC pin of the MCU is connected between R0 and R1.
[0011] Therefore, starting from R1, each resistor is connected in parallel with a button, R1 is connected in parallel with K1, R2 is connected in parallel with K2, and so on. n Parallel K nWhen a button is pressed, the two ends of the resistor in parallel with the button will be short-circuited. When the button is not pressed, there will be no effect on the circuit.
[0012] The present invention also provides a method for detecting multiple keys by a device for detecting multiple keys with a single pin of an MCU, comprising the following steps:
[0013] S1: MCU periodically reads the voltage value V on the ADC pin;
[0014] S2: Calculate the mth key K m The maximum value of V when pressed pmax , the mth key K m The minimum value of V when pressed pmin , the mth key K m Maximum value of V when released rmax , the mth key K m Minimum value of V when releasing rmin , m≤n;
[0015] S3: When V is at V pmax and V pmin The corresponding key is pressed when V is between V rmax and V rmin If the value is between 0 and 1, the corresponding button is released; if the value is other than 0, the corresponding detection result is discarded.
[0016] Preferably, in step S2, the mth key K m Press and K m+1 ~K n Release all to get V pmax ,
[0017]
[0018] Among them, R u For key K m The resistance value of the resistor close to the power supply Vcc end and the connection point to the ADC pin is m<n.
[0019] Therefore, according to the formula, K1~K n V corresponding to each key in pmax .
[0020] Preferably, in step S2, the mth key K m Press and K m+1 ~K n Press all to get V pmin , V pmin is 0, m<n.
[0021] Therefore, K1~K n V corresponding to each key inpmin All are 0.
[0022] Preferably, in step S2, the mth key K m Release and K m+1 ~K n Release all to get V rmax ,
[0023]
[0024] Among them, R u For key K m The resistance value of the resistor close to the power supply Vcc end and the connection point to the ADC pin is m<n.
[0025] Therefore, according to the formula, K1~K n V corresponding to each key in rmax .
[0026] Preferably, in step S2, the mth key K m Release and K m+1 ~K n Press all to get V rmin ,
[0027]
[0028] Among them, R u For key K m The resistance value of the resistor close to the power supply Vcc end and the connection point to the ADC pin is m<n.
[0029] Therefore, according to the formula, K1~K n V corresponding to each key in rmin .
[0030] Preferably, the first key K1 corresponds to R u is 0. If the first button K1 is pressed, the R corresponding to the second button K2 is u is 0, otherwise it is R1, and the R corresponding to all buttons can be calculated in turn u .
[0031] Therefore, K1~K can be calculated n Each key corresponds to R u .
[0032] Preferably, R m is the mth key K m The parallel resistor, R m+1 is the m+1th key K m+1 The parallel resistor, R n is the nth key K nResistors in parallel.
[0033] Therefore, each resistor corresponds to a button in parallel, and the number of the resistor is the same as the number of the button in parallel, which is convenient for correspondence and calculation.
[0034] Preferably, in step S2, V pmax <V rmin .
[0035] Therefore, in this case, the range of key pressing (V pmax ~V pmin ) and the range of key release (V rmin ~V rmax ) do not overlap, so the state of each key can be accurately and independently determined.
[0036] The present invention has the following outstanding beneficial effects:
[0037] 1. The purpose of detecting the status of n buttons can be achieved through a single ADC pin of the MCU, so that the status of the MCU's I / O port and hardware resources can be fully utilized, reducing the occupation of the MCU's I / O port resources, saving hardware resources and reducing costs;
[0038] 2. The state of each button (pressed or released) can be detected accurately and independently. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 A circuit schematic diagram of a device for detecting multiple keys with a single pin of an MCU according to the present invention;
[0040] Figure 2 for Figure 1 The device shown is marked with R u Schematic diagram of
[0041] Figure 3 for Figure 2 A flowchart of a method for detecting multiple keys by the device shown;
[0042] Figure 4 A circuit schematic diagram of a key state of an embodiment of the device for detecting multiple keys with a single pin of an MCU of the present invention;
[0043] Figure 5 for Figure 4 A circuit schematic diagram of another key state of the device shown. DETAILED DESCRIPTION
[0044] The technical solutions in the embodiments of the present invention will be described clearly and completely below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments.
[0045] See also Figure 1 and Figure 2 A device for detecting multiple buttons with a single pin of an MCU includes a power supply Vcc, an MCU, n buttons and n+1 resistors.
[0046] See also Figure 1 and Figure 2 , the n buttons are K1~Kn, and the n+1 resistors are R0, R1~Rn.
[0047] See also Figure 1 All resistors, i.e. n+1 resistors, are connected in series between the power supply Vcc and GND, and R0, R1~Rn are connected in series in sequence from the power supply Vcc to GND.
[0048] The ADC pin of the MCU is connected between the first resistor and the second resistor in the direction of power supply Vcc to GND, that is, refer to Figure 1 The ADC pin of the MCU is connected to the connection point between R0 and R1. The MCU is a microcontroller unit processor. The ADC pin is a pin on the MCU that has the function of converting analog signals into digital signals. In this embodiment, the MCU uses GD32F450, and the ADC pin can use the PA1 pin on the GD32F450.
[0049] See also Figure 1 , n+1 resistors, from the power supply Vcc to GND, the resistance of the resistors decreases in sequence and the resistance of the next resistor is less than or equal to half of the resistance of the adjacent previous resistor, such as: the resistance of R1 is less than or equal to half of the resistance of R0, the resistance of R2 is less than or equal to half of the resistance of R1, and so on. n The resistance is less than or equal to R n-1 In this embodiment, from the power supply Vcc to GND, the resistance of the next resistor is equal to half of the resistance of the adjacent previous resistor, that is, the resistance of R1 is equal to half of the resistance of R0, the resistance of R2 is equal to half of the resistance of R1, and so on. n The resistance is equal to R n-1 half of the resistance value.
[0050] See also Figure 1 , from the power supply Vcc to GND, starting from the second resistor R1, n buttons K1~Kn are connected in parallel with n resistors R1~Rn respectively, that is, starting from R1, each resistor is connected in parallel with a button, R1 is connected in parallel with K1, R2 is connected in parallel with K2...R n Parallel K n , see Figure 2 , R1 is the resistor in parallel with the first button K1, R m is the mth key K m The parallel resistor, Rn is the nth key K n Resistors in parallel, in this case, each resistor corresponds to a button in parallel, the number of the resistor is the same as the number of the button in parallel, which is convenient for correspondence and calculation, m is less than or equal to n, and the value of m is a positive integer between 1 and n. When a button is pressed, the two ends of the resistor in parallel with the button will be short-circuited. When the button is not pressed, there is no effect on the circuit.
[0051] See also Figure 2 , for the convenience of calculation, R u is the resistance value of the key near the power supply Vcc end to the ADC pin connection. The first key K1 corresponds to R u is 0. If the first button K1 is pressed, the R corresponding to the second button K2 is u is 0, otherwise it is R1, and the R corresponding to all keys can be calculated in turn. u ,
[0052] Figure 3 Schematically shows Figure 2 The process of the method for detecting multiple keys by the device shown.
[0053] See also Figure 2 and Figure 3 , a method for detecting multiple keys by a device for detecting multiple keys with a single pin of an MCU, comprising the following steps:
[0054] 101: MCU periodically reads the voltage value V on the ADC pin.
[0055] 102: Calculate the mth key K m The maximum value of V when pressed pmax , the mth key K m The minimum value of V when pressed pmin , the mth key K m Maximum value of V when released rmax , the mth key K m Minimum value of V when releasing rmin , m≤n;
[0056] The mth key K m Press and K m+1 ~K n Release all to get V pmax ,
[0057]
[0058] Among them, R u For key K m The resistance value of the resistor near the power supply Vcc end to the ADC pin connection, at this time m<n, the R corresponding to the first button K1u is 0. If the first button K1 is pressed, the R corresponding to the second button K2 is u is 0, otherwise it is R1, and the R corresponding to all keys can be calculated in turn. u ;
[0059] R m+1 is the m+1th key K m+1 The parallel resistor, R n is the nth key K n Resistors in parallel;
[0060] This way, the V corresponding to each key can be calculated pmax , the last key K n The corresponding calculation formula is:
[0061]
[0062] The mth key K m Press and K m+1 ~K n Press all to get the minimum value of V pmin , V pmin is 0, at this time m<n;
[0063] The mth key K m Release and K m+1 ~K n Release all to get V rmax ,
[0064]
[0065] Among them, R u For key K m The resistance value of the resistor close to the power supply Vcc end to the ADC pin connection, at this time m<n, R m is the mth key K m The parallel resistor, R n is the nth key K n Resistors in parallel;
[0066] This way, the V corresponding to each key can be calculated rmax ;
[0067] The mth key K m Release and K m+1 ~K n Press all to get V rmin ,
[0068]
[0069] Among them, R u For key Km The resistance value of the resistor close to the power supply Vcc end to the ADC pin connection, at this time m<n, R m is the mth key K m Resistors in parallel;
[0070] This way, the V corresponding to each key can be calculated rmin .
[0071] 103: When V in V pmax and V pmin The corresponding key is pressed when V is between V rmax and V rmin If the value is between 0 and 1, the corresponding key is released; if the value is other than 1, the corresponding test result is discarded.
[0072] Since all parameters are positive, it is easy to verify
[0073] V rmin ≤V rmax
[0074] Since all parameters are positive numbers, from the power supply Vcc to GND, the resistance of the latter resistor is less than or equal to half of the resistance of the previous resistor, so
[0075]
[0076] According to the geometric sequence summation formula, we have
[0077]
[0078] Right now
[0079] Since m is less than or equal to n, it is easy to verify
[0080]
[0081] Right now
[0082] V pmax <V rmin
[0083] In this case, the range of key pressing (V pmax ~V pmin ) and the range of key release (V rmin ~V rmax ) do not overlap, so the state of each key can be accurately and independently determined.
[0084] Figure 4 The circuit principle of a key state of an embodiment of the device for detecting multiple keys with a single pin of an MCU of the present invention is schematically shown.
[0085] See also Figure 4 In this embodiment, the MCU in the device for detecting multiple keys with a single MCU pin uses GD32F450, the ADC pin uses the PA1 pin with an analog-to-digital conversion function of GD32F450, the power supply voltage Vcc uses 3.3V, the number of keys is 4, namely K1, K2, K3, and K4, and the number of resistors is 5, namely R0=200kΩ, R1=100kΩ, R2=50kΩ, R3=25kΩ, and R4=12.5kΩ.
[0086] According to the calculation formula, the corresponding voltage range is calculated in turn, and the state of the button is judged. The PA1 measurement results, calculation results and button state are shown in Table 1. The button state results are judged as follows: Figure 4 The present invention can detect the states of n keys (n=4 in this embodiment) through a single ADC pin of the MCU, and can accurately and independently detect the state of each key (pressed or released), thereby making full use of the state of the I / 0 port and hardware resources of the MCU, reducing the occupation of the I / 0 port resources of the MCU, saving hardware resources and reducing costs.
[0087] Table 1
[0088]
[0089] Figure 5 The circuit principle of another key state of an embodiment of the device for detecting multiple keys with a single pin of an MCU of the present invention is schematically shown.
[0090] See also Figure 5 In this embodiment, the MCU in the device for detecting multiple keys with a single MCU pin uses GD32F450, the ADC pin uses the PA1 pin with an analog-to-digital conversion function of GD32F450, the power supply voltage Vcc uses 3.3V, the number of keys is 4, namely K1, K2, K3, and K4, and the number of resistors is 5, namely R0=200kΩ, R1=100kΩ, R2=50kΩ, R3=25kΩ, and R4=12.5kΩ.
[0091] According to the calculation formula, the corresponding voltage range is calculated in turn, and the state of the button is judged. The PA1 measurement results, calculation results and button state are shown in Table 2. The button state results are judged as follows: Figure 5 The present invention can detect the states of n keys (n=4 in this embodiment) through a single ADC pin of the MCU, and can accurately and independently detect the state of each key (pressed or released), thereby making full use of the state of the I / 0 port and hardware resources of the MCU, reducing the occupation of the I / 0 port resources of the MCU, saving hardware resources and reducing costs.
[0092] Table 2
[0093]
[0094] The above are only some embodiments of the present invention, which are intended to illustrate the technical means of the present invention, but not to limit the technical scope of the present invention. Those skilled in the art can make obvious improvements to the present invention in combination with existing common knowledge, which all fall within the protection scope of the present invention.
Claims
1. A device for detecting multiple buttons with a single pin of an MCU, characterized in that: Including power supply Vcc, MCU, n buttons and n+1 resistors, All resistors are connected in series between the power supply Vcc and GND. From the power supply Vcc to GND, the resistance of the resistors decreases in sequence and the resistance of the latter resistor is less than or equal to half of the resistance of the adjacent previous resistor. The ADC pin of the MCU is connected between the first resistor and the second resistor in the direction of power supply Vcc to GND. In the direction from power supply Vcc to GND, starting from the second resistor, n buttons are connected in parallel with n resistors respectively.
2. The device according to claim 1, characterized in that The n buttons are K1~Kn, and the n+1 resistors are R0, R1~Rn , , From the power supply Vcc to GND, the resistance of the latter resistor is equal to half of the resistance of the adjacent previous resistor. The ADC pin of the MCU is connected between R0 and R1.
3. A method for detecting multiple keys using the device of claim 2, characterized in that: The following steps are involved: S1: MCU periodically reads the voltage value V on the ADC pin; S2: Calculate the mth key K m The maximum value of V when pressed pmax , the mth key K m The minimum value of V when pressed pmin , the mth key K m Maximum value of V when released rmax , the mth key K m Minimum value of V when releasing rmin , m≤n; S3: When V is at V pmax and V pmin The corresponding key is pressed when V is between V rmax and V rmin If the value is between 0 and 1, the corresponding button is released; if the value is other than 0, the corresponding detection result is discarded.
4. The method according to claim 3, characterized in that In step S2, the mth key K m Press and K m+1 ~K n Release all to get V pmax , Among them, R u For key K m The resistance value of the resistor close to the power supply Vcc end and the connection point to the ADC pin is m<n.
5. The method according to claim 3, characterized in that: In step S2, the mth key K m Press and K m+1 ~K n Press all to get V pmin , V pmin is 0, m<n.
6. The method according to claim 3, characterized in that In step S2, the mth key K m Release and K m+1 ~K n Release all to get V rmax , Among them, R u For key K m The resistance value of the resistor close to the power supply Vcc end and the connection point to the ADC pin is m<n.
7. The method according to claim 3, characterized in that In step S2, the mth key K m Release and K m+1 ~K n Press all to get V rmin , Among them, R u For key K m The resistance value of the resistor close to the power supply Vcc end and the connection point to the ADC pin is m<n.
8. The method according to claim 4, 6 or 7, characterized in that: The first button K1 corresponds to R u is 0. If the first button K1 is pressed, the R corresponding to the second button K2 is u is 0, otherwise it is R1, and the R corresponding to all buttons can be calculated in turn u .
9. The method according to claim 4, 6 or 7, characterized in that: R m is the mth key K m The parallel resistor, R m+1 is the m+1th key K m+1 The parallel resistor, R n is the nth key K n Resistors in parallel.
10. The method according to claim 3, characterized in that In step S2, V pmax <V rmin .
Citation Information
Patent Citations
Key circuit based on analog-digital conversion
CN102594362A
Digital keying circuit and keying equipment
CN103701472A
Key identification circuit
CN113328737A
Control circuit, method and system applied to potentiometer and electronic equipment
CN114326544A
Key detection circuit
CN213875939U
