From 413b394d317a87e7664d83bce83e832588d7448c Mon Sep 17 00:00:00 2001 From: kalkyl Date: Fri, 3 Nov 2023 00:37:58 +0100 Subject: [PATCH] rp: Add PWM input example --- examples/rp/src/bin/pwm_input.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 examples/rp/src/bin/pwm_input.rs diff --git a/examples/rp/src/bin/pwm_input.rs b/examples/rp/src/bin/pwm_input.rs new file mode 100644 index 00000000..794b28af --- /dev/null +++ b/examples/rp/src/bin/pwm_input.rs @@ -0,0 +1,26 @@ +//! This example shows how to use the PWM module to measure the frequency of an input signal. + +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_rp::pwm::{Config, InputMode, Pwm}; +use embassy_time::{Duration, Ticker}; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = embassy_rp::init(Default::default()); + + let mut cfg: Config = Default::default(); + let mut pwm = Pwm::new_input(p.PWM_CH2, p.PIN_5, InputMode::RisingEdge, cfg); + + let mut ticker = Ticker::every(Duration::from_secs(1)); + loop { + info!("Input frequency: {} Hz", pwm.counter()); + pwm.set_counter(0); + ticker.next().await; + } +}