AnsweredAssumed Answered

sinus pwm wrong frequency

Question asked by ionut.fasola on Jun 16, 2016
Latest reply on Jun 16, 2016 by ionut.fasola
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;

Outcomes