Is there any mistake I am making in applying delay the delay?
This is the code I am working with to blink led 3 and 4 after with a delay.
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();
}
}
If I am not using delay part it is working fine
I think you set up your clocks
wrong. For the delay to work correctly you should use the system clock.
This is how to create the Delay
for STM32 based on this sample (stm32f4xx, but should work for you, too):
// 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);
where dp
are my device peripherals (eg let dp = stm32::Peripherals::take().unwrap()
) and cp
are the core peripherals.
So this uses the sysclk
.
Alternatively you could also try to replace your delay with cortex_m::delay(8_000_000);
, where the delay is given using the number of clock cycles.
In the loop you set the LED high led_3.set_high();
. However never set led_3
low again so it would never blink. So change your loop to:
led_3.set_high();
led_4.set_low();
delay.delay_ms(2_000_u16);
led_4.set_high();
led_3.set_low();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.