cancel
Showing results for 
Search instead for 
Did you mean: 

sinus pwm wrong frequency

ionutF
Associate III
Posted on June 16, 2016 at 14:26

Hi.

I am using discovery board with STM32F100; I have made a code to generate a sinusoidal waveform through pwm via lookup table method.

I think I miss something in computing the output frequency of the sine wave.

Here is my setup: timer 1 clock : 12MHz ; no prescaler ; period= 481

Lookup table has 828 values ranging from 0 to 480.

After filtering the pwm (33nF/22k) I got a 49.99Hz sine wave

The question is why I got this frequency because the calculations say that for a 50Hz sine  i should have about 500 values ranging from 0-480. The 828 value i came accros after trial and error with a scope.

Timer period (time it takes to count from n to n+1) = 1/12000000=83.33 nsec

One timer count period (time it takes to count form 0 to 480)=83.33nsec*480= 39.99usec (which gives a pwm frequency  of 25kHz)

500*39.99usec=19.99 msec which gives a  sine frequency of ~50Hz

On scope with 500 values all I got is 12 msec (83.33 Hz) .

Where this discrepancy came from? 

The timer clock is indeed 12MHz because on the scope I can see those 40 usec (83.33nsec*480) so the timer clock has to be 12MHz

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseStructure.TIM_Period = 481-1;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
2 REPLIES 2
Posted on June 16, 2016 at 16:50

Wow! That's an awfully awkward set of numbers you've come up with.

So you're modulating a 24.9280249 KHz signal, with how many 50 Hz cycles in 828 table entries??

Wouldn't you want a whole, integer, number of cycles?

Set one of the timer channels on toggle mode, and scope *that* pin. Or scope the internal clock via PA8 (MCO) and provide the AHB/APB clock dividers.

Your numbers are more suggestive of a 20 MHz clock.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ionutF
Associate III
Posted on June 16, 2016 at 17:34

I think I found the issue or at least one issue : my library has defined USE_FULL_ASSERT

I think all those assert statements affect execution time or I have some illegal/undefined peripheral initializations in my code...I  don't know but suddenly I got 50.00 Hz on my scope, the 500 value is the right one as I supposed and computed.

Now I have to dig what exactly ''use full assert'' does in my code, because I have put a breakpoint in assert_failed() and let the cod run but I never reached the breakpoint so