diff options
| author | Chen Yuheng <[email protected]> | 2024-06-19 10:57:18 +0800 |
|---|---|---|
| committer | Chen Yuheng <[email protected]> | 2024-06-19 10:57:18 +0800 |
| commit | 0579d248efc1ee78f039fbfabcf2e0272e32ee53 (patch) | |
| tree | fe6dca408b8a015d0eef4a473c1213193f22f0a4 /examples/stm32g0/src/bin/pwm_complementary.rs | |
| parent | b0172bb58217d625a13fed8122827b8d0b03c46a (diff) | |
Add PWM examples for stm32g0
Diffstat (limited to 'examples/stm32g0/src/bin/pwm_complementary.rs')
| -rw-r--r-- | examples/stm32g0/src/bin/pwm_complementary.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/examples/stm32g0/src/bin/pwm_complementary.rs b/examples/stm32g0/src/bin/pwm_complementary.rs new file mode 100644 index 000000000..98305ae42 --- /dev/null +++ b/examples/stm32g0/src/bin/pwm_complementary.rs | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | //! PWM complementary example | ||
| 2 | //! | ||
| 3 | //! This example uses two complementary pwm outputs from TIM1 with different duty cycles | ||
| 4 | //! ___ ___ | ||
| 5 | //! |_________| |_________| PA8 | ||
| 6 | //! _________ _________ | ||
| 7 | //! ___| |___| | PA7 | ||
| 8 | //! _________ _________ | ||
| 9 | //! |___| |___| PB3 | ||
| 10 | //! ___ ___ | ||
| 11 | //! _________| |_________| | PB0 | ||
| 12 | |||
| 13 | #![no_std] | ||
| 14 | #![no_main] | ||
| 15 | |||
| 16 | use defmt::info; | ||
| 17 | use embassy_executor::Spawner; | ||
| 18 | use embassy_stm32::gpio::OutputType; | ||
| 19 | use embassy_stm32::time::khz; | ||
| 20 | use embassy_stm32::timer::complementary_pwm::{ComplementaryPwm, ComplementaryPwmPin}; | ||
| 21 | use embassy_stm32::timer::simple_pwm::PwmPin; | ||
| 22 | use embassy_stm32::timer::Channel; | ||
| 23 | use embassy_stm32::Config as PeripheralConfig; | ||
| 24 | use {defmt_rtt as _, panic_probe as _}; | ||
| 25 | |||
| 26 | #[embassy_executor::main] | ||
| 27 | async fn main(_spawner: Spawner) { | ||
| 28 | let p = embassy_stm32::init(Default::default()); | ||
| 29 | |||
| 30 | let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull); | ||
| 31 | let ch1n = ComplementaryPwmPin::new_ch1(p.PA7, OutputType::PushPull); | ||
| 32 | let ch2 = PwmPin::new_ch2(p.PB3, OutputType::PushPull); | ||
| 33 | let ch2n = ComplementaryPwmPin::new_ch2(p.PB0, OutputType::PushPull); | ||
| 34 | |||
| 35 | let mut pwm = ComplementaryPwm::new( | ||
| 36 | p.TIM1, | ||
| 37 | Some(ch1), | ||
| 38 | Some(ch1n), | ||
| 39 | Some(ch2), | ||
| 40 | Some(ch2n), | ||
| 41 | None, | ||
| 42 | None, | ||
| 43 | None, | ||
| 44 | None, | ||
| 45 | khz(100), | ||
| 46 | Default::default(), | ||
| 47 | ); | ||
| 48 | |||
| 49 | let max = pwm.get_max_duty(); | ||
| 50 | info!("Max duty: {}", max); | ||
| 51 | |||
| 52 | pwm.set_duty(Channel::Ch1, max / 4); | ||
| 53 | pwm.enable(Channel::Ch1); | ||
| 54 | pwm.set_duty(Channel::Ch2, max * 3 / 4); | ||
| 55 | pwm.enable(Channel::Ch2); | ||
| 56 | |||
| 57 | loop {} | ||
| 58 | } | ||
