In the single-chip microcomputer system, the key is a common input device, this paper will introduce several key hardware, software design skills. Generally speaking, there are four schemes for the design of keys: first, the GPIO port directly detects a single key, as shown in Figure 1.1; Second, the matrix keyboard is used when there are many keys, as shown in Figure 1.2; Third, connect the key to the external interrupt pin, and use the edge signal generated by pressing the key to detect the key, as shown in Figure 1.3; The fourth is to use the ADC of single-chip microcomputer. After different keys are pressed, the voltage on the ADC interface is different. According to the different voltage, the keys can be identified, as shown in Figure 1.4.
Figure 1.1 scheme 1
Figure 1.2 scheme 2
Figure 1.3 scheme 3
Figure 1.4 scheme 4
In the above four designs, each has advantages and disadvantages.
The first one is the simplest and most basic one, which is easy to understand and use for beginners of single-chip microcomputer, but the disadvantage is that it needs to constantly detect whether the key is pressed in the main loop, and it needs to do anti shake processing. If a function task in the main loop takes a long time, the key will have different degrees of “failure”.
The second one has the advantage of being able to expand as many keys as possible in the case of limited GPIO. But the disadvantage is the same as above, need to constantly detect whether the key is pressed.
The third way is the most efficient, it does not need to cycle to detect whether the key is pressed, but the disadvantage is that the MCU needs to have enough external interrupt interface for use; The fourth advantage is that only one ADC interface and one wire of MCU are needed to identify multiple keys. The disadvantage is that once the internal contact of keys is bad, the key string may be in place, and the jitter of keys will cause certain identification errors.
Based on the above three kinds of common key design, now share the key scheme summarized in my study and work.
Improvement 1: on the basis of the original scheme 1, and gate circuit is added, so that any key pressed can produce an interrupt, and then identify which key was pressed in the interrupt. Therefore, there is no need to cycle scanning, which greatly improves the efficiency. The scheme is shown in Figure 1.5. Just need to add a diode to each key. By using the line and characteristics of diode, you can press any key and generate an interrupt signal, but the keys do not affect each other. Diode selection of ordinary rectifier diode, I personally test feasible.
Figure 1.5 improvement 1
Figure 1.6 improvement 2
Improvement 2: on the basis of the original ADC key, the way of adding diode can also be used to realize the key interruption, and carry out ad conversion in the interruption service program, so as to identify the key. The circuit is shown in Figure 1.6.
Improvement 3: because the key inevitably has jitter, the key jitter can be eliminated by hardware and software. Now share a very simple and effective hardware acne elimination method: parallel a 104 capacitor to the key. The software can avoid shaking without processing.
Improvement 4: under the scheme of key scan detection, if a function in the main loop takes a long time, the key will “fail” for a long time or a short time. Now share my solution. The key scanning is put into the timer interrupt, so that the key pressing can be detected periodically without the influence of the main cycle. In addition, it can analyze the different states of the key, that is, press, hold, bounce and press, so as to achieve richer functions.
But we need to pay attention to two points. First, the timing time of the timer should not be too long or too short. If it is too long, it is easy to not detect the press. If it is too short, it will take up a lot of time resources. Second, the interrupt service program should be simple and clear, only for detection, through the global variable transfer, complete the key response in the main loop, and try not to take up too much time in the interrupt service function.