correct rp pwm dutycycle examples: desired frequency

This commit is contained in:
rafael 2024-11-09 17:19:06 +01:00 committed by Dario Nieuwenhuis
parent bc5e0d60b3
commit 7538233616
3 changed files with 22 additions and 11 deletions

View File

@ -48,11 +48,15 @@ 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 desired_freq_hz = 25_000;
let clock_freq_hz = embassy_rp::clocks::clk_sys_freq();
let divider = 16u8;
let period = (clock_freq_hz / (desired_freq_hz * divider as u32)) as u16 - 1;
let mut c = Config::default();
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;
c.top = period;
c.divider = divider.into();
let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
loop {

View File

@ -53,10 +53,14 @@ 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 desired_freq_hz = 25_000;
let clock_freq_hz = embassy_rp::clocks::clk_sys_freq();
let divider = 16u8;
let period = (clock_freq_hz / (desired_freq_hz * divider as u32)) as u16 - 1;
let mut c = Config::default();
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;
c.top = period;
c.divider = divider.into();
let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());

View File

@ -40,10 +40,11 @@ async fn main(_spawner: Spawner) {
let s = split_resources!(p);
let r = s.motor;
// we want a PWM frequency of 1KHz, especially cheaper motors do not respond well to higher frequencies
let pwm_freq = 1_000; // Hz, our desired frequency
let clock_freq = embassy_rp::clocks::clk_sys_freq();
let period = (clock_freq / pwm_freq) as u16 - 1;
// we want a PWM frequency of 10KHz, especially cheaper motors do not respond well to higher frequencies
let desired_freq_hz = 10_000;
let clock_freq_hz = embassy_rp::clocks::clk_sys_freq();
let divider = 16u8;
let period = (clock_freq_hz / (desired_freq_hz * divider as u32)) as u16 - 1;
// we need a standby output and two motors to construct a full TB6612FNG
@ -55,6 +56,7 @@ async fn main(_spawner: Spawner) {
let left_bckw = gpio::Output::new(r.left_backward_pin, gpio::Level::Low);
let mut left_speed = pwm::Config::default();
left_speed.top = period;
left_speed.divider = divider.into();
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();
@ -63,6 +65,7 @@ async fn main(_spawner: Spawner) {
let right_bckw = gpio::Output::new(r.right_backward_pin, gpio::Level::Low);
let mut right_speed = pwm::Config::default();
right_speed.top = period;
right_speed.divider = divider.into();
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();