aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/rp23/src/bin/pwm.rs55
1 files changed, 48 insertions, 7 deletions
diff --git a/examples/rp23/src/bin/pwm.rs b/examples/rp23/src/bin/pwm.rs
index 15eae09ee..7314ee637 100644
--- a/examples/rp23/src/bin/pwm.rs
+++ b/examples/rp23/src/bin/pwm.rs
@@ -1,6 +1,8 @@
1//! This example shows how to use PWM (Pulse Width Modulation) in the RP2040 chip. 1//! This example shows how to use PWM (Pulse Width Modulation) in the RP235x chip.
2//! 2//!
3//! The LED on the RP Pico W board is connected differently. Add a LED and resistor to another pin. 3//! We demonstrate two ways of using PWM:
4//! 1. Via config
5//! 2. Via setting a duty cycle
4 6
5#![no_std] 7#![no_std]
6#![no_main] 8#![no_main]
@@ -8,8 +10,10 @@
8use defmt::*; 10use defmt::*;
9use embassy_executor::Spawner; 11use embassy_executor::Spawner;
10use embassy_rp::block::ImageDef; 12use embassy_rp::block::ImageDef;
11use embassy_rp::pwm::{Config, Pwm}; 13use embassy_rp::peripherals::{PIN_25, PIN_4, PWM_SLICE2, PWM_SLICE4};
14use embassy_rp::pwm::{Config, Pwm, SetDutyCycle};
12use embassy_time::Timer; 15use embassy_time::Timer;
16// use embedded_hal_1::pwm::SetDutyCycle;
13use {defmt_rtt as _, panic_probe as _}; 17use {defmt_rtt as _, panic_probe as _};
14 18
15#[link_section = ".start_block"] 19#[link_section = ".start_block"]
@@ -17,13 +21,22 @@ use {defmt_rtt as _, panic_probe as _};
17pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe(); 21pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe();
18 22
19#[embassy_executor::main] 23#[embassy_executor::main]
20async fn main(_spawner: Spawner) { 24async fn main(spawner: Spawner) {
21 let p = embassy_rp::init(Default::default()); 25 let p = embassy_rp::init(Default::default());
26 spawner.spawn(pwm_set_config(p.PWM_SLICE4, p.PIN_25)).unwrap();
27 spawner.spawn(pwm_set_dutycycle(p.PWM_SLICE2, p.PIN_4)).unwrap();
28}
22 29
23 let mut c: Config = Default::default(); 30/// Demonstrate PWM by modifying & applying the config
24 c.top = 0x8000; 31///
32/// Using the onboard led, if You are using a different Board than plain Pico2 (i.e. W variant)
33/// you must use another slice & pin and an appropriate resistor.
34#[embassy_executor::task]
35async fn pwm_set_config(slice4: PWM_SLICE4, pin25: PIN_25) {
36 let mut c = Config::default();
37 c.top = 32_768;
25 c.compare_b = 8; 38 c.compare_b = 8;
26 let mut pwm = Pwm::new_output_b(p.PWM_SLICE4, p.PIN_25, c.clone()); 39 let mut pwm = Pwm::new_output_b(slice4, pin25, c.clone());
27 40
28 loop { 41 loop {
29 info!("current LED duty cycle: {}/32768", c.compare_b); 42 info!("current LED duty cycle: {}/32768", c.compare_b);
@@ -32,3 +45,31 @@ async fn main(_spawner: Spawner) {
32 pwm.set_config(&c); 45 pwm.set_config(&c);
33 } 46 }
34} 47}
48
49/// Demonstrate PWM by setting duty cycle
50///
51/// Using GP4 in Slice2, make sure to use an appropriate resistor.
52#[embassy_executor::task]
53async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
54 let mut c = Config::default();
55 c.top = 32_768;
56 let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
57
58 loop {
59 // 100% duty cycle, fully on
60 pwm.set_duty_cycle_fully_on().unwrap();
61 Timer::after_secs(1).await;
62
63 // 50% duty cycle, half on. Expressed as simple percentage.
64 pwm.set_duty_cycle_percent(50).unwrap();
65 Timer::after_secs(1).await;
66
67 // 25% duty cycle, quarter on. Expressed as (duty / max_duty)
68 pwm.set_duty_cycle(8_192 / c.top).unwrap();
69 Timer::after_secs(1).await;
70
71 // 0% duty cycle, fully off.
72 pwm.set_duty_cycle_fully_off().unwrap();
73 Timer::after_secs(1).await;
74 }
75}