From b297e5f7bd152d1e5147631c81987c26f9fa0664 Mon Sep 17 00:00:00 2001 From: Jacob Rosenthal Date: Mon, 1 Nov 2021 13:51:40 -0700 Subject: [PATCH] led dimming example, dont need to keep all examples, just covering ground to test api --- examples/nrf/src/bin/pwm_led.rs | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 examples/nrf/src/bin/pwm_led.rs diff --git a/examples/nrf/src/bin/pwm_led.rs b/examples/nrf/src/bin/pwm_led.rs new file mode 100644 index 00000000..067f4d2c --- /dev/null +++ b/examples/nrf/src/bin/pwm_led.rs @@ -0,0 +1,47 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use defmt::*; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_nrf::gpio::NoPin; +use embassy_nrf::pwm::{Prescaler, Pwm}; +use embassy_nrf::Peripherals; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + let pwm = Pwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); + // set_period doesnt actually set what you give it, because it only has a + // few options from the hardhware so be explicit instead + // Div128 is slowest, 125khz still crazy fast for our eyes + pwm.set_prescaler(Prescaler::Div128); + + info!("pwm initialized!"); + + // default max_duty if not specified is 1000 + // so 0 would be fully off and 1000 or above would be fully on + loop { + info!("100%"); + pwm.set_duty(0, 1000); + Timer::after(Duration::from_millis(5000)).await; + + info!("25%"); + pwm.set_duty(0, 250); + Timer::after(Duration::from_millis(5000)).await; + + info!("10%"); + pwm.set_duty(0, 100); + Timer::after(Duration::from_millis(5000)).await; + + info!("5%"); + pwm.set_duty(0, 50); + Timer::after(Duration::from_millis(5000)).await; + + info!("0%"); + pwm.set_duty(0, 0); + Timer::after(Duration::from_millis(5000)).await; + } +}