Micropython STM32F4外部中断实验
📌固件刷可参考前面一篇《STM32刷Micropython固件参考指南》 🌿 相关篇《Micropython STM32F4入门点灯》 📍固件下载:https://micropython.org/download/?mcu=stm32f4
🔖本例程基于STM32F4DISC
,主控芯片STM32F407VGT6
,使用固件版本:MicroPython v1.20.0 on 2023-04-26
📑mpy外部中断,可以参考Micropython官方开发参考文档:https://docs.micropython.org/en/latest/library/pyb.ExtInt.html#pyb-extint
🛠开发平台基于Thonny
📓查询外部中断模块相关函数和常量
>> > from pyb import ExtInt
>> > help ( ExtInt)
object < class 'ExtInt' > is of type type
line -- < function>
enable -- < function>
disable -- < function>
swint -- < function>
regs -- < staticmethod>
IRQ_RISING -- 269549568
IRQ_FALLING -- 270598144
IRQ_RISING_FALLING -- 271646720
EVT_RISING -- 269615104
EVT_FALLING -- 270663680
EVT_RISING_FALLING -- 271712256
🌿ExtInt.disable()
:不使能外部中断。 🌿ExtInt.enable()
:使能外部中断。 🌿ExtInt.line()
:发生外部中断时,返回引脚映射到的中断线。 🌿swint()
:从软件触发回调。 常量:
ExtInt.IRQ_FALLING
:下降沿触发。
ExtInt.IRQ_RISING_FALLING
:边沿触发。(上升沿或下降沿)
ExtInt.EVT_RISING_FALLING
:边沿事件
⚡需要注意的是,在IDE中,调试执行睡眠模式相关代码,对应的调试端口号会消失。因为睡眠模式下,基本的硬件外设基本关闭了,无法进行在线调试。
pyb.ExtInt(pin, mode, pull, callback)
:创建中断对象
pin
is the pin on which to enable the interrupt (can be a pin object or any valid pin name).
mode
can be one of: - - trigger on a rising edge; - - trigger on a falling edge; - - trigger on a rising or falling edge.ExtInt.IRQ_RISINGExtInt.IRQ_FALLINGExtInt.IRQ_RISING_FALLING
pull
can be one of: - - no pull up or down resistors; - - enable the pull-up resistor; - - enable the pull-down resistor.pyb.Pin.PULL_NONEpyb.Pin.PULL_UPpyb.Pin.PULL_DOWN
callback
is the function to call when the interrupt triggers. The callback function must accept exactly 1 argument, which is the line that triggered the interrupt.
📝外部中断例程代码
'' '
STM32F4DISC开发板引脚映射关系
1 = red ( PD14) , 2 = green ( PD12) , 3 = yellow ( PD13) , 4 = blue ( PD15)
LED_GREEN PD12
LED_ORANGE PD13
LED_RED PD14
LED_BLUE PD15
IRQ_RISING -- 269549568
IRQ_FALLING -- 270598144
IRQ_RISING_FALLING -- 271646720
'' '
from pyb import Pin, ExtInt
from pyb import LED
import time # 调用sleep sleep_ms sleep_us延时函数
INT_EXT = Pin ( 'E3' , Pin. IN, Pin. PULL_UP)
LED_Pin = Pin ( 'E13' , Pin. OUT_PP) #PC10设置为推挽输出
LED_Pin2 = Pin ( 'E14' , Pin. OUT_PP) #PC10设置为推挽输出
# callback = lambda e: print ( 'intr PE3 Pin' )
def callback ( line) :
LED_Pin. value ( 0 ) #设为低电平
time. sleep ( 0.5 )
LED_Pin. value ( 1 ) #设为高电平
time. sleep ( 0.5 )
print ( 'intr PE3 Pin' )
print ( "line =" , line)
def led_toggle ( ) :
LED_Pin2. value ( 0 ) #设为低电平
time. sleep ( 0.5 )
LED_Pin2. value ( 1 ) #设为高电平
time. sleep ( 0.5 )
ext = ExtInt ( INT_EXT, ExtInt. IRQ_FALLING, Pin. PULL_UP, callback)
if __name__ == '__main__' :
while True:
led_toggle ( )