diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-12-02 23:17:17 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-12-02 23:17:17 +0000 |
| commit | 4b901f9a5b6706841c01dfea149504bc8abf33eb (patch) | |
| tree | 3cd4bfeabf5c201108de539189f506cadc23db38 /examples/rp | |
| parent | 5fabf7d462a2dd6c6aaca5de7631dd4a9d91bcff (diff) | |
| parent | ed10b9de7b0292a23036d3144146cff8137caf4f (diff) | |
Merge pull request #3517 from 1-rafael-1/correct-pwm-dutycycle-examples
correct rp pwm dutycycle examples: desired frequency
Diffstat (limited to 'examples/rp')
| -rw-r--r-- | examples/rp/src/bin/pwm.rs | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/examples/rp/src/bin/pwm.rs b/examples/rp/src/bin/pwm.rs index 791b88b5b..2f5f94870 100644 --- a/examples/rp/src/bin/pwm.rs +++ b/examples/rp/src/bin/pwm.rs | |||
| @@ -48,10 +48,14 @@ 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; | ||
| 55 | 59 | ||
| 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 | ||
