K210 - External Interrupt

K210

insert image description here

First, external interruption

Earlier, when we were doing ordinary buttons (GPIO), although the IO port input and output function could be realized, the code was always detecting the change of the IO input port, so the efficiency was not high, especially in some specific occasions, such as a certain button , it may be pressed once a day to perform the relevant function, so we waste a lot of time to detect the key press in real time. In order to solve such a problem, we introduce the concept of external interrupt, as the name implies, when the button is pressed (interrupt), we will perform the relevant function. This greatly saves CPU resources, so the application of interrupts in practical projects is very common.

Those who have studied single-chip microcomputers all know that the rising edge and falling edge
of the key are connected to GND through the key, which is what we call low level "0", so when the key is pressed and then released, the The pin gets the falling edge first, and then the rising edge, as shown in the figure below:
insert image description here
Friends who have studied MCU should know that the use of keys needs to be de-
jittered. When the key is pressed, jitter may occur. The jitter is as shown in the figure below, which may cause misjudgment. , so we
need to use the delay function to debounce. This is because jitter may occur when the button is pressed. The jitter is as shown in the figure below, which may cause misjudgment, so we need to use the delay function to debounce:
insert image description here
we can choose The falling edge method triggers an external interrupt, that is, an interrupt is generated immediately when the button is pressed.

2. Use steps

It should be noted that K210 has external interrupts only with high-speed GPIO. The GPIO constant table is as follows:
insert image description here
insert image description here
insert image description here

2. Complete code


from Maix import GPIO
from fpioa_manager import fm
import utime

#注册IO,注意高速GPIO口才有中断
fm.register(12, fm.fpioa.GPIO0)
fm.register(16, fm.fpioa.GPIOHS0)

#构建lED和KEY对象
LED_B=GPIO(GPIO.GPIO0,GPIO.OUT,value=1)
KEY=GPIO(GPIO.GPIOHS0, GPIO.IN, GPIO.PULL_UP)

#LED状态表示
state = 1

#中断回调函数
def fun(KEY):
    global state
    utime.sleep_ms(10) #消除抖动
    if KEY.value()==0: #确认按键被按下
        state = not state
        LED_B.value(state)

#开启中断,下降沿触发
KEY.irq(fun, GPIO.IRQ_FALLING)

Finally, be sure to turn on the interrupt and set the trigger mode
insert image description here

Summarize

From the reference code, the experimental function is implemented with just a few lines of code, and the efficiency of the code is greatly enhanced compared to the use of the while True real-time detection function. The application of external interrupts is very wide. In addition to ordinary key input and level detection, a large part of input devices, such as sensors, are also detected in real time through external interrupts, thereby providing CPU utilization.

Guess you like

Origin blog.csdn.net/qq_51963216/article/details/123945561