https://micropython.org/download/?mcu=stm32f4
STM32F4DISC
,主控芯片STM32F407VGT6
,使用固件版本:MicroPython v1.20.0 on 2023-04-26
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
:上升沿触发。ExtInt.IRQ_RISING_FALLING
:边沿触发。(上升沿或下降沿)ExtInt.EVT_FALLING
:下降沿事件ExtInt.EVT_RISING
:上降沿事件ExtInt.EVT_RISING_FALLING
:边沿事件
- ✨EVT_xxx模式与
睡眠模式
和WFE
指令有关。
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()
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 工作频率为 84MHz
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()