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

I'd like to know how to initialize multiple frequencies.

Hello

I would like to use three PWM to output multiple sounds with one buzzer.

First of all, I am testing using two PWM.  There is no problem using only one PWM, but there seems to be a problem with the initialization when using two.

APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1  (name, NRF_DRV_TIMER_INSTANCE(num)).
APP_PWM_INSTANCE(PWM2,3);                   // Create the instance "PWM2" using TIMER2.

void pwm_play_buzzer_duration(uint32_t duration)
{
    uint8_t i;
    uint32_t duty = 50;

    NRF_LOG_INFO("pwm_play_buzzer_duration =>");    
    ready_flag = false;

    //Set the duty cycle - keep trying until PWM is ready... 
    while (app_pwm_channel_duty_set(&PWM1, 0, duty) == NRF_ERROR_BUSY);

    nrf_delay_ms(duration);
    app_pwm_disable(&PWM1);
}

void pwm_play_buzzer_duration_2(uint32_t duration) 
{
    uint8_t i;
    uint32_t duty = 50;

    NRF_LOG_INFO("pwm_play_buzzer_duration =>");    
    ready_flag = false;

    // Set the duty cycle - keep trying until PWM is ready... 
    while (app_pwm_channel_duty_set(&PWM2, 0, duty) == NRF_ERROR_BUSY);

    nrf_delay_ms(duration);
    app_pwm_disable(&PWM2);
}

void pwm_init(void) 
{
    ret_code_t err_code;

    /* 1-channel PWM, 200Hz, output on DK LED pins. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(1500, BUZZER_PIN); //original 5000L
    
    /* 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);


    app_pwm_config_t pwm2_cfg = APP_PWM_DEFAULT_CONFIG_1CH(5000, BUZZER_PIN); 
    
    pwm2_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    
    err_code = app_pwm_init(&PWM2,&pwm2_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM2);
}

How should I set it up when I use multiple PWMs?

Thank you in advance.
  • Hi,

    I do not immediately see any issues with your code. Have you enabled logging to see if any error checks are being hit? It could be for instance that timer instance 3 is not enabled in sdk_config.h, some resource conflict, or something else. In that case, I would expect you should get a sensible error code returned from app_pwm_init().

  • Hi, Einar Thorsrud.

    I tried debugging with the breakpoint on the pwm_init.
    NRF_BREAKPOINT_COND; in app_error_weak.c when the code initializing PWM2.

    Timer 3 is enabled.

    Can you tell me what the error is?

    Thank you

  • Hi,

    The error code 3735928559 = 0xDEADBEEF, which is typically used  by the assert_nrf_callback() function in SDK examples. This could indicate a SoftDevice assert. Do you use the SoftDevice? If so, you must make sure that you do not use any peripherals used by the SoftDevice in your application.

    Are you sure this error is returned from app_pwm_init? Can you show the full log output, including file name and line number (no problem removing your username like you did, but the rest would be interesting).

    If the above does not make sense, and you do not make any progress, can you upload your complete code, preferably modified to run on a DK? Please also specify SDK version.

Related