This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Issue in buzzer beep

Hello everyone ,

I have written the program for getting the buzzer Beep sound and then i compiled the program without any errors and warnings but i am not getting the beep sound ,i don't understand what is going wrong ,any suggesstions will be helpful for me

#define FREQ_HALF_NOTE_FACTOR 1.059463f

 void set_frequency_and_duty_cycle(uint32_t frequency, uint32_t duty_cycle_percent)
{
    
nrf_pwm_set_max_value((16000000 + (frequency / 2)) / frequency);
    
nrf_pwm_set_value(0, (16000000 / frequency) * duty_cycle_percent / 100);

}

void pwm_init()

{
    nrf_pwm_config_t pwm_config = PWM_DEFAULT_CONFIG;
    
    pwm_config.mode             = PWM_MODE_BUZZER_255;

    pwm_config.num_channels     = 1;

    pwm_config.gpio_num[0]      = 14;
    
    // Initialize the PWM library

    nrf_pwm_init(&pwm_config); 
   
}

/**@brief  Application main function.
 */
int main(void)
{
	float frequency = 440.0f;
	nrf_gpio_cfg_output(LED_1);
    
    // Start the external 16 MHz clock for a more accurate PWM frequency
    NRF_CLOCK->TASKS_HFCLKSTART = 1;
    
    pwm_init();
    
    while (true)
    {
        set_frequency_and_duty_cycle((uint32_t)(frequency + 0.5f), 50);
        
        frequency *= FREQ_HALF_NOTE_FACTOR;
        if(frequency > (440.0f * 4.0f)) frequency = 440.0f;
            
        nrf_gpio_pin_toggle(LED_1);
        	
        nrf_delay_ms(5000);
    }
}

Thank you

Parents
  • If wonder if it could be a faulty high frequency crystal. The blinky example is using the internal 16MHz RC oscillator, but the PWM is using the external crystal. Are you able to start the crystal and check if it is running correctly? You can do it like this just use HFCLK instead of LFCLK.

    Or you can remove the line:

    NRF_CLOCK->TASKS_HFCLKSTART = 1;
    

    at the expense of some frequency accuracy.

Reply
  • If wonder if it could be a faulty high frequency crystal. The blinky example is using the internal 16MHz RC oscillator, but the PWM is using the external crystal. Are you able to start the crystal and check if it is running correctly? You can do it like this just use HFCLK instead of LFCLK.

    Or you can remove the line:

    NRF_CLOCK->TASKS_HFCLKSTART = 1;
    

    at the expense of some frequency accuracy.

Children
No Data
Related