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()
:发生外部中断时,返回引脚映射到的中断线。 🌿ExtInt.swint()
:从软件触发回调。 🌿ExtInt.regs()
: 转储 EXTI 寄存器的值。 常量:
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.回调函数形参一定要带上中断线line
📝外部中断例程代码
'' '
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) #PE13设置为推挽输出
LED_Pin2 = Pin ( 'E14' , Pin. OUT_PP) #PE14设置为推挽输出
# 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 ( )
from pyb import Pin, ExtInt
from machine import Pin
# 初始化GPIO引脚
# 初始化GPIO引脚为上拉输入
gpio_pin = Pin ( 'E2' , Pin. IN, Pin. PULL_UP)
p_out = Pin ( 'E12' , Pin. OUT_PP)
def interrupt_handler ( pin) :
print ( "External interrupt triggered!" )
# p _out. value ( p_out. value ( ) ^ 1 ) # 状态取反
p_out. value ( not p_out. value ( ) ) # 状态取反,同上
# 配置外部中断
gpio_pin. irq ( trigger= Pin. IRQ_FALLING, handler= interrupt_handler)
📗停机模式下外部中断事件唤醒示例
✨需要注意,直接在IDE里面运行下面的调试程序时,当执行到pyb.stop()
时,端口会消失,因为CPU进入了停机模式,所有外设停止了工作,所以端口也会消失,当按下E3
时,将会激活CPU,继续往下执行。
'' '
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
EVT_RISING -- 269615104
EVT_FALLING -- 270663680
EVT_RISING_FALLING -- 271712256
'' '
import pyb
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)
INT_EXT2 = Pin ( 'E2' , Pin. IN, Pin. PULL_UP)
LED_Pin = Pin ( 'E13' , Pin. OUT_PP) #PE13设置为推挽输出
LED_Pin2 = Pin ( 'E14' , Pin. OUT_PP) #PE14设置为推挽输出
# pyb . repl_uart ( pyb. UART ( 1 , 9600 ) ) # 复制 REPL 到 UART ( 1 )
value_freq = pyb. freq ( ) # 获取 CPU 和总线的频率
print ( 'value_freq:%s' % str ( value_freq) )
# pyb . freq ( 84000000 ) # 设置 CPU 工作频率为 84 MHz
def task ( line) :
print ( "外部中断事件已触发" )
print ( "line =" , line)
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_EXT2, ExtInt. EVT_FALLING, Pin. PULL_UP, callback) #E2
Ext3 = ExtInt ( INT_EXT, ExtInt. IRQ_FALLING, Pin. PULL_UP, task) # E3
pyb. stop ( ) # 进入STOP模式, 等待外部中断, 停止往下执行,直到E3发生外部中断, 才会往下执行
# pyb . wfi ( ) # 进入低功耗睡眠模式
if __name__ == '__main__' :
while True:
led_toggle ( )