aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorrafael <[email protected]>2024-11-09 17:19:06 +0100
committerDario Nieuwenhuis <[email protected]>2024-12-02 23:52:03 +0100
commit75382336164c8284196eb1fad057050ba735a72d (patch)
tree51e52cc29ae768fa31029e111d2fd7111a2ee9b1 /examples
parentbc5e0d60b3861ca23b09dbd7fda703a47d3d60d2 (diff)
correct rp pwm dutycycle examples: desired frequency
Diffstat (limited to 'examples')
-rw-r--r--examples/rp/src/bin/pwm.rs12
-rw-r--r--examples/rp23/src/bin/pwm.rs10
-rw-r--r--examples/rp23/src/bin/pwm_tb6612fng_motor_driver.rs11
3 files changed, 22 insertions, 11 deletions
diff --git a/examples/rp/src/bin/pwm.rs b/examples/rp/src/bin/pwm.rs
index 791b88b5b..06b9313f2 100644
--- a/examples/rp/src/bin/pwm.rs
+++ b/examples/rp/src/bin/pwm.rs
@@ -48,11 +48,15 @@ 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. 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. 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: 50 // Every such wraparound is one PWM cycle. So here is how we get 25KHz:
51 let desired_freq_hz = 25_000;
52 let clock_freq_hz = embassy_rp::clocks::clk_sys_freq();
53 let divider = 16u8;
54 let period = (clock_freq_hz / (desired_freq_hz * divider as u32)) as u16 - 1;
55
51 let mut c = Config::default(); 56 let mut c = Config::default();
52 let pwm_freq = 25_000; // Hz, our desired frequency 57 c.top = period;
53 let clock_freq = embassy_rp::clocks::clk_sys_freq(); 58 c.divider = divider.into();
54 c.top = (clock_freq / pwm_freq) as u16 - 1; 59
55
56 let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone()); 60 let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
57 61
58 loop { 62 loop {
diff --git a/examples/rp23/src/bin/pwm.rs b/examples/rp23/src/bin/pwm.rs
index 5a4457158..ed3c94f15 100644
--- a/examples/rp23/src/bin/pwm.rs
+++ b/examples/rp23/src/bin/pwm.rs
@@ -53,10 +53,14 @@ async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
53 // If we aim for a specific frequency, here is how we can calculate the top value. 53 // If we aim for a specific frequency, here is how we can calculate the top value.
54 // The top value sets the period of the PWM cycle, so a counter goes from 0 to top and then wraps around to 0. 54 // The top value sets the period of the PWM cycle, so a counter goes from 0 to top and then wraps around to 0.
55 // Every such wraparound is one PWM cycle. So here is how we get 25KHz: 55 // Every such wraparound is one PWM cycle. So here is how we get 25KHz:
56 let desired_freq_hz = 25_000;
57 let clock_freq_hz = embassy_rp::clocks::clk_sys_freq();
58 let divider = 16u8;
59 let period = (clock_freq_hz / (desired_freq_hz * divider as u32)) as u16 - 1;
60
56 let mut c = Config::default(); 61 let mut c = Config::default();
57 let pwm_freq = 25_000; // Hz, our desired frequency 62 c.top = period;
58 let clock_freq = embassy_rp::clocks::clk_sys_freq(); 63 c.divider = divider.into();
59 c.top = (clock_freq / pwm_freq) as u16 - 1;
60 64
61 let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone()); 65 let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
62 66
diff --git a/examples/rp23/src/bin/pwm_tb6612fng_motor_driver.rs b/examples/rp23/src/bin/pwm_tb6612fng_motor_driver.rs
index 3fad2928c..0682888e8 100644
--- a/examples/rp23/src/bin/pwm_tb6612fng_motor_driver.rs
+++ b/examples/rp23/src/bin/pwm_tb6612fng_motor_driver.rs
@@ -40,10 +40,11 @@ async fn main(_spawner: Spawner) {
40 let s = split_resources!(p); 40 let s = split_resources!(p);
41 let r = s.motor; 41 let r = s.motor;
42 42
43 // we want a PWM frequency of 1KHz, especially cheaper motors do not respond well to higher frequencies 43 // we want a PWM frequency of 10KHz, especially cheaper motors do not respond well to higher frequencies
44 let pwm_freq = 1_000; // Hz, our desired frequency 44 let desired_freq_hz = 10_000;
45 let clock_freq = embassy_rp::clocks::clk_sys_freq(); 45 let clock_freq_hz = embassy_rp::clocks::clk_sys_freq();
46 let period = (clock_freq / pwm_freq) as u16 - 1; 46 let divider = 16u8;
47 let period = (clock_freq_hz / (desired_freq_hz * divider as u32)) as u16 - 1;
47 48
48 // we need a standby output and two motors to construct a full TB6612FNG 49 // we need a standby output and two motors to construct a full TB6612FNG
49 50
@@ -55,6 +56,7 @@ async fn main(_spawner: Spawner) {
55 let left_bckw = gpio::Output::new(r.left_backward_pin, gpio::Level::Low); 56 let left_bckw = gpio::Output::new(r.left_backward_pin, gpio::Level::Low);
56 let mut left_speed = pwm::Config::default(); 57 let mut left_speed = pwm::Config::default();
57 left_speed.top = period; 58 left_speed.top = period;
59 left_speed.divider = divider.into();
58 let left_pwm = pwm::Pwm::new_output_a(r.left_slice, r.left_pwm_pin, left_speed); 60 let left_pwm = pwm::Pwm::new_output_a(r.left_slice, r.left_pwm_pin, left_speed);
59 let left_motor = Motor::new(left_fwd, left_bckw, left_pwm).unwrap(); 61 let left_motor = Motor::new(left_fwd, left_bckw, left_pwm).unwrap();
60 62
@@ -63,6 +65,7 @@ async fn main(_spawner: Spawner) {
63 let right_bckw = gpio::Output::new(r.right_backward_pin, gpio::Level::Low); 65 let right_bckw = gpio::Output::new(r.right_backward_pin, gpio::Level::Low);
64 let mut right_speed = pwm::Config::default(); 66 let mut right_speed = pwm::Config::default();
65 right_speed.top = period; 67 right_speed.top = period;
68 right_speed.divider = divider.into();
66 let right_pwm = pwm::Pwm::new_output_b(r.right_slice, r.right_pwm_pin, right_speed); 69 let right_pwm = pwm::Pwm::new_output_b(r.right_slice, r.right_pwm_pin, right_speed);
67 let right_motor = Motor::new(right_fwd, right_bckw, right_pwm).unwrap(); 70 let right_motor = Motor::new(right_fwd, right_bckw, right_pwm).unwrap();
68 71