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

PWM Wrong frequency and duty cycle

I set up my PWM Driver like this:

    nrf_drv_pwm_config_t const nrf_drv_pwm_config =
{
    .output_pins =
    {
            BSP_LED_2 | NRF_DRV_PWM_PIN_INVERTED,   // channel 0
            NRF_DRV_PWM_PIN_NOT_USED,             	// channel 1
			BSP_LED_3 | NRF_DRV_PWM_PIN_INVERTED,   // channel 2
            NRF_DRV_PWM_PIN_NOT_USED              	// channel 3
    },
    .irq_priority = APP_IRQ_PRIORITY_MID,
    .base_clock   = NRF_PWM_CLK_16MHz,       // Need 250 clock to get 41kHz PWM that could have 50% duty cycle
    .count_mode   = NRF_PWM_MODE_UP,
    .top_value    = PWM_MAX_STEPS,
    .load_mode    = NRF_PWM_LOAD_GROUPED,
    .step_mode    = NRF_PWM_STEP_AUTO
};

And start the complex sequence like this:

static  nrf_pwm_values_grouped_t  final_pwm[2];

final_pwm[0].group_0 = PWM_MAX_STEPS;
final_pwm[0].group_1 = PWM_MAX_STEPS/2;

final_pwm[1].group_0 = PWM_MAX_STEPS;
final_pwm[1].group_1 = PWM_MAX_STEPS/2;

nrf_pwm_sequence_t  const nrf_pwm_sequence =
{
	.values.p_grouped = final_pwm,
	.length           = 4,
	.repeats          = 0,
	.end_delay        = 0
};

nrf_drv_pwm_simple_playback(&m_pwm, &nrf_pwm_sequence, 2, NRF_DRV_PWM_FLAG_LOOP|NRF_DRV_PWM_FLAG_SIGNAL_END_SEQ0);

But what happens is that the LED4 (pwm channel 2) gets full brightness and LED3 (pwm channel 0) gets no brightness. Then I realize that if I go from 0 to PWM_MAX_STEPS (62500), it goes up to maximum then back to zero again. Moreover, in the oscilloscope I checked that the frequency is actually close to 512Hz. It should be 256Hz, right?

F(pwm) = F(clk)/COUNTERTOP

In this case my F(clk) is 16MHz and COUNTERTOP should be PWM_MAX_STEPS which is 62500. Therefore F(pwm) should be 256Hz. In the o'scope it seems to be 512 as the period is close to 2ms. What am I missing here?

Parents Reply Children
Related