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/input_capture.rs | |
| parent | b0172bb58217d625a13fed8122827b8d0b03c46a (diff) | |
Add PWM examples for stm32g0
Diffstat (limited to 'examples/stm32g0/src/bin/input_capture.rs')
| -rw-r--r-- | examples/stm32g0/src/bin/input_capture.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/examples/stm32g0/src/bin/input_capture.rs b/examples/stm32g0/src/bin/input_capture.rs new file mode 100644 index 000000000..69fdae96d --- /dev/null +++ b/examples/stm32g0/src/bin/input_capture.rs | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | //! Input capture example | ||
| 2 | //! | ||
| 3 | //! This example showcases how to use the input capture feature of the timer peripheral. | ||
| 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 | //! When connecting PB1 (software pwm) and PA6 the output is around 10000 (it will be a bit bigger, around 10040) | ||
| 7 | //! Output is 1000 when connecting PB1 (PWMOUT) and PA6. | ||
| 8 | //! | ||
| 9 | #![no_std] | ||
| 10 | #![no_main] | ||
| 11 | |||
| 12 | use defmt::*; | ||
| 13 | use embassy_executor::Spawner; | ||
| 14 | use embassy_stm32::gpio::{Level, Output, OutputType, Pull, Speed}; | ||
| 15 | use embassy_stm32::time::khz; | ||
| 16 | use embassy_stm32::timer::input_capture::{CapturePin, InputCapture}; | ||
| 17 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; | ||
| 18 | use embassy_stm32::timer::Channel; | ||
| 19 | use embassy_stm32::{bind_interrupts, peripherals, timer}; | ||
| 20 | use embassy_time::Timer; | ||
| 21 | use {defmt_rtt as _, panic_probe as _}; | ||
| 22 | |||
| 23 | // Connect PB1 and PA6 with a 1k Ohm resistor | ||
| 24 | |||
| 25 | #[embassy_executor::task] | ||
| 26 | async fn blinky(led: peripherals::PB1) { | ||
| 27 | let mut led = Output::new(led, Level::High, Speed::Low); | ||
| 28 | |||
| 29 | loop { | ||
| 30 | led.set_high(); | ||
| 31 | Timer::after_millis(50).await; | ||
| 32 | |||
| 33 | led.set_low(); | ||
| 34 | Timer::after_millis(50).await; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | bind_interrupts!(struct Irqs { | ||
| 39 | TIM2 => timer::CaptureCompareInterruptHandler<peripherals::TIM2>; | ||
| 40 | }); | ||
| 41 | |||
| 42 | #[embassy_executor::main] | ||
| 43 | async fn main(spawner: Spawner) { | ||
| 44 | let p = embassy_stm32::init(Default::default()); | ||
| 45 | info!("Hello World!"); | ||
| 46 | |||
| 47 | unwrap!(spawner.spawn(blinky(p.PB1))); | ||
| 48 | |||
| 49 | // Connect PB1 and PA8 with a 1k Ohm resistor | ||
| 50 | let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull); | ||
| 51 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(1), Default::default()); | ||
| 52 | pwm.enable(Channel::Ch1); | ||
| 53 | pwm.set_duty(Channel::Ch1, 50); | ||
| 54 | |||
| 55 | let ch1 = CapturePin::new_ch1(p.PA0, Pull::None); | ||
| 56 | let mut ic = InputCapture::new(p.TIM2, Some(ch1), None, None, None, Irqs, khz(1000), Default::default()); | ||
| 57 | |||
| 58 | let mut old_capture = 0; | ||
| 59 | |||
| 60 | loop { | ||
| 61 | ic.wait_for_rising_edge(Channel::Ch1).await; | ||
| 62 | |||
| 63 | let capture_value = ic.get_capture_value(Channel::Ch1); | ||
| 64 | info!("{}", capture_value - old_capture); | ||
| 65 | old_capture = capture_value; | ||
| 66 | } | ||
| 67 | } | ||
