This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to Generate PWM with variable Freq but with fix tolerance with PWM Generator Block ?

Other Parts Discussed in Thread: TM4C1290NCPDT

Hello Mates,

i have TM4c1290NCPDT TIVA ware uC which i want to use to generate the Square wave signal with Desired Freq but with Fine Accuracy i.e. i want to use it with 50 Khz and Also with 5Khz too but with accruacy of +/- 0.1Hz.

I have use the PWM Generator Block for the same.

But it will not work properly. 

Please help me for the same.and if any example code .

Below is my Code but i didn't got the exact 4KHz. If i want exact 4KHz how could i be able to achieve?

	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
	ROM_PWMClockSet(PWM0_BASE, PWM_SYSCLK_DIV_64);
	ROM_GPIOPinConfigure(GPIO_PG0_M0PWM4);
	ROM_GPIOPinTypePWM(GPIO_PORTG_BASE, GPIO_PIN_0);

	//
	// Configure the PWM generator for count down mode with immediate updates
	// to the parameters.
	//
	ROM_PWMGenConfigure(PWM0_BASE, PWM_GEN_2,
	PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);

	//
	// Set the period. For a 4 KHz frequency, the period = 1/4000, or 0.25
	// milliseconds. For a (60MHz/64) clock, this translates to 937.5 clock ticks.
	// Use this value to set the period.
	//
	ROM_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_2, (937.5/4));

	//
	// Set the pulse width of PWM0 for a 50% duty cycle.
	//
	ROM_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_4, (937.5/8));

	//
	// Start the timers in generator 0.
	//
	ROM_PWMGenEnable(PWM0_BASE, PWM_GEN_2);

	//
	// Disable the outputs.
	//
	ROM_PWMOutputState(PWM0_BASE, PWM_OUT_4_BIT, false);

	//
	// Enable the outputs.
	//
	ROM_PWMOutputState(PWM0_BASE, PWM_OUT_4_BIT, true);

  • smit majithia said:
     I want to use it with 50 Khz and Also with 5Khz too but with accruacy of +/- 0.1Hz.

    and

    smit majithia said:
    But it will not work properly. 

    Should you not consult the MCU's frequency control specs to see if (especially if) the MCU has such accuracy?   (especially @ higher frequencies!)  Doesn't the crystal vendor's spec and your implementation (also) intrude upon such, "Quest for accuracy."   Is a simple MCU "famed" for such accuracy?   What is the "norm?"

    How have you determined that such tight tolerance & accuracy is REALLY required?

    Your 2nd quote (above) is (essentially) "empty of meaningful content!"   More properly - should you not list what frequency you achieved - rather than just stating, (the always helpful) "Does NOT work!"

    And - choosing division factors which are non-integral CANNOT be good if accuracy is your goal - can it?

  • Hello Smit,

    In regard to the desired accuracy, this seems very tight and I can assure you that we will not be able to meet it under normal uses. Even slight changes in temperature and or voltage can cause small changes in the frequency due to gate delays and transistor performance within not only the clocking circuit within the MCU but also within the logic of the PWM module. Also, this is dependent on the tolerance/stability of the crystal you are using which, in most cases, would use up a significant portion of the variation tolerance you are requesting. Finally, one should also consider jitter within the PLL and clock tree in general.

    For your initial attempt at generating a PWM, what was the resulting PWM output (as CB had asked)? Given this we may be able to assess if this is the best you can do or if there is room for optimization and improvement even if not enough to achieve the tolerance you are looking for.
  • @Chuck,

    So often each/every "poster request" is obliged - even when (and especially when) - they make (little) sense. And (never) do posters describe, "Why they believe they require such performance." Good that you "braved PC" by (also) identifying the "unreasonableness" of poster's quest.

    Minus (any) attempt at post, "salesmanship" (or at inducing others to contribute) such unique (and demanding) poster quests will sit idle as they prove too "specialized" for most here... (especially so when "unsold!")
  • Hey Chuck,

    ya i understand the effects of tempreture with the gate delays would occur but i want it as far as the coding is concern,

    Can you please let me know how to put it into action from firmware side,

    my problem is something like as below,

    Suppose i want to generate 4kHz PWM signal so my approach from code is to load it in the ROM_PWMGenPeriodSet as parameter ui32Period 

    so i took steps like i have 60MHz system clock so and i have used 64 PWM divisor now,

    60000000/64 = 937500 now for the 4kHz signal i would do : 937500/4000 = 234.375,

    so my ui32Period would be 234.375 but as the parameter ui32Period is unsigned int so 234.375 would be truncated to 234 only now, this lead to the 4.1 KHz signal instead of 4KHz Please assist me how to generate such signal so that the period would not get truncated due to the unsigned int and i would get the 4KHz perfect signal.

  • Hey cb1,

    ya i understand the specs and but i want it as far as the coding is concern,

    Can you please let me know how to put it into action from firmware side,

    my problem is something like as below,

    Suppose i want to generate 4kHz PWM signal so my approach from code is to load it in the ROM_PWMGenPeriodSet as parameter ui32Period

    so i took steps like i have 60MHz system clock so and i have used 64 PWM divisor now,

    60000000/64 = 937500 now for the 4kHz signal i would do : 937500/4000 = 234.375,

    so my ui32Period would be 234.375 but as the parameter ui32Period is unsigned int so 234.375 would be truncated to 234 only now, this lead to the 4.1 KHz signal instead of 4KHz Please assist me how to generate such signal so that the period would not get truncated due to the unsigned int and i would get the 4KHz perfect signal.

    I hope you understand my concern.
  • So often each/every "poster request" is obliged - even when (and especially when) - they make (little) sense. And (never) do posters describe, "Why they believe they require such performance." Good that you "braved PC" by (also) identifying the "unreasonableness" of poster's quest.

    Minus (any) attempt at post, "salesmanship" (or at inducing others to contribute) such unique (and demanding) poster quests will sit idle as they prove too "specialized" for most here... (especially so when "unsold!")

    You forgot the prevalent unwillingness of many posters to do any research on their own, and to try understanding the basics of the MCU/peripherals/programming language they are using. Instead they insist on unreasonable expectations, and prompt delivery of finished solutions. Add an unclear formulation of the problem and a demanding language to the mix.

    I use to avoid those threads ...

  • I may (not) have forgotten - instead chose to applaud the vendor for pointing out the unreasonableness of poster's request. (that's not done often enough (as I suspect you/I to agree.))

    Note that this almost certainly is a school assignment as the near identical request arrived (today) from another. In that case - both posters and their instructor "share" blame for such, "unreasonable expectations."

    PC too often (IMHO) forces vendor reps to "agree" w/every request - even when - and especially when - such posts are unreasonable... The "applaud" of the rep's courage for "stating reality" was central in my reply...
  • I just had been baffled by the complaint that "one can't use floats for setting the PWM period", which reveals a total ignorance of the operating principle of the TM4C (and other) PWM modules. IMHO, such does not go together with "engineer" -  PC or not ...

  • smit majithia said:
    ya i understand the specs

    Apparently not.

    smit majithia said:
    so my ui32Period would be 234.375 but as the parameter ui32Period is unsigned int so 234.375 would be truncated to 234 only now, this lead to the 4.1 KHz signal instead of 4KHz

    smit majithia said:
    I hope you understand my concern.

    Quite frankly, no. Your concern is not understandable.

    Check the IC documentation. You will quickly discover that the hardware is incapable of doing what you suggest.

    smit majithia said:
    i would get the 4KHz perfect signal.

    Perfect is not possible (see the earlier notes from Chuck, cb1 and f.m.) but you can do better. Consider why you are performing two divisions to get your final frequency. Are both necessary?

    Robert

    And as cb1 said, you are probably best served by asking why do you need such high accuracy to begin with (2ppm, really!?)

  • Smit,

    So, if I understand correctly, you are asking how to get to a point where your calculations result in an exact frequency of 4KHz. IF this is really the question, the simple answer is to choose an operating frequency that divides evenly (power of 2) resulting in integer divisors in the PWM module/formulas. If the 60MHz is not flexible, then 4KHz output is not possible given that the PWMDIV has to be a power of 2 divider. You have to understand the basic principle that all clocks within the uC are derived from the main system clock or from the OSCIN, As a result all desired output frequencies must somehow divide evenly into the primary clock source so that integer values can be used to load into the registers.

    In the end, you are interested more in precision than in accuracy/tolerance as Robert has mentioned in another post. To this end the precision capability is limited to the smallest divisor count (main PWM counter). If you want an exact PWM ideal frequency via program/mathematical calculation, you will would have to have a variable system clock to accommodate the infinite range of possibilities up to and including 50KHz which isn't feasible.
  • I think i would an precise answer to my question is due to the digital dividers i would not get the exact frequency PWM output as i want to get for that i would use the specific IC for the same.
  • smit majithia said:
    precise answer to my question is due to the digital dividers i would not get the exact frequency

    My friend - self awarded "Suggested Answer" (always highly questionable) falls flat (i.e. untrue) here, too!

    It is NOT the "digital dividers" which impact precision!   It is your earlier use of  "non-integral" dividers.   

    And - you have confused precision w/accuracy.   Your precision is "independent from accuracy" your "precision" will NOT be harmed by your use of non-integral dividers.   (all such results will (still) "cluster" about some central point.)   But the accuracy of your results will be off.

    The DDS IC which poster Petrei proposed (pop those "P"s) also employs your (dreaded) digital dividers.   Thus your "Suggested Answer" appears "circular" - does it not?

    You describe your quest as, "my question" - yet another posed the exact same question!    Should not you use, "the class' question" instead of "your" question?   That's important as you list "precision" as a key goal...

  • Smit,

    generally speaking, your understanding is correct irrespective of what you call it (precision, accuracy, granularity of control, etc). The combination of the limitation of the PWMDIV as a power of 2 divider, the chosen system frequency, and internal clock tree implementation will prevent you from achieving your targeted frequency within the range you are trying to achieve for every integer frequency up to 50KHz.