Fix buzzer frequency calculation

This commit is contained in:
Thomas Kolb 2024-10-28 21:04:15 +01:00
parent bc8e454242
commit 99d4f5f876

View file

@ -8,7 +8,7 @@
#include "buzzer.h" #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; 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 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); gpio_set_function(BUZZER_PIN, GPIO_FUNC_PWM);
pwm_set_wrap(m_slice_num, 512); 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) 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); gpio_set_function(BUZZER_PIN, GPIO_FUNC_PWM);
pwm_set_enabled(m_slice_num, true); pwm_set_enabled(m_slice_num, true);
} }