diff options
Diffstat (limited to 'examples/rp')
| -rw-r--r-- | examples/rp/src/bin/pwm.rs | 58 |
1 files changed, 52 insertions, 6 deletions
diff --git a/examples/rp/src/bin/pwm.rs b/examples/rp/src/bin/pwm.rs index 26e233260..791b88b5b 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,37 @@ 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 | // If we aim for a specific frequency, here is how we can calculate the top value. | ||
| 49 | // The top value sets the period of the PWM cycle, so a counter goes from 0 to top and then wraps around to 0. | ||
| 50 | // Every such wraparound is one PWM cycle. So here is how we get 25KHz: | ||
| 51 | let mut c = Config::default(); | ||
| 52 | let pwm_freq = 25_000; // Hz, our desired frequency | ||
| 53 | let clock_freq = embassy_rp::clocks::clk_sys_freq(); | ||
| 54 | c.top = (clock_freq / pwm_freq) as u16 - 1; | ||
| 55 | |||
| 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 | // 66% duty cycle. Expressed as simple percentage. | ||
| 64 | pwm.set_duty_cycle_percent(66).unwrap(); | ||
| 65 | Timer::after_secs(1).await; | ||
| 66 | |||
| 67 | // 25% duty cycle. Expressed as 32768/4 = 8192. | ||
| 68 | pwm.set_duty_cycle(c.top / 4).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 | } | ||
