Using nrf52840 DK, a PWM signal is unstable

Hello,

I perform a test using a nrf52840 DK board in order to output a PWM at 50 Hz on a GPIO.

Here is the code:


void pwm_init(void)
{
nrf_drv_pwm_config_t const config0 =
{
.output_pins =
{
NRF_DRV_PWM_PIN_NOT_USED,
2, // P0.2
NRF_DRV_PWM_PIN_NOT_USED,
NRF_DRV_PWM_PIN_NOT_USED
},
.irq_priority = APP_IRQ_PRIORITY_HIGHEST,
.base_clock = NRF_PWM_CLK_4MHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = 65535, // max possible
.load_mode = NRF_PWM_LOAD_INDIVIDUAL,
.step_mode = NRF_PWM_STEP_AUTO
};
APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));seq_values.channel_0 = 0;
seq_values.channel_1 = 32767; // 50% duty cycle
seq_values.channel_2 = 0;
seq_values.channel_3 = 0;
nrf_drv_pwm_simple_playback(&m_pwm0, &seq, 1, NRF_DRV_PWM_FLAG_LOOP);
}

Using this the output signal is unstable, it change for 49.5 to 49.6 Hz.

What could be the cause of this?

Thanks

Fabrice

  • Hello Fabrice,

    Refer to the Product Specification, the PWM peripheral uses 16MHz peripheral clock, which is sourced from the HFCLK.

    The HFCLK also has two sources, internal RC, HFINT, and external crystal, HFXO. HFINT by default has an accuracy of only 1.5%, which can with temperature and voltage. Therefore, your observation that the configured 50MHz gives a PWM signal with frequency varying from 49.6 to 49.5MHz is within expectation, assuming HFINT.

    Could you please request the HFXO and see if the accuracy improves? You can request it with nrf_drv_clock_hfclk_request() and release with nrf_drv_clock_hfclk_release(). Please try to call those functions when there is no radio activity, like during a SoftDevice timeslot, if possible.

    Hieu

Related