https://micropython.org/download/?mcu=stm32f4
STM32F4DISC
,主控芯片STM32F407VGT6
https://docs.micropython.org/en/latest/
Thonny
Thonny
开发平台配置https://github.com/micropython/micropython/blob/master/ports/stm32/boards/
)from pyb import LED
:引入pyb模块中的子模块LEDimport time
: 为例了调用sleep sleep_ms sleep_us延时函数具体函数说明以及使用可以参考官方文档对应的模块说明: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()
'''
1=red(PD14), 2=green(PD12), 3=yellow(PD13), 4=blue(PD15)
LED_GREEN PD12
LED_ORANGE PD13
LED_RED PD14
LED_BLUE PD15
'''
import pyb
from pyb import LED
led = LED(1) # 1=红, 2=绿, 3=黄, 4=蓝
# 设置延时时间为1000毫秒
delay_time = 1000
# 获取当前的毫秒数
start_time = pyb.millis()
if __name__ == '__main__':
while True:
current_time = pyb.millis()
# 检查是否达到了延时时间
if current_time - start_time >= delay_time:
led.toggle()
start_time = current_time
https://docs.micropython.org/en/latest/pyboard/quickref.html#internal-leds
- ⚡该例程有bug,在使用
STM32F407
时,PD13
和PD15
并不能如预期那样输出PWM。
'''
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 步长5
led3_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!")
from pyb import Pin
模块'''
STM32F4DISC开发板引脚映射关系
1=red, 2=green, 3=yellow, 4=blue
LED_GREEN PD12
LED_ORANGE PD13
LED_RED PD14
LED_BLUE PD15
'''
from pyb import Pin
import time # 调用sleep sleep_ms sleep_us延时函数
p_out = Pin('D12', Pin.OUT_PP)
if __name__ == '__main__':
while True:
p_out.high()
time.sleep_ms(500)
p_out.low()
time.sleep_ms(500)
from machine import Pin
模块'''
STM32F4DISC开发板引脚映射关系
1=red, 2=green, 3=yellow, 4=blue
LED_GREEN PD12
LED_ORANGE PD13
LED_RED PD14
LED_BLUE PD15
'''
from machine import Pin
import time # 调用sleep sleep_ms sleep_us延时函数
LED_Pin = Pin('C10', Pin.OUT_PP) #PC10设置为推挽输出
def led_toggle():
LED_Pin.value(0) #设为低电平
time.sleep(0.5)
LED_Pin.value(1) #设为高电平
time.sleep(0.5)
if __name__ == '__main__':
while True:
led_toggle()
from pyb import Pin
import time # 调用sleep sleep_ms sleep_us延时函数
p_out = Pin('D12', Pin.OUT_PP)
if __name__ == '__main__':
while True:
# p_out.value(p_out.value()^1 ) # 状态取反
p_out.value(not p_out.value()) # 状态取反,同上
time.sleep_ms(500)
time.ticks_ms()
函数实现'''
STM32F4DISC开发板引脚映射关系
1=red, 2=green, 3=yellow, 4=blue
LED_GREEN PD12
LED_ORANGE PD13
LED_RED PD14
LED_BLUE PD15
'''
import machine
from pyb import Pin
import time # 调用sleep sleep_ms sleep_us延时函数
# 设置LED引脚
# led_pin = machine.Pin('A6', machine.Pin.OUT)
led_pin = Pin('A6', Pin.OUT_PP)
# 定义亮灭时间间隔(以毫秒为单位)
interval = 500
# 获取当前的毫秒数
start_time = time.ticks_ms()
while True:
# 获取当前的毫秒数
current_time = time.ticks_ms()
# 计算经过的时间
elapsed_time = time.ticks_diff(current_time, start_time)
# 判断是否达到亮灭时间间隔
if elapsed_time >= interval:
# 翻转LED状态
led_pin.value(not led_pin.value())
# 更新开始时间
start_time = current_time