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_input.rs | |
| parent | b0172bb58217d625a13fed8122827b8d0b03c46a (diff) | |
Add PWM examples for stm32g0
Diffstat (limited to 'examples/stm32g0/src/bin/pwm_input.rs')
| -rw-r--r-- | examples/stm32g0/src/bin/pwm_input.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/examples/stm32g0/src/bin/pwm_input.rs b/examples/stm32g0/src/bin/pwm_input.rs new file mode 100644 index 000000000..152ecda86 --- /dev/null +++ b/examples/stm32g0/src/bin/pwm_input.rs | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | //! PWM input example | ||
| 2 | //! | ||
| 3 | //! This program demonstrates how to capture the parameters of the input waveform (frequency, width and duty cycle) | ||
| 4 | //! Connect PB1 and PA6 with a 1k Ohm resistor or Connect PB1 and PA8 with a 1k Ohm resistor | ||
| 5 | //! to see the output. | ||
| 6 | //! | ||
| 7 | |||
| 8 | #![no_std] | ||
| 9 | #![no_main] | ||
| 10 | |||
| 11 | use defmt::*; | ||
| 12 | use embassy_executor::Spawner; | ||
| 13 | use embassy_stm32::gpio::{Level, Output, OutputType, Pull, Speed}; | ||
| 14 | use embassy_stm32::time::khz; | ||
| 15 | use embassy_stm32::timer::pwm_input::PwmInput; | ||
| 16 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; | ||
| 17 | use embassy_stm32::timer::Channel; | ||
| 18 | use embassy_stm32::{bind_interrupts, peripherals, timer}; | ||
| 19 | use embassy_time::Timer; | ||
| 20 | use {defmt_rtt as _, panic_probe as _}; | ||
| 21 | |||
| 22 | // Connect PB1 and PA6 with a 1k Ohm resistor | ||
| 23 | #[embassy_executor::task] | ||
| 24 | async fn blinky(led: peripherals::PB1) { | ||
| 25 | let mut led = Output::new(led, Level::High, Speed::Low); | ||
| 26 | |||
| 27 | loop { | ||
| 28 | led.set_high(); | ||
| 29 | Timer::after_millis(50).await; | ||
| 30 | |||
| 31 | led.set_low(); | ||
| 32 | Timer::after_millis(50).await; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | bind_interrupts!(struct Irqs { | ||
| 37 | TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>; | ||
| 38 | }); | ||
| 39 | |||
| 40 | #[embassy_executor::main] | ||
| 41 | async fn main(spawner: Spawner) { | ||
| 42 | let p = embassy_stm32::init(Default::default()); | ||
| 43 | |||
| 44 | unwrap!(spawner.spawn(blinky(p.PB1))); | ||
| 45 | // Connect PA8 and PA6 with a 1k Ohm resistor | ||
| 46 | let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull); | ||
| 47 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(1), Default::default()); | ||
| 48 | let max = pwm.get_max_duty(); | ||
| 49 | pwm.set_duty(Channel::Ch1, max / 4); | ||
| 50 | pwm.enable(Channel::Ch1); | ||
| 51 | |||
| 52 | let mut pwm_input = PwmInput::new(p.TIM2, p.PA0, Pull::None, khz(1000)); | ||
| 53 | pwm_input.enable(); | ||
| 54 | |||
| 55 | loop { | ||
| 56 | Timer::after_millis(500).await; | ||
| 57 | let period = pwm_input.get_period_ticks(); | ||
| 58 | let width = pwm_input.get_width_ticks(); | ||
| 59 | let duty_cycle = pwm_input.get_duty_cycle(); | ||
| 60 | info!( | ||
| 61 | "period ticks: {} width ticks: {} duty cycle: {}", | ||
| 62 | period, width, duty_cycle | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | } | ||
