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

PWM

Dear nordic,

I need a help to create a PWM. I want to create pwm of 10 ms time period and pulse width of 1 micro second. i created PWM of 10 ms time period but how i create pulse width to 1 micro second. I think it has option to change duty cycle of pulse but till i don't know how i create 1 micro second pulse width. i tried in example PWM library i sucess to create 10 ms(time period) pwm but i can't set PWM width with 1micro second i need  some help to achive this.

  • Hi,

    The period is set by changing the frequency while the pulse width is set by adjusting the duty cyle. Here is an example from the documentation of the PWM library that shows how to setup a PWM signal:

    APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.
    void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
    {
        
    }
    /*
        ...
     */
    ret_code_t err_code;
    /* 2-channel PWM, 200 Hz, output on DK LED pins. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, 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);
            
            /* Se the duty cycle - keep trying until PWM is ready. */
            while (app_pwm_channel_duty_set(&PWM1, 0, value) == NRF_ERROR_BUSY);
            while (app_pwm_channel_duty_set(&PWM1, 1, value) == NRF_ERROR_BUSY);
            nrf_delay_ms(25);
        }
    }

    best regards

    Jared 

Related