cancel
Showing results for 
Search instead for 
Did you mean: 

PWM and TIMER1 problem in stm32f4

vss01
Associate II
Posted on February 22, 2016 at 00:13

Hello,

I need to generate sin wave using STM32 uC. 

Now here comes the problem. 

I am able to obtain the required sin wave with the required frequency and the required amplitude however there are other (more than one) frequency components.

After spending some time, I think I have pinpointed my problem down to having something related to 'jitter' in the PWM or phase noise because of a mismatched PWM frequency and sample rate of the lookup table.

What do you think? Any help would be appreciated!
11 REPLIES 11
Posted on February 22, 2016 at 01:47

Ultimately the machine does what you tell it too, unfortunately I can't see from this what you're physically instructing the machine to do.

Instead of programming the timer, write all the data you generate to a file, and the plot and review that.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
vss01
Associate II
Posted on February 23, 2016 at 22:55

I have a sinusoidal lookup table that contains only a 50Hz that I'm using, however I don't know how its not only 50Hz on the capacitor of the lowpass filter. If you need details of the setup see my previous post. 

Thanks!

Posted on February 24, 2016 at 02:17

Ok, the key detail missing is the code, something I can compile and follow the logic, I'm not looking to recreate or resolve this for myself.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on February 24, 2016 at 21:47

Your update interrupt looks really dodgy

//update sin sample on overflow only if(htim8.Instance->CNT > 3000){ //... And 4200 is half of 8400 Needs to be

if (__HAL_TIM_GET_IT_SOURCE(&htim8, TIM_IT_UPDATE))
{
__HAL_TIM_CLEAR_IT(&htim8, TIM_IT_UPDATE);
if (sin_index >= 200)
sin_index = 0;
__HAL_TIM_SetCompare(&htim8, TIM_CHANNEL_1, 4200 + sin_wave_50hz[sin_index++]); //4200 = PERIOD/2 = 8400/2
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
vss01
Associate II
Posted on February 24, 2016 at 22:14

The interrupt is entered on an underflow or an overflow. I want to update my sample only on overflows or else i will be updating it twice per 100E-6. That's why I have if(htim8.Instance->CNT > 3000) to make sure i update it only once, and that is on an overflow.

Posted on February 25, 2016 at 00:45

I'm not sure how it is going to underflow, the update occurs once per period, ie at 10 KHz

Realistically this could be done with DMA and be completely autonomous.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on February 25, 2016 at 02:15

Other thoughts, the Period should be N-1, the TIM8 will run at 168 MHz

Attached is a .HEX that drives PC6 (TIM8_CH1) and PA7 (TIM8_CH1N), this should generate a 50 Hz sine wave via a 10 KHz PWM. Please see if this generate the wave form you expect.

________________

Attachments :

PWM8_50HZ_REL1.hex : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I12Q&d=%2Fa%2F0X0000000bhN%2FAlv3B8cCDLf9hMbKFwEV.s6SQFd0RrtTqBJiz4KPsl8&asPdf=false
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
vss01
Associate II
Posted on February 25, 2016 at 07:13

Underflow can occur because I have my timer running in up-down mode counter.