mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-22 06:42:32 +00:00
also add to the rp pwm example
This commit is contained in:
parent
d92fb002ec
commit
8dfc9ba1a3
@ -1,24 +1,36 @@
|
|||||||
//! This example shows how to use PWM (Pulse Width Modulation) in the RP2040 chip.
|
//! This example shows how to use PWM (Pulse Width Modulation) in the RP2040 chip.
|
||||||
//!
|
//!
|
||||||
//! The LED on the RP Pico W board is connected differently. Add a LED and resistor to another pin.
|
//! We demonstrate two ways of using PWM:
|
||||||
|
//! 1. Via config
|
||||||
|
//! 2. Via setting a duty cycle
|
||||||
|
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use defmt::*;
|
use defmt::*;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_rp::pwm::{Config, Pwm};
|
use embassy_rp::peripherals::{PIN_25, PIN_4, PWM_SLICE2, PWM_SLICE4};
|
||||||
|
use embassy_rp::pwm::{Config, Pwm, SetDutyCycle};
|
||||||
use embassy_time::Timer;
|
use embassy_time::Timer;
|
||||||
use {defmt_rtt as _, panic_probe as _};
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
#[embassy_executor::main]
|
#[embassy_executor::main]
|
||||||
async fn main(_spawner: Spawner) {
|
async fn main(spawner: Spawner) {
|
||||||
let p = embassy_rp::init(Default::default());
|
let p = embassy_rp::init(Default::default());
|
||||||
|
spawner.spawn(pwm_set_config(p.PWM_SLICE4, p.PIN_25)).unwrap();
|
||||||
|
spawner.spawn(pwm_set_dutycycle(p.PWM_SLICE2, p.PIN_4)).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
let mut c: Config = Default::default();
|
/// Demonstrate PWM by modifying & applying the config
|
||||||
c.top = 0x8000;
|
///
|
||||||
|
/// Using the onboard led, if You are using a different Board than plain Pico2 (i.e. W variant)
|
||||||
|
/// you must use another slice & pin and an appropriate resistor.
|
||||||
|
#[embassy_executor::task]
|
||||||
|
async fn pwm_set_config(slice4: PWM_SLICE4, pin25: PIN_25) {
|
||||||
|
let mut c = Config::default();
|
||||||
|
c.top = 32_768;
|
||||||
c.compare_b = 8;
|
c.compare_b = 8;
|
||||||
let mut pwm = Pwm::new_output_b(p.PWM_SLICE4, p.PIN_25, c.clone());
|
let mut pwm = Pwm::new_output_b(slice4, pin25, c.clone());
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
info!("current LED duty cycle: {}/32768", c.compare_b);
|
info!("current LED duty cycle: {}/32768", c.compare_b);
|
||||||
@ -27,3 +39,31 @@ async fn main(_spawner: Spawner) {
|
|||||||
pwm.set_config(&c);
|
pwm.set_config(&c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Demonstrate PWM by setting duty cycle
|
||||||
|
///
|
||||||
|
/// Using GP4 in Slice2, make sure to use an appropriate resistor.
|
||||||
|
#[embassy_executor::task]
|
||||||
|
async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
|
||||||
|
let mut c = Config::default();
|
||||||
|
c.top = 32_768;
|
||||||
|
let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
|
||||||
|
|
||||||
|
loop {
|
||||||
|
// 100% duty cycle, fully on
|
||||||
|
pwm.set_duty_cycle_fully_on().unwrap();
|
||||||
|
Timer::after_secs(1).await;
|
||||||
|
|
||||||
|
// 66% duty cycle. Expressed as simple percentage.
|
||||||
|
pwm.set_duty_cycle_percent(66).unwrap();
|
||||||
|
Timer::after_secs(1).await;
|
||||||
|
|
||||||
|
// 25% duty cycle. Expressed as 32768/4 = 8192.
|
||||||
|
pwm.set_duty_cycle(8_192).unwrap();
|
||||||
|
Timer::after_secs(1).await;
|
||||||
|
|
||||||
|
// 0% duty cycle, fully off.
|
||||||
|
pwm.set_duty_cycle_fully_off().unwrap();
|
||||||
|
Timer::after_secs(1).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -59,12 +59,12 @@ async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
|
|||||||
pwm.set_duty_cycle_fully_on().unwrap();
|
pwm.set_duty_cycle_fully_on().unwrap();
|
||||||
Timer::after_secs(1).await;
|
Timer::after_secs(1).await;
|
||||||
|
|
||||||
// 50% duty cycle, half on. Expressed as simple percentage.
|
// 66% duty cycle. Expressed as simple percentage.
|
||||||
pwm.set_duty_cycle_percent(50).unwrap();
|
pwm.set_duty_cycle_percent(66).unwrap();
|
||||||
Timer::after_secs(1).await;
|
Timer::after_secs(1).await;
|
||||||
|
|
||||||
// 25% duty cycle, quarter on. Expressed as (duty / max_duty)
|
// 25% duty cycle. Expressed as 32768/4 = 8192.
|
||||||
pwm.set_duty_cycle(8_192 / c.top).unwrap();
|
pwm.set_duty_cycle(8_192).unwrap();
|
||||||
Timer::after_secs(1).await;
|
Timer::after_secs(1).await;
|
||||||
|
|
||||||
// 0% duty cycle, fully off.
|
// 0% duty cycle, fully off.
|
||||||
|
Loading…
Reference in New Issue
Block a user