From 99d4f5f876d1fb82c4ef5f78d378044da5f7c6ed Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Mon, 28 Oct 2024 21:04:15 +0100 Subject: [PATCH] Fix buzzer frequency calculation --- src/buzzer.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/buzzer.c b/src/buzzer.c index 65a157b..f7243ef 100644 --- a/src/buzzer.c +++ b/src/buzzer.c @@ -8,7 +8,7 @@ #include "buzzer.h" -#define FREQ_BASE 24414063 // round(125 MHz [CPU frequency] * 100 [scaling] / 512 [PWM steps]) +#define FREQ_BASE 3906250 // round(125 MHz [CPU frequency] * 16 [scaling] / 512 [PWM steps]) uint m_slice_num; @@ -23,14 +23,14 @@ void buzzer_init(void) pwm_set_clkdiv_int_frac(m_slice_num, 244, (14 << 4) / 100); // 125 MHz / 244.14 / 512 = 1 kHz gpio_set_function(BUZZER_PIN, GPIO_FUNC_PWM); pwm_set_wrap(m_slice_num, 512); - pwm_set_chan_level(m_slice_num, PWM_CHAN_B, 1 * 512 / 100); + pwm_set_chan_level(m_slice_num, PWM_CHAN_B, 1 * 512 / 100); // 1% duty cycle is sufficient for full voltage swing } void buzzer_on(uint16_t freq_hz) { - uint32_t divider_x100 = FREQ_BASE / freq_hz; // = 100 * 125e6 / 512 / freq_hz + uint32_t divider_x16 = FREQ_BASE / freq_hz; // = 16 * 125e6 / 512 / freq_hz - pwm_set_clkdiv_int_frac(m_slice_num, divider_x100 / 100, divider_x100 % 100); + pwm_set_clkdiv_int_frac(m_slice_num, divider_x16 / 16, divider_x16 % 16); gpio_set_function(BUZZER_PIN, GPIO_FUNC_PWM); pwm_set_enabled(m_slice_num, true); }