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

PWM with a piezo buzzer,How can I change the PWM frequency?

I use the nrf51822qfaa,s110 v8.00,SDK V10.0. I use PWM module with a piezo buzzer. Now I need change the buzzer frequency, How can I change the PWM frequency?

for example:

/* 2-channel PWM, 200Hz, output on DK LED pins. */
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, BSP_LED_0, BSP_LED_1);
Parents
  • You cannot change the PWM frequency without un-initializing and then initialize the PWM instance again.

  • Is this OK?

     /*
     init, Once executed
     */
    
     void pwm_init(void)  
     {
        /* 1-channel PWM, 200Hz, output on DK LED pins. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(5000L, BSP_LED_0);
        
        /* Switch the polarity of the second channel. */
        pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
        
        /* Initialize and enable PWM. */
        err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&PWM1);
      }
      
    
      
     /*
     pwm_change_frequency,Can be executed multiple times
     */
       void pwm_change_frequency(uint16_t freq)  
     {
     
      app_pwm_disenable(&PWM1);
      app_pwm_uninit(&PWM1);  
         /* 1-channel PWM, 200Hz, output on DK LED pins. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(freq, BSP_LED_0);
        
        /* Switch the polarity of the second channel. */
        pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
        
        /* Initialize and enable PWM. */
        err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&PWM1);
        
        }
    
Reply
  • Is this OK?

     /*
     init, Once executed
     */
    
     void pwm_init(void)  
     {
        /* 1-channel PWM, 200Hz, output on DK LED pins. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(5000L, BSP_LED_0);
        
        /* Switch the polarity of the second channel. */
        pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
        
        /* Initialize and enable PWM. */
        err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&PWM1);
      }
      
    
      
     /*
     pwm_change_frequency,Can be executed multiple times
     */
       void pwm_change_frequency(uint16_t freq)  
     {
     
      app_pwm_disenable(&PWM1);
      app_pwm_uninit(&PWM1);  
         /* 1-channel PWM, 200Hz, output on DK LED pins. */
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(freq, BSP_LED_0);
        
        /* Switch the polarity of the second channel. */
        pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
        
        /* Initialize and enable PWM. */
        err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
        APP_ERROR_CHECK(err_code);
        app_pwm_enable(&PWM1);
        
        }
    
Children
No Data
Related