This is the 26th day of my participation in the August Text Challenge.More challenges in August
Key circuit is the single chip microcomputer application of the most extensive and simple circuit, the actual project often use the key, but a key often needs to occupy an IO port, especially the single chip microcomputer resources are relatively tight, IO is more precious. If you want to use the key, and do not want to waste IO port, then you can change the idea, through the AD port voltage detection method to achieve the key function.
Let’s start with a simple example:
Three resistors R1, R2 and R3 are connected in series. Key S1 is connected in parallel with resistance R1, key S2 is connected in parallel with resistance R2. Then R1 and R2 are connected to the IO port of the SINGLE chip microcomputer in the middle.
It can be seen from the simulation that when the key is not pressed, the VOLTAGE of THE ADC port is 2.5V, the voltage of the ADC port is 5V after the key S1 is pressed, and the voltage of the ADC port is 0.05V after the key S2 is pressed.
After pressing S1 and S2 at the same time, the voltage of ADC port is still 5V. Note This method can detect only independent keys, not combined keys.
Note that the R3 resistance in the circuit should not be omitted, otherwise the power supply will be short-circuited directly after pressing S1 and S2 keys at the same time.
External keys can be judged by voltage changes of ADC ports, so that multiple keys can be detected with only one ADC port. So let’s see how more keys are detected.
This circuit can realize the detection of 6 keys. After pressing different keys, different resistors will be selected for series and parallel combination, thus changing the output voltage value.
When the key is not pressed, the output voltage is 0V.
After the key S3 is pressed, the output voltage is 3.947V. After the S4 button is pressed, the output voltage is 3.125V.
After pressing S5, the output voltage is 2.439V. After pressing S6, the output voltage is 1.829V.
After pressing S7, the output voltage is 1.25V. After pressing S8, the output voltage is 0.657V.
The number of keys that can be detected varies according to the sampling accuracy of the SINGLE-chip ADC and the voltage divider accuracy. But the core idea is to read the voltage value through the ADC and determine which key is pressed according to the voltage value. Note that this method is only suitable for detecting individual keys, not combination keys.
In this case, when IO port resources are insufficient, the ADC port can be used as the detection port of the key.