Author Topic: Incorrect PWM frequency  (Read 2940 times)

Offline Raffaele

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Incorrect PWM frequency
« on: October 01, 2020, 05:46:19 AM »
Hi,

simple question, I'm trying to generate a PWM signals with 50% duty cycle, between 650kHz-750kHz on a KL03.

For example at 600kHz I use

Code: [Select]
    pwm_setup.pwm_mode = (PWM_SYS_CLK | PWM_PRESCALER_1);
    pwm_setup.pwm_frequency = PWM_TIMER_US_DELAY(TIMER_FREQUENCY_VALUE(650000), 1);
    pwm_setup.pwm_value = _PWM_PERCENT(50, pwm_setup.pwm_frequency);

PWM_SYS_CLK should be 8MHz. But I don't get the right value, my PWM is at 990kHz.
What am I doing wrong?

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Incorrect PWM frequency
« Reply #1 on: October 01, 2020, 12:56:28 PM »
Hi

The problem is the use of the
PWM_TIMER_US_DELAY() macro
since this is for passing us resolution.
That means that it will do 1us (1MHz), 2us (500kHz), 3us (333kHz) etc. but won't allow your frequency of 650kHz.

Use instead
pwm_setup.pwm_frequency = PWM_FREQUENCY(650000, 1);
which will give 666.66kHz (next best)

Regards

Mark


Offline Raffaele

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Incorrect PWM frequency
« Reply #2 on: October 01, 2020, 02:36:14 PM »
great, that worked

thank you