diff options
| author | Jacob Rosenthal <[email protected]> | 2021-11-01 13:51:40 -0700 |
|---|---|---|
| committer | Jacob Rosenthal <[email protected]> | 2021-11-01 13:51:40 -0700 |
| commit | b297e5f7bd152d1e5147631c81987c26f9fa0664 (patch) | |
| tree | 4fac51dc5f8d28e19107692a441513db3c8b5a39 /examples/nrf/src/bin | |
| parent | 74e7f4a22717a73f735caa97589c9603091016e6 (diff) | |
led dimming example, dont need to keep all examples, just covering ground to test api
Diffstat (limited to 'examples/nrf/src/bin')
| -rw-r--r-- | examples/nrf/src/bin/pwm_led.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/nrf/src/bin/pwm_led.rs b/examples/nrf/src/bin/pwm_led.rs new file mode 100644 index 000000000..067f4d2ca --- /dev/null +++ b/examples/nrf/src/bin/pwm_led.rs | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | use defmt::*; | ||
| 8 | use embassy::executor::Spawner; | ||
| 9 | use embassy::time::{Duration, Timer}; | ||
| 10 | use embassy_nrf::gpio::NoPin; | ||
| 11 | use embassy_nrf::pwm::{Prescaler, Pwm}; | ||
| 12 | use embassy_nrf::Peripherals; | ||
| 13 | |||
| 14 | #[embassy::main] | ||
| 15 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 16 | let pwm = Pwm::new(p.PWM0, p.P0_13, NoPin, NoPin, NoPin); | ||
| 17 | // set_period doesnt actually set what you give it, because it only has a | ||
| 18 | // few options from the hardhware so be explicit instead | ||
| 19 | // Div128 is slowest, 125khz still crazy fast for our eyes | ||
| 20 | pwm.set_prescaler(Prescaler::Div128); | ||
| 21 | |||
| 22 | info!("pwm initialized!"); | ||
| 23 | |||
| 24 | // default max_duty if not specified is 1000 | ||
| 25 | // so 0 would be fully off and 1000 or above would be fully on | ||
| 26 | loop { | ||
| 27 | info!("100%"); | ||
| 28 | pwm.set_duty(0, 1000); | ||
| 29 | Timer::after(Duration::from_millis(5000)).await; | ||
| 30 | |||
| 31 | info!("25%"); | ||
| 32 | pwm.set_duty(0, 250); | ||
| 33 | Timer::after(Duration::from_millis(5000)).await; | ||
| 34 | |||
| 35 | info!("10%"); | ||
| 36 | pwm.set_duty(0, 100); | ||
| 37 | Timer::after(Duration::from_millis(5000)).await; | ||
| 38 | |||
| 39 | info!("5%"); | ||
| 40 | pwm.set_duty(0, 50); | ||
| 41 | Timer::after(Duration::from_millis(5000)).await; | ||
| 42 | |||
| 43 | info!("0%"); | ||
| 44 | pwm.set_duty(0, 0); | ||
| 45 | Timer::after(Duration::from_millis(5000)).await; | ||
| 46 | } | ||
| 47 | } | ||
