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

Is that possible to generate three PWM with softdevice?

Hi,

In my application, I need 3 PWM to drive LED. Each PWM will have the same frequency but not the same duty cycle. Is that possible to do that? I know that Timer0 is used by softdevice so I can only use 2 timer.

Furthermore, I will use current time sevice + 1 RTC to use as clock (need to light up the LED to some given time slot).

Thanks in advance for your help.

  • Hello julienh,

    Not sure what kinds of device or SDK you are using now. According to your description, I assumed that you are using the nRF51. The softdevice will occupy Timer0, but the user still can use Timer1 and Timer2 to create the PWM. And each of the timers can create two channels fo the PWM signal. So It's possible to deal the three channels PWM and using the softdevice at the same time.

    Short sample code is shown in the below,

    APP_PWM_INSTANCE(PWM1, 1);
    APP_PWM_INSTANCE(PWM2, 2);
    
    void pwm_callback(uint32_t pwm_id)
    {
        //do what you want in the callback function 
    }
    
    void pwm_init()
    {
        ret_code_t err_code;
        app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000, BSP_LED_0, BSP_LED_1);
        app_pwm_config_t pwm2_cfg = APP_PWM_DEFAULT_CONFIG_1CH(5000, BSP_LED_2);
        err_code = app_pwm_init(&PWM1, &pwm1_cfg, pwm_callback);
        APP_ERROR_CHECK(err_code);
        err_code = app_pwm_init(&PWM2, &pwm2_cfg, pwm_callback);
        APP_ERROR_CHECK(err_code);
        
        app_pwm_enable(&PWM1);
        app_pwm_enable(&PWM2);
    }
    

    After initializing the PWM module, you can configure the PWM channels as you want,

    void set_led(uint8_t red, uint8_t green, uint8_t blue)
    {
        while(app_pwm_channel_duty_set(&PWM1, 0, red) == NRF_ERROR_BUSY);
        while(app_pwm_channel_duty_set(&PWM1, 1, green) == NRF_ERROR_BUSY);
        while(app_pwm_channel_duty_set(&PWM2, 0, blue) == NRF_ERROR_BUSY);
    }
    

    For more detail information about the PWM module, please check here.

  • Hi Rick,

    Thank you for the anserwer. I didn't undestood that each channel of a PWM instance could be configured independently.

    Thanks

Related