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

how to make PWM pulse in both channels

Hi

i wrote the program which is making pulse one time on channel 0 and then followed by channel 1 and then again channel 0 and then 1 going on , my aim is to create pulses on both channel at the same time i tested by conencting buzzer on pin 6 and pin 7 so can anyone help me how to create that any help will be appreicated

Thank you

main.c

  • The pulses will not be totally synchronous if you use the driver. The driver can only set one channel at the time. You could use app_pwm_disable, but also here the output is set sequentially, so you will have an interval in time where one pin is high and the other is low. bottom line: You cannot do this with the pwm driver.

  • /* 2-channel PWM, 200Hz, output on DK LED pins. */

    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(5000L, 6, 7);
    
    /* Switch the polarity of the second channel. */
    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
    pwm1_cfg.pin_polarity[0] = 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, value) == NRF_ERROR_BUSY);
            
            /* ... or wait for callback. */
            while(!ready_flag);
            APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, value));
            nrf_delay_ms(25);
        }
    }
    

    }

  • i am using this example from the SDK but this making pulses synchroncially in both pins for infinite time by adding this line

    pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;

    then can you tell me how this working ?and also can you tell me how can i make condition for just five pulses (example just five times on and off uniformally) what condition do i have to set

  • It will not be totally synchronous. The first pwm channel will be updated with the duty set function, then the code will wait for the update to take effect, either with waiting for pwm ready callback or wait for the duty set function to not return NRF_ERROR_BUSY. Then the other channel may be updated. Because of this, there will be an interval of 1 or 2 periods where the PWM channels have different duty cycle.

Related