diff options
| author | rafael <[email protected]> | 2024-10-21 11:55:10 +0200 |
|---|---|---|
| committer | rafael <[email protected]> | 2024-10-21 11:55:10 +0200 |
| commit | f32b0fbc3b614ab79b7756b49e44a380e5e60192 (patch) | |
| tree | 58b0a99f7693bccb72f1f9ffe56fa874354df461 /examples | |
| parent | 693bd8c6ded003f78ae89fa2700ba7282fee64d0 (diff) | |
change existing pwm example to reflect both existing ways to use pwm output
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp23/src/bin/pwm.rs | 55 |
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 @@ | |||
| 8 | use defmt::*; | 10 | use defmt::*; |
| 9 | use embassy_executor::Spawner; | 11 | use embassy_executor::Spawner; |
| 10 | use embassy_rp::block::ImageDef; | 12 | use embassy_rp::block::ImageDef; |
| 11 | use embassy_rp::pwm::{Config, Pwm}; | 13 | use embassy_rp::peripherals::{PIN_25, PIN_4, PWM_SLICE2, PWM_SLICE4}; |
| 14 | use embassy_rp::pwm::{Config, Pwm, SetDutyCycle}; | ||
| 12 | use embassy_time::Timer; | 15 | use embassy_time::Timer; |
| 16 | // use embedded_hal_1::pwm::SetDutyCycle; | ||
| 13 | use {defmt_rtt as _, panic_probe as _}; | 17 | use {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 _}; | |||
| 17 | pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe(); | 21 | pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe(); |
| 18 | 22 | ||
| 19 | #[embassy_executor::main] | 23 | #[embassy_executor::main] |
| 20 | async fn main(_spawner: Spawner) { | 24 | async 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] | ||
| 35 | async 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] | ||
| 53 | async 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 | } | ||
