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

I want to implement 2-channel PWM dead time.

Hello

I am using sdk v17.0 and pwm_library example.

I'd like to add a 'dead time' of 10us between the two PWM channels.

Can you give me a good example or method for this?

(Attempt to create dead time using nrf_delay failed.)

#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "app_error.h"
#include "bsp.h"
#include "nrf_delay.h"
#include "app_pwm.h"

APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.

static volatile bool ready_flag;            // A flag indicating PWM status.

void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
{
    ready_flag = true;
}

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(20, 23, 24);

    /* 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);

    while (true)
    {
          ready_flag = false;
          //while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY);
          APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 0, 50)); //1-channel
          nrf_delay_us(10); //dead time
          /* ... or wait for callback. */
          //while (!ready_flag);
          APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, 40)); //2-channel
    }

}

Thank you!

Parents Reply
  • Hi 

    What would the waveform look like if you have a PWM frequency of 50kHz and a dead time of 10us? 
    At 50kHz a single PWM cycle is only 20us long.

    The app_pwm driver will not really work for this use case, and unfortunately the PWM hardware modules don't have proper support for this use case either. 

    It might be that the best solution here is to do a more custom PWM implementation using a TIMER, some PPI channels and the GPIOTE module, which allows you to control pins accurately by configuring compare events at different points in time using the TIMER, and then connecting these events to pin operations through the PPI and GPIOTE. 

    This concept is explored in more detail in this hands on example. 

    Best regards
    Torbjørn

Children
Related