Key detection of K210 MCU

This picture is the effect of the program, the blue light is on after the button is pressed, and the blue light is off after the button is released.

 The main use is the construction method and usage method of the function:
GPIO(ID,MODE,PULL,VALUE)
GPIO object.
    [ID] Internal GPIO number;
    [MODE] GPIO mode;
          GPIO.IN: input mode
          GPIO.OUT: output mode
    [PULL]
          GPIO.PULL_UP: pull up
          GPIO.PULL_DOWN: pull down
          GPIO.PULL_NONE: no
    [value] GPIO initialization power Level
          1: high level
          0: low level

 Usage method
GPIO.value([value])
    【value】GPIO output level value;
          1: high level
          0: low level
*The parameter is empty in input mode, which means to get the current IO input level value

 The following is the program code, take a closer look, it is very simple. Because the positive pole of the led light is connected to +3.3V, we control the negative pole. So the value of led is set to 0 to light up. Set to 1 high level is off.

from Maix import GPIO           # 导入GPIO模块
from fpioa_manager import fm    # 导入fm模块

#注册 IO,蓝灯-->IO12,KEY-->IO16
fm.register(12,fm.fpioa.GPIO0)  # 注册外部12口为内部GPIO0口
fm.register(16,fm.fpioa.GPIO1)  # 注册外部12口为内部GPIO1口

#  初始化IO
led = GPIO(GPIO.GPIO0, GPIO.OUT)  # led对象为GPIO0,输出模式
key = GPIO(GPIO.GPIO1, GPIO.IN)   # key对象为GPIO1,输入模式

while(1):                       # 无限循环
    if(key.value()==0):             # 如果按键的值是0:
        led.value(0)                     # led的值设为0,也就是开灯
    else:                           # 否则:
        led.value(1)                     # led的值设为1,也就是关灯

This program is tested by myself, and it can be copied and pasted completely. Normal operation, if it helps you, I hope to give me a free heart, thank you~!

Guess you like

Origin blog.csdn.net/xingyuncao520025/article/details/130448614