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

nRF52840 how to change PWM duty cycle

Hi. I have a nRF52840 setup using the PWM driver, and it is outputting a 50Khz pulse 50% duty cycle.

How do I modify the sequence values so that I can modulate this 50Khz signal at 500Hz, i.e. on for 2mS pulsing 50Khz and then off for 2mS.

My sequence settings at the moment;

static nrf_pwm_values_individual_t seq_values[] = {0, 0, 0, 0};
static nrf_pwm_sequence_t const seq =
{
    .values.p_individual = seq_values,
    .length          = NRF_PWM_VALUES_LENGTH(seq_values),
    .repeats         = 0,
    .end_delay       = 0
};

And PWM init code

void pwm_init(uint16_t freq) //freq = 320 //16Mhz / 320 = 50KHz
{
    nrf_drv_pwm_config_t const config0 =
    {
        .output_pins =
        {
            COIL_PWM, // channel 0
            NRF_DRV_PWM_PIN_NOT_USED,             // channel 1
            NRF_DRV_PWM_PIN_NOT_USED,             // channel 2
            NRF_DRV_PWM_PIN_NOT_USED,             // channel 3
        },
        .irq_priority = APP_IRQ_PRIORITY_LOWEST,
        .base_clock   = NRF_PWM_CLK_16MHz,
        .count_mode   = NRF_PWM_MODE_UP,
        .top_value    = freq,
        .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
        .step_mode    = NRF_PWM_STEP_AUTO
    };
    // Init PWM without error handler
    APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));
}

Thanks

Related