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

How to change PWM frequency

Hi,

[ Environment ]

- Application: Lifesaver
- Chip Series: nRF52832
- SDK version: SDK14.2.0
- Compiler: Keil V5

We have to keep changing the pwm frequency.

Reinit will cause a delay.

Can I change the PWM frequency without reinitializing the PWM?

Thanks.

Best regards,

Yun-joo

  • Hi,

    I have a few follow up questions to help me understand the challenge:

    • Are you sure you mean changing the PWM frequency and not the PWM duty cycle? The duty cycle is the more common property to change while the PWM is running.
    • Are you using the hardware PWM peripheral? An alternative may be to use a setup using a Timer or RTC with PPI or interrupts to drive a lower frequency PWM signal
    • What sort of device is this PWM signal intended to drive?

    Best regards,
    Rune Holmgren

  • Hi, Rune Holmgren

    Thanks for your reply.

    • Yes, we control the PWM frequency, not the duty.
    • Yes, we are using the PWM driver.
    • The PWM signal is used for melody buzzer and wireless charging.  In both cases, frequency control is required. So we'd like to know how to freely change the frequency of pwm using pwm driver.

    Thanks.

    Best regards,

    Yun-joo

  • Hi,

    The PWM hardware is a high-frequency peripheral which can accurately drive a PWM signal even at high frequencies. It's intended for use-cases where the duty cycle is changing, so you have to uninitialize and reinitialize the hardware to change the frequency. It's worth noting that the uninitialization and reinitialization of the peripheral is quite fast, so for many applications this is not a problem. For the purposes of playing sound from a piezo the delay from initializing the reinitializing, the PWM module will not matter. The delay is to short to be perceived by a human ear. For your wireless charging application I do not know the specification, so you will have to check this out yourself.

    An alternative to using the PWM module is using a timer and GPIO with PPI channels performing toggle operations at some frequency. The timer can run and after some period it will send an event directly to the GPIO to toggle a pin through a task. In this question you will find an answer by user Steven Wik where he explains how to set this up. The timer is more flexible than the PWM module, so it is often a good alternative. My manipulating  NRF_TIMER2->CC[1] runtime you can get different frequencies without the delay you get using the PWM peripheral.

    Best regards,
    Rune Holmgren

  • Hello, just wanted to add to this post that a sequence of nrf_drv_pwm_uninit() -> nrf_drv_pwm_init() -> nrf_drv_pwm_simple_playback() takes 11.6 us on a target NRF52832 running at 32 MHz, so it shouldn't affect the hearing of the human ear.

Related