cancel
Showing results for 
Search instead for 
Did you mean: 

Sin wave generation problem using TIM1 & PWM

ralph
Associate II
Posted on March 02, 2016 at 23:17

I am a student from University of Cairo Iam trying to  generate a 50 hz sinusoidal using my stm32f4 discovery board. I am running my timer in 10khz (switching freq = 10khz) and I am using a lookup table (generated from excel) in order to get new compare values for my pwm => lookup table of 200 points. The discovery board and my timer 1 are running on 168Mhz. My timer is in up-down count mode with period = 8400 - 1 . The new compare value is obtained on overflow and updated. But its effect takes place on next underflow because of shadowing . I am able to get 50hz but it is not a pure 50hz. It has other frequency components which makes it moving around as can be seen in attachment. I have an RC filter placed on channel 1 of timer 1 on PE9 to make the sin wave visible. 

What is the reason of not getting a pure 50hz although my sin table is pure 50hz? I attach my code for you to take a look please.

Thank you for your help!
7 REPLIES 7
Posted on March 03, 2016 at 01:03

Like I told the last guy, the Update interrupt doesn't need you to check the CNT value, just qualify that the Update is the source.

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/PWM%20and%20TIMER1%20problem%20in%20stm32f4&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentview...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ralph
Associate II
Posted on March 03, 2016 at 18:12

Hello Clive1 I removed the checking on the CNT but the problem didn't solve and now i am getting 100 hz fundamental sin wave instead of 50hz because i am updating the sin table on overflow and underflow (see attachement). this is why i was checking CNT to detect overflow and update the compare value only once per cycle. What can be done about this?

Thank you for your help.

________________

Attachments :

pic.png : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0nv&d=%2Fa%2F0X0000000bgc%2FKbPEGpLY7QcVMyJSz2kYsflKVEq9vjOmIrk5OayYmj4&asPdf=false

pic2.png : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0zG&d=%2Fa%2F0X0000000bgb%2F1CNSmzm8pqsm7uBb.K8NJsOYy61dRwEumsn6Ft2cUDs&asPdf=false
Posted on March 03, 2016 at 18:46

I see, it's in the centred mode, sure you need that for this?

I'd use the DIR bit of the TIMx->CR1 to determine the Update I was looking at, not the CNT value.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ralph
Associate II
Posted on March 03, 2016 at 19:01

I tried edge aligned mode once but i got similar results. In literature review they say that its better to use symmetric pwm.

I did what you told me and i am using DIR however problem is still happening.

Thank you for your help Clive1 ! I hope we can find a logic explanation to this because I don't know what is causing this. 
ralph
Associate II
Posted on March 03, 2016 at 20:12

has anyone experienced something like this? Is it a limitation in mcu or software bug that i have to fix ? Thanks you

Posted on March 03, 2016 at 22:29

Attached two example .HEX file, write with ST-LINK Utilities. Evaluate the signals here and report.

________________

Attachments :

PWM1_50HZ_TEST1.hex : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0zB&d=%2Fa%2F0X0000000bgY%2FS_HlNRNd_XVHqwJWGOlAmHedTUG_tFSyCY.JzSmHU20&asPdf=false

PWM1_50HZ_TEST2.hex : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0wJ&d=%2Fa%2F0X0000000bga%2FF93TFS6ECF4TkI4Eq6Koq0W9w_8vVmjeklwHIZlTmtM&asPdf=false
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ralph
Associate II
Posted on March 05, 2016 at 00:26

Hello Clive1. Thank you for your help. I tried both of hex files. The first has generated sin wave with amplitude aboute 1.65V and 50hz, but again it wasn't a pure 50hz, it had other frequency components. Comparing with the output seen from my code,  your code was generating extra components i think on lower frequency component because the whole  wave (50Hz + other) varied slower. For seconde hex I saw upper side of sin (positif cycle) but also the wave was not stable and increasing time axis on osxillocope showed that other components were also present (wavy signal).

I was trying different configurations for my timer to see what changes. The modulated sin wave is 0.5*sin(2*pi ... ). I tried increasing prescaler from 0 to 21-1 (makes 8 mhz timer 1) and period to 400-1 (10khz switching). I saw the  expected 50 hz signal but other frequency components were also coming in at frequencies lower than 50Hz. So i conclude that the additional frequency components were different than the one seen with the timer configuration I previously had in my code with no prescaler and period=8400-1 (the code posted in my 1st post). Why is this so?

Another very unusual thing that is happening is that for all timer configurations i mentioned before when i change modulating sin wave from 0.5*sin(2*pi ... ) to 1*sin(2*pi ... ) i am able to get a very clean 50hz with no extra components. How come is this possible? Is there something logical behind this? Thanks for your help.