How to set PWM cycle to 10 seconds ?

Hi,

I use SDK 10 pwm example (examples\peripheral\pwm) to modify the PWM cycle to 10s and duty cycle to 50%.

Code showed as below:

int main(void)
{
    ret_code_t err_code;
    
    /* 2-channel PWM, 200Hz, output on DK LED pins. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(10000000L, BSP_LED_0, BSP_LED_1);
    
    /* 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);
    
    //uint32_t value;
    while(true)
    {
        
        for (uint8_t i = 0; i < 40; ++i)
        {
            //value = (i < 20) ? (i * 5) : (100 - (i - 20) * 5);
            
            ready_flag = false;
            
            /* Set the duty cycle - keep trying until PWM is ready... */
            while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY);
            
            /* ... or wait for callback. */
            while(!ready_flag);
            APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, 50));
            nrf_delay_ms(25);
        }
    }
    
}

But the PWM cycle seem only around 3 seconds. Can it possible change PWM cycle time to 10s ? If can, how to do so?

Thanks.

  • Hi,

    The 16 bit timer on the nRF51 will wrap around after a bit more than two seconds, using the lowest possible frequency (after the prescaler). So if you need a longer cycle you need to either modify the PWM library to handle wrapping or consider using the RTC (for instance via the app_timer) instead. Luckily, you don't have to implement the RTC approach yourself, as we provide a low power PWM library for this reason. Unless you need a very accurate signal despite the long duration, then the RTC bases approach you get in the ow power PWM library is probably just what you need.

    By the way, is there a particular reason you are using SDK 10? (The latest SDK with support for nRF51 is 12.3).

Related