Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help:Mains AC synced PWM sine output?

Status
Not open for further replies.

abicash

Member
Hello

Posting something which was directed to Microcontroller forum.
Please help.

My OP
Hi
I require help from learned forum members regarding a general concept.
I have a sine weighted PWM generator from a microcontroller.
Simple question : How to determine synchronise the o/p frequency of sinewave with Mains frequency?
PWM freq = 8KHz
Microcontroller has 2 timers of which one is generating the 4 PWM o/ps
Also i have a Comparator module which can trigger this Timer1
or else i can use Timer2 in input capture mode to trigger Timer1 for PWM.

To which alec_t replied
You'd need to:
1) use a transformer to get a mains-isolated low voltage representation of the mains waveform,
2) detect zero-crossings or peaks of the waveform as reference points,
3) get the micro to spit out the sine-weighted values at the appropriate times relative to the reference points.

I replied
I have known 1) and 2) of your post.
I know 3) as per your explanation but just bear me through with my explanation, and point out if i am wrong..
Normally i start the Timer1 for sine-weighted values from a look up table (LUT) after a key press.In the overflow interrupts , i update the counter with updated value and then do housekeeping stuff.Next value is updated from incremented LUT index on next overflow, and so on till LUT is reinit at 0.
This process occurs or rather progresses naturally.
Now how do i integrate this with an input capture event (triggered by ZCD).
Would you suggest , reinitialize Timer1 at every edge of the ZCD, or should i reset the index counter of LUT to zero?
Please provide an insight


after which i was guided here.

Apology if the post has been too long to read, but i am sure learned forum members will definitely help :)
 
Convert mains into a square wave using a circuit on the o/p of a transformer, then either in software or using interrupts clear a counter which you are using to generate the pwm when the input from this circuit changes state, then your wave will allways be in sync with the mains.
There will be a little phase shift on a transformer which isnt fully loaded, but probably not enough to be an issue.
 
Convert mains into a square wave using a circuit on the o/p of a transformer, then either in software or using interrupts clear a counter which you are using to generate the pwm when the input from this circuit changes state, then your wave will allways be in sync with the mains.
There will be a little phase shift on a transformer which isnt fully loaded, but probably not enough to be an issue.
I use a LUT (Look Up Table) to store 32 values of a full sinewave (positive and negative swings)
I now call each of these values 5 times to generate a 50Hz wave (with 8KHz PWM)
The Math is ->
PWM period = 125 usec
32 values table of each 125 usec but weighted in duty cycle as per a sinewave
So Sine period = 125 usec x 32 = 4 msec
To get a 20 msec period i call each value 5 times so
Sine period = 4ms x 5 = 20msec

This sine period is defined by a Variable named Frequency
In the Overflow interrupt of the PWM timer , i calculate the index of the LUT based on this.
From this i understand (?) that i need to define the value of Frequency after every input mains capture event.
So do i need to measure the input mains frequency as well? apart from reinitializing the PWM timer every time in this capture interrupt event?

Please comment
 
Nope what you have makes sense.
Before you call the very first position in the lookup table just wait till the i/p from the mains zero crossing detector changes, so long as your code produces ball park 50hz it should always be in sync, there may be minor distortion at that point but only in the order of a uS or so if your timing is good.
 
Hello again

Replying after a long time since was tied up with other things recently.

I am revisiting this chapter now :)

I am currently able to synchronise the o/p of the inverter with the mains but with few issues.I will explain it as follows

1) I take a half diode bridge to rectify only positive Grid voltage
2) I feed this to a trigger circuit in my Microcontroller
3) At exact ZCD events, i reset my index of the generated sine wave to zero.

I found out that above approach syncs both frequencies fairly well. But i do see a negative side dancing at odd intervals
(changing phase and again correcting in the next event)

For this i took a Full diode bride rectification of the grid and ran the above procedure again.
Now i see that there is perfect sync but with the major flaw that sometimes both waves are 180° shifted. They remain in shift perpetually if either grid doesn't fail or inverter isn't restarted.

