diff options
| author | chemicstry <[email protected]> | 2022-06-23 02:27:39 +0300 |
|---|---|---|
| committer | chemicstry <[email protected]> | 2022-06-23 02:27:39 +0300 |
| commit | 3cdd8c1aebaebd7464fa3477ce63525a101dcf72 (patch) | |
| tree | b2884853b64b9790e8d5613f2c2193260eee6972 /examples/stm32f4 | |
| parent | 4a6f69e2d9e1c18ff5999a6aa049c280a6b8dcc4 (diff) | |
Fix PWM for advanced timers
Diffstat (limited to 'examples/stm32f4')
| -rw-r--r-- | examples/stm32f4/src/bin/pwm.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/examples/stm32f4/src/bin/pwm.rs b/examples/stm32f4/src/bin/pwm.rs new file mode 100644 index 000000000..4073f5385 --- /dev/null +++ b/examples/stm32f4/src/bin/pwm.rs | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy::executor::Spawner; | ||
| 7 | use embassy::time::{Duration, Timer}; | ||
| 8 | use embassy_stm32::pwm::simple_pwm::SimplePwm; | ||
| 9 | use embassy_stm32::pwm::Channel; | ||
| 10 | use embassy_stm32::time::{Hertz, U32Ext}; | ||
| 11 | use embassy_stm32::{Config, Peripherals}; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | fn config() -> Config { | ||
| 15 | let mut config = Config::default(); | ||
| 16 | config.rcc.hse = Some(Hertz(8_000_000)); | ||
| 17 | config.rcc.sys_ck = Some(Hertz(84_000_000)); | ||
| 18 | config | ||
| 19 | } | ||
| 20 | |||
| 21 | #[embassy::main(config = "config()")] | ||
| 22 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 23 | info!("Hello World!"); | ||
| 24 | |||
| 25 | let mut pwm = SimplePwm::new_1ch(p.TIM1, p.PE9, 10000.hz()); | ||
| 26 | let max = pwm.get_max_duty(); | ||
| 27 | pwm.enable(Channel::Ch1); | ||
| 28 | |||
| 29 | info!("PWM initialized"); | ||
| 30 | info!("PWM max duty {}", max); | ||
| 31 | |||
| 32 | loop { | ||
| 33 | pwm.set_duty(Channel::Ch1, 0); | ||
| 34 | Timer::after(Duration::from_millis(300)).await; | ||
| 35 | pwm.set_duty(Channel::Ch1, max / 4); | ||
| 36 | Timer::after(Duration::from_millis(300)).await; | ||
| 37 | pwm.set_duty(Channel::Ch1, max / 2); | ||
| 38 | Timer::after(Duration::from_millis(300)).await; | ||
| 39 | pwm.set_duty(Channel::Ch1, max - 1); | ||
| 40 | Timer::after(Duration::from_millis(300)).await; | ||
| 41 | } | ||
| 42 | } | ||
