Author Topic: 2 PWM outputs with half a cycle phase shift on AVR  (Read 3844 times)

0 Members and 1 Guest are viewing this topic.

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
2 PWM outputs with half a cycle phase shift on AVR
« on: March 01, 2016, 09:17:10 pm »
I have a need to generate 2 PWM signals on an AVR but they need to be out of phase by half a cycle. Is this actually possible? The only way I can think of doing it is somewhat manually with interrupts and perhaps 2 different counters that are synchronised.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9946
  • Country: nz
Re: 2 PWM outputs with half a cycle phase shift on AVR
« Reply #1 on: March 01, 2016, 09:26:22 pm »
As long as you use a timer mode that doesn't mess with the top/bottom of the counting the two timers will stay in whatever sync they are started in.

So it's only a one-off issue getting the 2nd timer to start at the right time.

Perhaps set the timer to first generate a compare interrupt at 1/2 the time and use that to start the 2nd one and then re-purpose the compare to do pwm from then on as you need.
« Last Edit: March 01, 2016, 09:28:27 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline bktemp

  • Super Contributor
  • ***
  • Posts: 1616
  • Country: de
Re: 2 PWM outputs with half a cycle phase shift on AVR
« Reply #2 on: March 01, 2016, 09:27:40 pm »
In hardware it is not possible (except for the AT90PWM series). Either you have to do the PWM using timer interrupt + software, or you can use 2 timers set to the same period but one startet with a delay of half the period.
For generating complex PWM waveforms there are much better controllers than AVR.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9946
  • Country: nz
Re: 2 PWM outputs with half a cycle phase shift on AVR
« Reply #3 on: March 01, 2016, 09:34:34 pm »
Maybe you can find a AVR which can generate PWM on a ext int pin.
Then use any ext int state change interrupt to trigger the start of another timer that runs a fixed time and changes another i/o to match what the first one was. 

That would cut down the amount of code needed
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: 2 PWM outputs with half a cycle phase shift on AVR
« Reply #4 on: March 01, 2016, 09:34:57 pm »
As long as you use a timer mode that doesn't mess with the top/bottom of the counting the two timers will stay in whatever sync they are started in.

So it's only a one-off issue getting the 2nd timer to start at the right time.

Perhaps set the timer to first generate a compare interrupt at 1/2 the time and use that to start the 2nd one and then re-purpose the compare to do pwm from then on as you need.

that sounds easy enough. I'll study the datasheets.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9946
  • Country: nz
Re: 2 PWM outputs with half a cycle phase shift on AVR
« Reply #5 on: March 01, 2016, 09:40:58 pm »
The real question is, are there any PWM modes that don't mess with top/bottom.
You may have to do it yourself by calculating when to use 'compare match set output' and when to use 'compare match clear output'

That way the signals are still being generated in hardware, you just have to preload what you want it to do before it has to do it.

EDIT: I think it will have to use a timer mode that updates OCR Immediately. Which rules out all the dedicated pwm modes. Unless there's something i've not thought of.
« Last Edit: March 01, 2016, 09:48:54 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline dgtl

  • Regular Contributor
  • *
  • Posts: 183
  • Country: ee
Re: 2 PWM outputs with half a cycle phase shift on AVR
« Reply #6 on: March 01, 2016, 09:45:35 pm »
On some AVRs some timers share a prescaler. This prescaler can be stopped (set TSM, then PRSYNC). Then set the initial timer counter values of both timers with required offset. Enable both timers, set everything up. Then enable the prescaler and both timers start counting at the same time, staying in sync as they go.
Changing PWM duty cycle is easy. Changing frequency or the phase difference requires stopping of the prescaler. Still, with careful coding, it can be made almost invisible.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9946
  • Country: nz
Re: 2 PWM outputs with half a cycle phase shift on AVR
« Reply #7 on: March 01, 2016, 09:47:52 pm »
Yeah, i think there are a few options for doing this that will work.

Unless you're trying to generate a really fast pwm signal it should be doable with a few timer/int register tweaks inside a few interrupts.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17816
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: 2 PWM outputs with half a cycle phase shift on AVR
« Reply #8 on: March 01, 2016, 10:02:33 pm »
50 to 500Hz is plenty for this, it's that slow that i could do it all in software and make a right pigs ear of it  :-DD but yes if the counters can all be stopped and started together then I can preload 127 in one and start the other at 0 and have then firing same frequency but offset cycles.
 

Offline collingtech

  • Contributor
  • !
  • Posts: 13
  • Country: br
Re: 2 PWM outputs with half a cycle phase shift on AVR
« Reply #9 on: March 02, 2016, 02:15:44 am »
interesting, im in the middle of one project like this one, will open a new project post and put there my aproach, only a minor diff , im using pll , pay attention for the timers :0)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf