diff options
| -rw-r--r-- | examples/rp/src/bin/pwm.rs | 52 | ||||
| -rw-r--r-- | examples/rp23/src/bin/pwm.rs | 8 |
2 files changed, 50 insertions, 10 deletions
diff --git a/examples/rp/src/bin/pwm.rs b/examples/rp/src/bin/pwm.rs index 26e233260..862a7da22 100644 --- a/examples/rp/src/bin/pwm.rs +++ b/examples/rp/src/bin/pwm.rs | |||
| @@ -1,24 +1,36 @@ | |||
| 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 RP2040 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] |
| 7 | 9 | ||
| 8 | use defmt::*; | 10 | use defmt::*; |
| 9 | use embassy_executor::Spawner; | 11 | use embassy_executor::Spawner; |
| 10 | use embassy_rp::pwm::{Config, Pwm}; | 12 | use embassy_rp::peripherals::{PIN_25, PIN_4, PWM_SLICE2, PWM_SLICE4}; |
| 13 | use embassy_rp::pwm::{Config, Pwm, SetDutyCycle}; | ||
| 11 | use embassy_time::Timer; | 14 | use embassy_time::Timer; |
| 12 | use {defmt_rtt as _, panic_probe as _}; | 15 | use {defmt_rtt as _, panic_probe as _}; |
| 13 | 16 | ||
| 14 | #[embassy_executor::main] | 17 | #[embassy_executor::main] |
| 15 | async fn main(_spawner: Spawner) { | 18 | async fn main(spawner: Spawner) { |
| 16 | let p = embassy_rp::init(Default::default()); | 19 | let p = embassy_rp::init(Default::default()); |
| 20 | spawner.spawn(pwm_set_config(p.PWM_SLICE4, p.PIN_25)).unwrap(); | ||
| 21 | spawner.spawn(pwm_set_dutycycle(p.PWM_SLICE2, p.PIN_4)).unwrap(); | ||
| 22 | } | ||
| 17 | 23 | ||
| 18 | let mut c: Config = Default::default(); | 24 | /// Demonstrate PWM by modifying & applying the config |
| 19 | c.top = 0x8000; | 25 | /// |
| 26 | /// Using the onboard led, if You are using a different Board than plain Pico2 (i.e. W variant) | ||
| 27 | /// you must use another slice & pin and an appropriate resistor. | ||
| 28 | #[embassy_executor::task] | ||
| 29 | async fn pwm_set_config(slice4: PWM_SLICE4, pin25: PIN_25) { | ||
| 30 | let mut c = Config::default(); | ||
| 31 | c.top = 32_768; | ||
| 20 | c.compare_b = 8; | 32 | c.compare_b = 8; |
| 21 | let mut pwm = Pwm::new_output_b(p.PWM_SLICE4, p.PIN_25, c.clone()); | 33 | let mut pwm = Pwm::new_output_b(slice4, pin25, c.clone()); |
| 22 | 34 | ||
| 23 | loop { | 35 | loop { |
| 24 | info!("current LED duty cycle: {}/32768", c.compare_b); | 36 | info!("current LED duty cycle: {}/32768", c.compare_b); |
| @@ -27,3 +39,31 @@ async fn main(_spawner: Spawner) { | |||
| 27 | pwm.set_config(&c); | 39 | pwm.set_config(&c); |
| 28 | } | 40 | } |
| 29 | } | 41 | } |
| 42 | |||
| 43 | /// Demonstrate PWM by setting duty cycle | ||
| 44 | /// | ||
| 45 | /// Using GP4 in Slice2, make sure to use an appropriate resistor. | ||
| 46 | #[embassy_executor::task] | ||
| 47 | async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) { | ||
| 48 | let mut c = Config::default(); | ||
| 49 | c.top = 32_768; | ||
| 50 | let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone()); | ||
| 51 | |||
| 52 | loop { | ||
| 53 | // 100% duty cycle, fully on | ||
| 54 | pwm.set_duty_cycle_fully_on().unwrap(); | ||
| 55 | Timer::after_secs(1).await; | ||
| 56 | |||
| 57 | // 66% duty cycle. Expressed as simple percentage. | ||
| 58 | pwm.set_duty_cycle_percent(66).unwrap(); | ||
| 59 | Timer::after_secs(1).await; | ||
| 60 | |||
| 61 | // 25% duty cycle. Expressed as 32768/4 = 8192. | ||
| 62 | pwm.set_duty_cycle(8_192).unwrap(); | ||
| 63 | Timer::after_secs(1).await; | ||
| 64 | |||
| 65 | // 0% duty cycle, fully off. | ||
| 66 | pwm.set_duty_cycle_fully_off().unwrap(); | ||
| 67 | Timer::after_secs(1).await; | ||
| 68 | } | ||
| 69 | } | ||
diff --git a/examples/rp23/src/bin/pwm.rs b/examples/rp23/src/bin/pwm.rs index 1dd5ca3de..838eee625 100644 --- a/examples/rp23/src/bin/pwm.rs +++ b/examples/rp23/src/bin/pwm.rs | |||
| @@ -59,12 +59,12 @@ async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) { | |||
| 59 | pwm.set_duty_cycle_fully_on().unwrap(); | 59 | pwm.set_duty_cycle_fully_on().unwrap(); |
| 60 | Timer::after_secs(1).await; | 60 | Timer::after_secs(1).await; |
| 61 | 61 | ||
| 62 | // 50% duty cycle, half on. Expressed as simple percentage. | 62 | // 66% duty cycle. Expressed as simple percentage. |
| 63 | pwm.set_duty_cycle_percent(50).unwrap(); | 63 | pwm.set_duty_cycle_percent(66).unwrap(); |
| 64 | Timer::after_secs(1).await; | 64 | Timer::after_secs(1).await; |
| 65 | 65 | ||
| 66 | // 25% duty cycle, quarter on. Expressed as (duty / max_duty) | 66 | // 25% duty cycle. Expressed as 32768/4 = 8192. |
| 67 | pwm.set_duty_cycle(8_192 / c.top).unwrap(); | 67 | pwm.set_duty_cycle(8_192).unwrap(); |
| 68 | Timer::after_secs(1).await; | 68 | Timer::after_secs(1).await; |
| 69 | 69 | ||
| 70 | // 0% duty cycle, fully off. | 70 | // 0% duty cycle, fully off. |
