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

app_pwm 100% duty

Hi,

this code works as expected (PWM goes to a pin with a LED connected, and the LED blinks):

while(1) {
    while(app_pwm_channel_duty_set(&PWM1, 0, 99) == NRF_ERROR_BUSY);
    waitMs(1000);
    while(app_pwm_channel_duty_set(&PWM1, 0, 0) == NRF_ERROR_BUSY);
    waitMs(1000);
}

If I change 99% duty cycle to 100% then the LED is on all the time, its brightness doesn't change when the duty is set to 0% in the second step. Is that some bug? I use latest SDK.

  • Hi

    100 won't be a valid input value in this function. If you set the duty cycle to 100 the application will wait for a timeout that never comes, and for some reason it won't be reset when setting another duty cycle (Yes, a bug).

    A possible workaround will be to replace the app_pwm_channel_duty_set function to this:

    ret_code_t app_pwm_channel_duty_set(app_pwm_t const * const p_instance,
                                      uint8_t channel, app_pwm_duty_t duty)
    {
        uint32_t ticks;
        if (duty >= 100)
        {
            ticks = ((uint32_t)app_pwm_cycle_ticks_get(p_instance) * (uint32_t)100) / 100UL;
            ticks -= 1;
        }
        else
        
        {
            ticks = ((uint32_t)app_pwm_cycle_ticks_get(p_instance) * (uint32_t)duty) / 100UL;
        }
        
        return app_pwm_channel_duty_ticks_set(p_instance, channel, ticks);
    } 
     
    

    If the duty cycle = 100 or more now, it will use the value of 100 and subtract 1 one tick, so that the timeout can be achieved. Thank you for notifying us!

    Best regards,

    Simon

  • Thanks Simon for your quick reply. Just a note - I migrate an app from old SDK (nrf51) and there 100% duty worked. I will send max 99% duty to app_pwm till you fix this bug.

Related