本例子实现如下功能:
通过 MicroPython 编程在 ESP32-S3 上实现按下按键时 LED 状态取反,并且对按键进行 10ms 的滤波处理。
硬件连接
1、LED 连接到 GPIO12(IO12)。
2、按键 连接到 GPIO0(IO0),并确保按键按下时连接到地(GND)
代码:
from machine import Pin
import time
# 初始化LED和按键引脚
led = Pin(12,Pin.OUT,value=1)
key = Pin(0,Pin.IN,Pin.PULL_UP)
#LED状态
led_state = False
# 防抖动时间50ms
last_interrupt_time = 0
debounce_time = 0.05
# 中断处理函数
def Key_Int_IRQHandler(pin):
global led_state,last_interrupt_time
current_time = time.ticks_ms()
if current_time-last_interrupt_time>debounce_time:
led_state=not led_state
led.value(led_state)
last_interrupt_time=current_time
if __name__ == '__main__':
# 按键中断,检测下降沿
key.irq(trigger=Pin.IRQ_FALLING,handler=Key_Int_IRQHandler)
while True :
pass
1821

被折叠的 条评论
为什么被折叠?