Can someone of you guys guide a bit as to how this can be done?
 
Thanks KISS

But know RB's legendary stuff for quite some time :)

I am almost there , but unable to get there completely...it seems...
 
Shouldn't you be using a PLL. The reference being the line and your created waveform the should be phase-shifted by the error.
 
Shouldn't you be using a PLL. The reference being the line and your created waveform the should be phase-shifted by the error.
:) That is what i am trying to create through the software.

You see, whenever the Line frequency triggers an input capture channel, i index the LUT (look up table) for the sine wave PWM at '0' or the first position, thereby ensuring (?) the generated wave in line with grid.
But somehow am running in troubles
 
I was thinking more of a single diode to trigger a synthesised full wave, at the end of which wait for another trigger pulse, then 180 degree switching shouldnt occur.

Watc out when using a transformer, there could be a 180 degree phase shift deping which way you connect the primary/secondary (but this will be constant and can be dealt with).
 
If you rectify the sine wave, you can no longer tell when it is negative and when it is positive.

You have two wires from the transformer. Zero Crossing is when they're the same. Simply connect them to + and - of the comparator (after appropriate conditioning). Comparator will go high for half of the wave and low for the other half.

What I don't understand is why, instead of using the sine wave that you already get from mains, you're trying to dissect it and then re-generate your own. It looks like a lot of work for nothing.
 
I was thinking more of a single diode to trigger a synthesised full wave, at the end of which wait for another trigger pulse, then 180 degree switching shouldnt occur.
Yes that is what i am doing currently.

If you rectify the sine wave, you can no longer tell when it is negative and when it is positive.
You have two wires from the transformer. Zero Crossing is when they're the same. Simply connect them to + and - of the comparator (after appropriate conditioning). Comparator will go high for half of the wave and low for the other half.
What I don't understand is why, instead of using the sine wave that you already get from mains, you're trying to dissect it and then re-generate your own. It looks like a lot of work for nothing.
Hello again NG , glad and happy to see you again, helping me out :)
Maybe i could not explain it clearly earlier.I am trying to make a sine inverter converting DC from the PV to AC.
I have a relay which connects mains to the o/p of the H-Bridge. (Attaching the pdf for that scheme).So irrespective of mains presence or absence , i should be able to run the H-Bridge.
When the drives are present on the H-Bridge and inverter is running, and Mains arrives, the generated should phase-freq lock to incoming mains and then should the relay be switched.Then the inverter/drives can be switched off.
The load in this case will not see any pulse missing.
When mains fails, the inverter will be switched on instantly (few CPU cycles).

Now i am able to sync the Half rectified input wave to the o/p on the generated wave.
For this i took the rectified signal in the internal comparator of my MCU and compared with an internal DAC (set at 100mV)
The Comparator triggers another input capture timer channel.In the ISR for this channel, i reset the PWM pulses' index to zero each time.

Now i see something odd happening at times (grab on the DSO)
Maybe it is happening due to noise on the mains rectified input or something else...not sure..
 

Attachments

  • Solar Inv Block.pdf
    32.3 KB · Views: 378
Last edited:
Hello again

Problem solved :D

That was happening due to some noise spikes on mains.
I configured the internal comparator for filtered input and everything runs nice
 
Hi Pommie

I will not sync to mains when it fails...that is obvious...

only when it is present, i will do that
 
I was thinking something similar, noise on the mains, a filter as you say gets rid of this.
Had the filter not sorted it then I would have been looking at the 'scope itself, possibly it was false triggering, I have chased faults for hours when its just been the way the 'scope is displaying.
Glad to see you sorted it.

I havent built a synced inverter but I do have a couple of electric clocks designed to run on the mains, they have beeen rewound low voltage, the 50hz 9v supply for them comes from a microcontroller which generates 50hz synced to the msf timecode transmitter which has an edge every second, sort of a similar phase lock your using.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top