PWM output frequency or prescaler change and configuration

Dear ,

I am using the nRF52833-DK development board with the NCS1.7.0 SDK.

I am trying to get a PWM waveform output at 50% duty cycle with 409.6kHz frequency based on the blinky_pwm and fade_led sample projects. However, it was unsuccessful.

Could you please kindly advise and help? How could I config/change the clock frequency for PWM peripheral? Currently, it seems to be 1MHz.

Thanks and regards, Kevin

Parents
  • Hi,

    In blinky_pwm, try changing the 2 pwm_pin_set_usec() functions to pwm_pin_set_nsec(), and set MAX_PERIOD_USEC to 2500 and MIN_PERIOD_USEC to 625.

    Does that produce a waveform closer to what you are after?

  • Dear Øivind,

    Thanks for your reply.

    I managed to make it work by switching it to pwm_pin_set_nsec(). However, to be able to get a better understanding, I still have a few questions.

    1. What's the purpose of setting MAX_PERIOD_USEC and MIN_PERIOD_USEC? Are they purely for the calibration? Is the calibration process compulsory for using PWM?

    2. When I tried different period values to see the waveform changes, it seems that it works normally even if I use a value that's not within the range of MAX_PERIOD_USEC and MIN_PERIOD_USEC. Is it normal? If so, then what's the purpose of the MAX_PERIOD_USEC and MIN_PERIOD_USEC?

    3. When supplying period = 2 and pulse = 1 for pwm_pin_set_usec() function, it generates a waveform with 500kHz frequency observed on the oscilloscope. If I am getting this right, it means that the PWM is working at 1MHz, i.e. 32MHz with a prescaler of 32. Can I change this prescaler value? 

    Thanks again for your kind assistance.

    Kind regards, Kevin

  • Dear Øivind,

    Thanks a lot for your info and they are very helpful.

    Meanwhile, I still have got one more issue.

    When I combined the blinky_pwm and button samples, I found that the button interrupt callback function cannot execute if pwm_pin_set_nsec() is called.

    After pwm_pin_set_nsec() is commented, the button callback function returns back to normal. (NRF TERMINAL displays the button_pressed message generated by the code below.

    void button_pressed(const struct device *dev, struct gpio_callback *cb,
    		    uint32_t pins)
    {
    	printk("Button pressed at %" PRIu32 "\n", k_cycle_get_32());
    }

    Does it have anything to do with the interrupt priority or similar configurations? How can I resolve this issue and make them all work properly at the same time?

    And what is the proper way to stop the PWM output? Do I just simply set the const struct device pointer variable *pwm to NULL?

    Thanks and kind regards, Kevin

  • Can you zip and send me the project, so I can take a look? You can attach files to your reply with Insert->Image/video/file.

    You can stop the PWM output by setting the duty cycle to 0.

  • Dear Øivind,

    Please see the zip file as attached. It was tested on nRF52833dk configured as nRF52833dk-nRF52820.

    In the main.c, if line108 - 113 are commented, you won't get the console printout string from the button_pressed callback. This problem disappears as long as these lines remain commented.

    Please kindly help.

    Thanks and kind regards, Kevin

    button.zip

  • The nRF52820 does not have a hardware PWM peripheral, only an implementation using the TIMER peripheral.

    It is disabled by default, try adding this file to your project and see if that works.

    nrf52833dk_nrf52820.overlay

  • Dear Øivind,

    Thanks a lot for your reply.

    As for now, I do not have any issue with the PWM output on the desired pin, which has been checked with an oscilloscope.

    However, once the PWM function output is enabled, the button interrupt callback stopped working. I've also attached the nRF terminal output for your reference.

    Below showing the console output when PWM is off.

    *** Booting Zephyr OS build v2.6.99-ncs1  ***
    Set up button at GPIO_0 pin 11
    Set up LED at GPIO_0 pin 13
    Press the button
    Button pressed at 306246
    Button pressed at 320278
    Button pressed at 332251

    Below showing the console output when PWM is on.

    *** Booting Zephyr OS build v2.6.99-ncs1  ***
    Set up button at GPIO_0 pin 11
    Set up LED at GPIO_0 pin 13
    [00:00:00.319,458] <dbg> pwm_nrf5_sw.pwm_nrf5_sw_pin_set: PWM 13, period 320, pulse 160
    Press the button

    As you may see, there is no button_pressed console print info even though I've pressed the button multiple times.

    Could you please kindly help?

    Thanks and regards, Kevin

Reply
  • Dear Øivind,

    Thanks a lot for your reply.

    As for now, I do not have any issue with the PWM output on the desired pin, which has been checked with an oscilloscope.

    However, once the PWM function output is enabled, the button interrupt callback stopped working. I've also attached the nRF terminal output for your reference.

    Below showing the console output when PWM is off.

    *** Booting Zephyr OS build v2.6.99-ncs1  ***
    Set up button at GPIO_0 pin 11
    Set up LED at GPIO_0 pin 13
    Press the button
    Button pressed at 306246
    Button pressed at 320278
    Button pressed at 332251

    Below showing the console output when PWM is on.

    *** Booting Zephyr OS build v2.6.99-ncs1  ***
    Set up button at GPIO_0 pin 11
    Set up LED at GPIO_0 pin 13
    [00:00:00.319,458] <dbg> pwm_nrf5_sw.pwm_nrf5_sw_pin_set: PWM 13, period 320, pulse 160
    Press the button

    As you may see, there is no button_pressed console print info even though I've pressed the button multiple times.

    Could you please kindly help?

    Thanks and regards, Kevin

Children
Related