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 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

  • Can you try increasing the value of SLEEP_TIME_MS, 100 maybe, and commenting out the other content of the while loop?

  • Dear Øivind,

    I tried different values of SLEEP_TIME_MS and commenting out all other contents in the while loop as you suggested, but still with no luck.

    Please kindly advise and let me know if there are any other possible solutions I could try.

    Thanks and regards, Kevin

  • Try printing in the loop, to make sure that the printing is working, and try reducing the frequency of the pwm significantly, to make sure that there is no problem there.

    You might want to increase the sleep_time so you don't get 10+ prints per second.

  • Dear Øivind,

    As you may see in my post above, the nRF terminal actually displays other console output information.

    Could you please kindly explain how the PWM frequency may have an impact on the other peripherals? Is it because of the way how PWM is generated or other reasons?

    For my particular application, the PWM is used in a 50% duty cycle as a clock signal which requires to be at least 500 kHz, while a button-press action to start and stop is also needed.

    What is the potential risk if the PWM output frequency is high in this particular application?

    Thanks and regards, Kevin

Reply
  • Dear Øivind,

    As you may see in my post above, the nRF terminal actually displays other console output information.

    Could you please kindly explain how the PWM frequency may have an impact on the other peripherals? Is it because of the way how PWM is generated or other reasons?

    For my particular application, the PWM is used in a 50% duty cycle as a clock signal which requires to be at least 500 kHz, while a button-press action to start and stop is also needed.

    What is the potential risk if the PWM output frequency is high in this particular application?

    Thanks and regards, Kevin

Children
No Data
Related