mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-22 06:42:32 +00:00
add pwm frequency to examples
This commit is contained in:
parent
8dfc9ba1a3
commit
14e69309eb
@ -45,8 +45,14 @@ async fn pwm_set_config(slice4: PWM_SLICE4, pin25: PIN_25) {
|
|||||||
/// Using GP4 in Slice2, make sure to use an appropriate resistor.
|
/// Using GP4 in Slice2, make sure to use an appropriate resistor.
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
|
async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
|
||||||
|
// If we aim for a specific frequency, here is how we can calculate the top value.
|
||||||
|
// The top value sets the period of the PWM cycle, so a counter goes from 0 to top and then wraps around to 0.
|
||||||
|
// Every such wraparound is one PWM cycle. So here is how we get 25KHz:
|
||||||
let mut c = Config::default();
|
let mut c = Config::default();
|
||||||
c.top = 32_768;
|
let pwm_freq = 25_000; // Hz, our desired frequency
|
||||||
|
let clock_freq = embassy_rp::clocks::clk_sys_freq();
|
||||||
|
c.top = (clock_freq / pwm_freq) as u16 - 1;
|
||||||
|
|
||||||
let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
|
let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@ -59,7 +65,7 @@ async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
|
|||||||
Timer::after_secs(1).await;
|
Timer::after_secs(1).await;
|
||||||
|
|
||||||
// 25% duty cycle. Expressed as 32768/4 = 8192.
|
// 25% duty cycle. Expressed as 32768/4 = 8192.
|
||||||
pwm.set_duty_cycle(8_192).unwrap();
|
pwm.set_duty_cycle(c.top / 4).unwrap();
|
||||||
Timer::after_secs(1).await;
|
Timer::after_secs(1).await;
|
||||||
|
|
||||||
// 0% duty cycle, fully off.
|
// 0% duty cycle, fully off.
|
||||||
|
@ -50,8 +50,14 @@ async fn pwm_set_config(slice4: PWM_SLICE4, pin25: PIN_25) {
|
|||||||
/// Using GP4 in Slice2, make sure to use an appropriate resistor.
|
/// Using GP4 in Slice2, make sure to use an appropriate resistor.
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
|
async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
|
||||||
|
// If we aim for a specific frequency, here is how we can calculate the top value.
|
||||||
|
// The top value sets the period of the PWM cycle, so a counter goes from 0 to top and then wraps around to 0.
|
||||||
|
// Every such wraparound is one PWM cycle. So here is how we get 25KHz:
|
||||||
let mut c = Config::default();
|
let mut c = Config::default();
|
||||||
c.top = 32_768;
|
let pwm_freq = 25_000; // Hz, our desired frequency
|
||||||
|
let clock_freq = embassy_rp::clocks::clk_sys_freq();
|
||||||
|
c.top = (clock_freq / pwm_freq) as u16 - 1;
|
||||||
|
|
||||||
let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
|
let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@ -64,7 +70,7 @@ async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
|
|||||||
Timer::after_secs(1).await;
|
Timer::after_secs(1).await;
|
||||||
|
|
||||||
// 25% duty cycle. Expressed as 32768/4 = 8192.
|
// 25% duty cycle. Expressed as 32768/4 = 8192.
|
||||||
pwm.set_duty_cycle(8_192).unwrap();
|
pwm.set_duty_cycle(c.top / 4).unwrap();
|
||||||
Timer::after_secs(1).await;
|
Timer::after_secs(1).await;
|
||||||
|
|
||||||
// 0% duty cycle, fully off.
|
// 0% duty cycle, fully off.
|
||||||
|
@ -16,12 +16,6 @@ use embassy_time::{Duration, Timer};
|
|||||||
use tb6612fng::{DriveCommand, Motor, Tb6612fng};
|
use tb6612fng::{DriveCommand, Motor, Tb6612fng};
|
||||||
use {defmt_rtt as _, panic_probe as _};
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
/// Maximum PWM value (fully on)
|
|
||||||
const PWM_MAX: u16 = 50000;
|
|
||||||
|
|
||||||
/// Minimum PWM value (fully off)
|
|
||||||
const PWM_MIN: u16 = 0;
|
|
||||||
|
|
||||||
#[link_section = ".start_block"]
|
#[link_section = ".start_block"]
|
||||||
#[used]
|
#[used]
|
||||||
pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe();
|
pub static IMAGE_DEF: ImageDef = ImageDef::secure_exe();
|
||||||
@ -46,6 +40,11 @@ async fn main(_spawner: Spawner) {
|
|||||||
let s = split_resources!(p);
|
let s = split_resources!(p);
|
||||||
let r = s.motor;
|
let r = s.motor;
|
||||||
|
|
||||||
|
// we want a PWM frequency of 25KHz
|
||||||
|
let pwm_freq = 25_000; // Hz, our desired frequency
|
||||||
|
let clock_freq = embassy_rp::clocks::clk_sys_freq();
|
||||||
|
let period = (clock_freq / pwm_freq) as u16 - 1;
|
||||||
|
|
||||||
// we need a standby output and two motors to construct a full TB6612FNG
|
// we need a standby output and two motors to construct a full TB6612FNG
|
||||||
|
|
||||||
// standby pin
|
// standby pin
|
||||||
@ -55,8 +54,7 @@ async fn main(_spawner: Spawner) {
|
|||||||
let left_fwd = gpio::Output::new(r.left_forward_pin, gpio::Level::Low);
|
let left_fwd = gpio::Output::new(r.left_forward_pin, gpio::Level::Low);
|
||||||
let left_bckw = gpio::Output::new(r.left_backward_pin, gpio::Level::Low);
|
let left_bckw = gpio::Output::new(r.left_backward_pin, gpio::Level::Low);
|
||||||
let mut left_speed = pwm::Config::default();
|
let mut left_speed = pwm::Config::default();
|
||||||
left_speed.top = PWM_MAX;
|
left_speed.top = period;
|
||||||
left_speed.compare_a = PWM_MIN;
|
|
||||||
let left_pwm = pwm::Pwm::new_output_a(r.left_slice, r.left_pwm_pin, left_speed);
|
let left_pwm = pwm::Pwm::new_output_a(r.left_slice, r.left_pwm_pin, left_speed);
|
||||||
let left_motor = Motor::new(left_fwd, left_bckw, left_pwm).unwrap();
|
let left_motor = Motor::new(left_fwd, left_bckw, left_pwm).unwrap();
|
||||||
|
|
||||||
@ -64,8 +62,7 @@ async fn main(_spawner: Spawner) {
|
|||||||
let right_fwd = gpio::Output::new(r.right_forward_pin, gpio::Level::Low);
|
let right_fwd = gpio::Output::new(r.right_forward_pin, gpio::Level::Low);
|
||||||
let right_bckw = gpio::Output::new(r.right_backward_pin, gpio::Level::Low);
|
let right_bckw = gpio::Output::new(r.right_backward_pin, gpio::Level::Low);
|
||||||
let mut right_speed = pwm::Config::default();
|
let mut right_speed = pwm::Config::default();
|
||||||
right_speed.top = PWM_MAX;
|
right_speed.top = period;
|
||||||
right_speed.compare_b = PWM_MIN;
|
|
||||||
let right_pwm = pwm::Pwm::new_output_b(r.right_slice, r.right_pwm_pin, right_speed);
|
let right_pwm = pwm::Pwm::new_output_b(r.right_slice, r.right_pwm_pin, right_speed);
|
||||||
let right_motor = Motor::new(right_fwd, right_bckw, right_pwm).unwrap();
|
let right_motor = Motor::new(right_fwd, right_bckw, right_pwm).unwrap();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user