Generating two pwms from same timer with different duty cycle

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

how can I generate two PWM signals from the same timer? when I have the following constraints:
 -First Signal Ton = 20ms & Toff = 30ms
-Second Signal Ton = 60ms & Toff = 60ms

-I only have 1 ocr register as I am using timer0

and a quick question is the OCF flag raised when PWM reaches compare match? or the pwm only raises the TOV flag when it restars?

This topic has a solution.

Sherif Nassar

Last Edited: Thu. Feb 13, 2020 - 09:41 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The second signal isn't technically "PWM" - it's 50:50 duty but anyway.

 

The fact is that the total period of those is 50ms for the first and 120ms for the second. That's two different periods which means two different frequencies. You cannot do that one one timer if you want to use the hardware PWM generation facilities.

 

What I guess you might do is look at the common denominator of all the above which would appear to be 10ms. So you run a time with a 10ms period. Both outputs start on (assuming you want to start in the same place?) then after 2 ticks you turn off the first signal, then after another 3 ticks you turn that one one again. After one more tick (so 6 so far) you turn off the second signal and so on

 

So you need to keep two counts, one for each signal and for the first when the count reaches 2 you make the On/Off transition and when it reaches 5 you make the Off/On transition then reset that counter. The other one counts 3 and 3 then resets. etc etc.

 

PS isn't life a whole lot easier if you simply use two timers ??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah i know its a 50:50 but since I am using the pwm mode I just called them both pwms, but yeah you are definetly right.

and yes that just solved it, i missed the the common denominator thing. 

Thank you man you are a life saver!

Sherif Nassar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

PS isn't life a whole lot easier if you simply use two timers ??

yeah well lol cant argue with that, but its not me who put the constraints I would love to make my love easier anytime any day

Sherif Nassar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sherifb96 wrote:

if you dont mind ,last question

and a quick question is the OCF flag raised when PWM reaches compare match -if the compare match is any value but not top-? or the pwm only raises the TOV flag when it restarts?

Sherif Nassar