为什么我的led(stm32f3发现板)在应用延迟后没有发光



我在应用延迟时犯了什么错误吗?

这是我正在使用的代码,在led 3和4闪烁后会有延迟。

use cortex_m_rt::entry;
use stm32f30x_hal as hal;
use hal::delay::Delay;
use hal::prelude::*;
use hal::stm32f30x;
use panic_halt;
#[entry]
fn main() -> ! {
let device_p = stm32f30x::Peripherals::take().unwrap();
let core_periphs=cortex_m::Peripherals::take().unwrap();
let mut reset_clock_control = device_p.RCC.constrain();
let mut gpioe = device_p.GPIOE.split(&mut reset_clock_control.ahb);
**let mut flash = device_p.FLASH.constrain();
let clocks = reset_clock_control.cfgr.freeze(&mut flash.acr);
let mut delay = Delay::new(core_periphs.SYST,clocks);**
let mut led_3 = gpioe
.pe9
.into_push_pull_output(&mut (gpioe.moder), &mut (gpioe.otyper));
let mut led_4=gpioe.pe8.into_push_pull_output(&mut gpioe.moder,&mut gpioe.otyper);

loop {
led_3.set_high();
**delay.delay_ms(2_000_u16);**
led_4.set_high();
}
}

如果我没有使用延迟部分,它运行良好

我认为您的clocks设置错误。为了使延迟正常工作,您应该使用系统时钟。

以下是如何基于此示例为STM32创建Delay(stm32f4xx,但也适用于您(:

// Set up the system clock. We want to run at 48MHz for this one.
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.sysclk(48.mhz()).freeze();
// Create a delay abstraction based on SysTick
let mut delay = hal::delay::Delay::new(cp.SYST, clocks);

其中dp是我的设备外围设备(例如let dp = stm32::Peripherals::take().unwrap()(,而cp是核心外围设备。

因此使用sysclk

或者,您也可以尝试用cortex_m::delay(8_000_000);替换您的延迟,其中延迟是使用时钟周期数给出的。

在循环中,您将LED设置为高led_3.set_high();。然而,再也不要将led_3设置为低电平,这样它就永远不会闪烁。因此,将您的循环更改为:

led_3.set_high();
led_4.set_low();
delay.delay_ms(2_000_u16);
led_4.set_high();
led_3.set_low();

最新更新