Micropython STM32F4入门点灯第一课
- 📌固件刷可参考前面一篇《STM32刷Micropython固件参考指南》
- 📍固件下载:
https://micropython.org/download/?mcu=stm32f4
- 🔖本例程基于
STM32F4DISC
,主控芯片STM32F407VGT6
- 📑Micropython官方开发参考文档:
https://docs.micropython.org/en/latest/
- 🛠开发平台基于
Thonny
🛠Thonny
开发平台配置
-
🔧在菜单栏-工具-选项,进行设置:
-
🌿创建
led.py
程序文件
-
🌿按F5快捷键或者点击菜单上的运行按钮运行。
⛳注意事项
- 🌿不同的开发板烧录的固件不一样。
- 🌿不同型号的开发板引脚映射关系不一样。(具体查看源码对应开发板型号:
https://github.com/micropython/micropython/blob/master/ports/stm32/boards/
)
📖引入模块说明
from pyb import LED
:引入pyb模块中的子模块LEDimport time
: 为例了调用sleep sleep_ms sleep_us延时函数
📑模块内API函数查看方法
-
🔨在
Thonny
查看模块内的函数方法
-
📜具体函数说明以及使用可以参考官方文档对应的模块说明:
https://docs.micropython.org/en/latest/library/pyb.LED.html#pyb-led
📝点灯程序代码
'''
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'''from pyb import LED
import time # 调用sleep sleep_ms sleep_us延时函数led = LED(1) # 1=红, 2=绿, 3=黄, 4=蓝
led2 = LED(2)
led3 = LED(3)
led4 = LED(4)while True:led.toggle() led2.toggle()led3.toggle()led4.toggle()time.sleep(1)
# led.on()
# led.off()
📓LED模块例程二
- 🌿参考文档:
https://docs.micropython.org/en/latest/pyboard/quickref.html#internal-leds
'''
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
# LEDs 3 and 4 support PWM intensity (0-255)
'''from pyb import LED
import time # 调用sleep sleep_ms sleep_us延时函数
led3_pwm = LED(3) # PD13
led4_pwm = LED(4) # PD15
print('Hello World')
if __name__ == '__main__': while True:for i in range(0,255,5): # 0 - 254 步长5led3_pwm.intensity(i)led4_pwm.intensity(255-i) time.sleep_ms(50)
# print('value1=%d' % led3_pwm.intensity())
# print('value2=%d' % led4_pwm.intensity()) print('i= {0} ,led3_value={1},led4_value={2}' .format(i,led3_pwm.intensity(),led4_pwm.intensity()))else:print("Finally finished!")