aboutsummaryrefslogtreecommitdiff
path: root/examples/rp
diff options
context:
space:
mode:
authorrafael <[email protected]>2024-10-21 22:42:18 +0200
committerrafael <[email protected]>2024-10-21 22:42:18 +0200
commit14e69309ebe25a76f67c38411c7a80a4d83da5ed (patch)
treede14c04891b7f148565cae50d81b5e9f3015675e /examples/rp
parent8dfc9ba1a3e3f69aedf5bce748783fb6a8f5e92e (diff)
add pwm frequency to examples
Diffstat (limited to 'examples/rp')
-rw-r--r--examples/rp/src/bin/pwm.rs10
1 files changed, 8 insertions, 2 deletions
diff --git a/examples/rp/src/bin/pwm.rs b/examples/rp/src/bin/pwm.rs
index 862a7da22..791b88b5b 100644
--- a/examples/rp/src/bin/pwm.rs
+++ b/examples/rp/src/bin/pwm.rs
@@ -45,8 +45,14 @@ async fn pwm_set_config(slice4: PWM_SLICE4, pin25: PIN_25) {
45/// Using GP4 in Slice2, make sure to use an appropriate resistor. 45/// Using GP4 in Slice2, make sure to use an appropriate resistor.
46#[embassy_executor::task] 46#[embassy_executor::task]
47async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) { 47async 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.
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:
48 let mut c = Config::default(); 51 let mut c = Config::default();
49 c.top = 32_768; 52 let pwm_freq = 25_000; // Hz, our desired frequency
53 let clock_freq = embassy_rp::clocks::clk_sys_freq();
54 c.top = (clock_freq / pwm_freq) as u16 - 1;
55
50 let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone()); 56 let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
51 57
52 loop { 58 loop {
@@ -59,7 +65,7 @@ async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
59 Timer::after_secs(1).await; 65 Timer::after_secs(1).await;
60 66
61 // 25% duty cycle. Expressed as 32768/4 = 8192. 67 // 25% duty cycle. Expressed as 32768/4 = 8192.
62 pwm.set_duty_cycle(8_192).unwrap(); 68 pwm.set_duty_cycle(c.top / 4).unwrap();
63 Timer::after_secs(1).await; 69 Timer::after_secs(1).await;
64 70
65 // 0% duty cycle, fully off. 71 // 0% duty cycle, fully off.