When learning MCU, everyone must have touched the key. The key is an interface device of human-computer interaction. At the beginning of contact, the simplest and most direct way is to connect the key directly to an I / O port, and then detect the level change of I / O. assuming that the other end of the key is connected to GND, once the low level is detected, it is considered that the key is triggered (here false) At first glance, no problem! And it’s very good! In fact, this is really the best choice for beginners, but it’s the wrong design idea for the boss.

The problem lies in the process of detecting the I / O port. The CPU must detect every other period of time. Generally, it is implemented by timer. The interval time is too long to achieve the real-time performance. The time is too short, which wastes CPU resources. Because in many occasions, when the key is in the trigger period, it is quite rare, such as the school’s automatic storage machine. Generally, no one uses it, which means that the CPU is not used Interval time to view I / O is very unreasonable, only when the key is pressed to take view, this is the most reasonable way.

In this way, the design scheme of external interrupt must be introduced. The design schematic diagram of a key is as follows:

If there are two or more buttons, how to do it is very easy to use or gate to solve this problem. The circuit diagram is as follows:

Leave a Reply

Your email address will not be published. Required fields are marked *