go_away

Author Topic: Does PWM run in the background?  (Read 9034 times)

0 Members and 1 Guest are viewing this topic.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #30 on: November 20, 2008, 09:05:16 PM »
OK thank you very much

I'll do some more research on timers and try to understand the scheduler routine. I'll be sure to be back with more questions , :P
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline mbateman

  • Full Member
  • ***
  • Posts: 82
  • Helpful? 0
Re: Does PWM run in the background?
« Reply #31 on: November 21, 2008, 10:26:50 AM »
I still think the hardware PWM is the way to go. You do have to use all three timers, but once set up, you only have to set the pulse-width setting and forget about it until you need to change it. Changing each one is just a single variable change, and 6 of those would easily fit between UART readings.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #32 on: November 21, 2008, 11:44:35 AM »
I still think the hardware PWM is the way to go. You do have to use all three timers, but once set up, you only have to set the pulse-width setting and forget about it until you need to change it. Changing each one is just a single variable change, and 6 of those would easily fit between UART readings.
I still don't understand how three timers could control PWM for 6 channels at the same time . Can someone please explain it?

Also is there an AVRlib file for PWM ?
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline mbateman

  • Full Member
  • ***
  • Posts: 82
  • Helpful? 0
Re: Does PWM run in the background?
« Reply #33 on: November 21, 2008, 12:07:11 PM »
On the atMega168, each timer has two Output Compare Registers (OCR). Those are used to independently trigger PWM waveforms on different pins based on the same timer. Here is the datasheet for the 168 http://www.atmel.com/dyn/resources/prod_documents/doc2545.pdf. Check out Chapter 14, especially figure 14.1 and section 14.5.

Here is a simplified version of what is happening...

- Timer will start at 0 and count up one unit each prescaled clock tick. When at zero, it sets all the enabled pins high.
- If timer value equals either of the OCR values, set the corresponding pin low
- If the timer value equals TOP, reset timer to zero and set all pins high

This is oversimplified, but it gives you the idea. In the PWM Phase Correct mode, it manages to do this in both directions of counting, but the concept is the same.

To initialize, you need the following steps:
- Set prescale value for counter
- Set top value for counter. This sets the total number of units that each cycle will have
- The two values above will set the base frequency for your PWM waveform
- Configure TCCRx to set the compare and WGM modes
- Set OCRxx values for each of the two pins

See my earlier post for a link to my code for the Axon. You will need to modify it since the 168 has different timer configs, but it is a place to start.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #34 on: November 22, 2008, 04:44:21 PM »
Thank you mbateman . Hardware PWM looks like the way to go.

But I also would like to understand how I would do it software-wise. I mean lets say I didn't have the ATmega168 and I was stuck with a chip that has one UART but no PWM.

Would the following code
Code: [Select]
On for 1mS
Off for 20mS
Repeat
give the same voltage as this
Code: [Select]
On for 1uS
Off for 20uS
Repeat
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
Re: Does PWM run in the background?
« Reply #35 on: November 22, 2008, 06:14:06 PM »
Thank you mbateman . Hardware PWM looks like the way to go.

But I also would like to understand how I would do it software-wise. I mean lets say I didn't have the ATmega168 and I was stuck with a chip that has one UART but no PWM.

Would the following code
Code: [Select]
On for 1mS
Off for 20mS
Repeat
give the same voltage as this
Code: [Select]
On for 1uS
Off for 20uS
Repeat

Yes, the "voltage" will be the same, just the frequency will be a lot higher for the second variant.
Check out the uBotino robot controller!

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #36 on: November 22, 2008, 11:19:49 PM »
So the voltage that would be outputted from the microcontroller would be 
VOUT = VIN ∙ DutyCycle
      = VIN ∙  ( TON / TS )
      = 5 V ∙ ( 1 μs / 20 μs)
      = .25 V

An LED requires at least 1.7V to light up or a minimum duty cycle of 34%. I need 255 steps of PWM(where 255 is full 5V and 1 is 1.7V )How can I calculate the proper delay thats needed?

Thanks
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline mbateman

  • Full Member
  • ***
  • Posts: 82
  • Helpful? 0
Re: Does PWM run in the background?
« Reply #37 on: November 23, 2008, 09:57:36 AM »
Your formula is correct if you are talking average voltage out, but that is not really what is happening unless you add some RC circuit to it to smooth it out. If you just connect direct, you get a square wave with voltages of either Vin or 0. In the case of the LED, it will just flicker between on and off creating the illusion of dimming. There will be some inherent delay in the time it takes for it to turn on and off, so you may have to expirament with your base frequency to find what works best.

I did a quick test and 100uS (10kHz) base period seemed to work pretty well. Then you just set the duty cycle from 0 - 100% to control the brightness. So with an 8 bit timer and TOP set to 255, you can use 1-254 for the OCRxx settings to get the full range of brightness. You can't get 10kHz out of the freqency you have, but you can play with the prescaler to get something within an order of magnitude that should work. I have to run out now, but if I get some time later, I can help you with the settings.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #38 on: November 23, 2008, 09:59:11 AM »
In the case of the LED, it will just flicker between on and off creating the illusion of dimming. There will be some inherent delay in the time it takes for it to turn on and off, so you may have to expirament with your base frequency to find what works best
So you're saying it will work , because to the human eye it seems like its dimming ?

Did an experiment and it works perfectly.

My only problem now is deriving some equation for the software to work on . I guess I would need a standard delay between PWM , any ideas on how to calculate what that delay should be? I need 255 steps.
« Last Edit: November 23, 2008, 02:17:44 PM by airman00 »
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline mbateman

  • Full Member
  • ***
  • Posts: 82
  • Helpful? 0
Re: Does PWM run in the background?
« Reply #39 on: November 23, 2008, 06:00:51 PM »
Not sure what you mean by...
Quote
I guess I would need a standard delay between PWM , any ideas on how to calculate what that delay should be?

The delays are already handled for you by the hardware PWM. Once you get a setting with a base frequency that works for you, all you have to do is change the OCRxx settting to change the duty cycle which sets the brightness. Each step would be 1/256, so to get 50% brightness, you would set OCRxx to 128. If you are using 8 bit values for each LED, you can just set OCRxx directly from the 8 bit variable. If you want to set a certain percentage of brightness, where Pis the desired percentage of brightness (0-100), you would set OCRxx to P * 256 / 100. Remember to use a 16 bit variable to do that calculation so it doesn't overflow. The end result will be from 0-256, but the interim calculations will be well above 256.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #40 on: November 23, 2008, 06:44:10 PM »
mbateman I understand hardware PWM, I'm talking about software based PWM
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #41 on: November 27, 2008, 02:24:14 PM »
hey I think I know what I can do

take existing servo controller code for an ATmega and just tweak teh code to be in uS and then I will be able to get variable dimming. Can anyone point me to some servo controller code for the ATmega? I'm reffering to servo controllers that control like 16 servos at the same time.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
Re: Does PWM run in the background?
« Reply #42 on: November 27, 2008, 08:15:15 PM »
You can take a look at the SSC-32 code, it's on their site. But they use SPI to multiplex the servo... Or you can look at the ServoTimeTimer1 library for Arduino, maybe it helps...
Check out the uBotino robot controller!

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,666
  • Helpful? 169
    • Society of Robots
Re: Does PWM run in the background?
« Reply #43 on: December 06, 2008, 01:37:57 AM »
Quote
Each step would be 1/256, so to get 50% brightness, you would set OCRxx to 128.
I've found that its not so linear like that . . . At 50% PWM my green Axon LED gives me ~85% brightness. I haven't a clue why its being non-linear like that . . .

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,166
  • Helpful? 111
Re: Does PWM run in the background?
« Reply #44 on: December 06, 2008, 10:50:46 AM »
Perceived intensity is not linear.

see http://en.wikipedia.org/wiki/Weber-Fechner_law
and http://en.wikipedia.org/wiki/Apparent_magnitude

So you may need a logarithmic look up table. The last article would suggest that:

Perceived intensity = 2.5 log (Real Intensity)

Which would go towards explaining the result that Admin 'perceives'
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,666
  • Helpful? 169
    • Society of Robots
Re: Does PWM run in the background?
« Reply #45 on: December 06, 2008, 11:02:40 AM »
It was definitely exponential, x^2.5 or more would sound about right . . .

Sooooo an optical illusion? Meaning, it got brighter but my eyes couldn't detect any further brightness . . . I guess . . .

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #46 on: December 06, 2008, 11:31:57 AM »
very interesting about the perceived brightness. I'll take that into account.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #47 on: December 09, 2008, 10:48:44 PM »
Perceived intensity = 2.5 log (Real Intensity)

In this equation Perceived Intensity is a number between 0 and 5. Real Intensity is measured in percentage.
So for example we give an LED a real intensity of 50%
Perceived Intensity = 2.5log(50)
                            =4.24742501

Now to convert Perceived Intensity into voltage be merely multiply by 20 - so 4.24742501 * 20 = 84.9485002  or ~85%

So that makes sense but the following does not.
Lets say I want the LED to have a perceived intensity of 20%. So I would do
20 = 20 ( 2.5log(x) )
1= 2.5log(x)
and I solve and get that x = 2.5 . That makes no sense . How can an LED light if its only receiving 2.5% intensity or .125 volts? It won't even light up at .125 volts.

Can someone explain to me where I am wrong and how to calculate how much real intensity I need for 20% perceived intensity?
 
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline mbateman

  • Full Member
  • ***
  • Posts: 82
  • Helpful? 0
Re: Does PWM run in the background?
« Reply #48 on: December 10, 2008, 09:47:03 AM »
PWM is not setting a voltage, it is setting a duty cycle. If you are thinking of average voltage, then, yes, the average is .125V. But that is not what is happening. It is getting Vcc for 2.5% of the time. During that time, it is lit. The other 97.5% of the time it is off. If your base freqency is too slow, you will actually see it turn on and off. But if you set the base frequency high enough, your eyes/brain interpret it as a dim LED.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #49 on: December 10, 2008, 10:07:09 AM »
PWM is not setting a voltage, it is setting a duty cycle. If you are thinking of average voltage, then, yes, the average is .125V. But that is not what is happening. It is getting Vcc for 2.5% of the time. During that time, it is lit. The other 97.5% of the time it is off. If your base freqency is too slow, you will actually see it turn on and off. But if you set the base frequency high enough, your eyes/brain interpret it as a dim LED.

oh I understand now. Thank you.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #50 on: December 11, 2008, 09:47:54 PM »
Just in case anyone is wondering I got the following numbers

Brightness of LED             PWM Duty Cycle       
20%                                  2.5%
40%                                  6.3%
60%                                 15.8%
80%                                 39.8%
100%                               100%

They sorta make sense, I'm going to test it now
Tested it out and it looks all right
« Last Edit: December 11, 2008, 10:27:58 PM by airman00 »
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #51 on: December 20, 2008, 11:53:42 PM »
Perceived intensity is not linear.

see http://en.wikipedia.org/wiki/Weber-Fechner_law
and http://en.wikipedia.org/wiki/Apparent_magnitude

So you may need a logarithmic look up table. The last article would suggest that:

Perceived intensity = 2.5 log (Real Intensity)

Which would go towards explaining the result that Admin 'perceives'


I have a problem , different colors have different sensitivity to the human eye. That means that green might need a lower PWM value to appear at 80% than blue would have needed to appear at 80%.
Here is a graph of the sensitivity of the human eye to certain colors;


Is there some equation that takes into account the sensitivity of the color to the human eye?
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #52 on: December 21, 2008, 12:10:54 AM »
I think I solved my own question

I would do the following steps to get an accurate PWM for all the colors
Find the forward voltages of each of the color LEDs ( they have different ones)
Then for each of the LEDs I use their forward voltage and then add on the PWM I need. Basically its "biasing" the PWM for each LED's color.

Example : Red has a forward voltage of 1.95V so the range of the PWM for red would be from 1.95 to 5V.
    However Green has a forward voltage of 3.5V so the range of the PWM would be between 3.5V and 5V.
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline Kirk

  • Jr. Member
  • **
  • Posts: 44
  • Helpful? 2
Re: Does PWM run in the background?
« Reply #53 on: December 21, 2008, 09:47:43 PM »
Dear Airman,
I see that you have gotten LOTS of advice.  Some of it good and some of it not so true.
The best advice that you have gotten is to read climber's write up on timers.
I also recommend his write up on interrupts

Some Facts:
1) On the mega 168 you have 6 hardware PWM channels
After getting the timers set up, all you need to do is load a value in the OCRxx register
OCR1A=189;
OCR2B=somevariable;
2) Zero CPU time is taken with this method. The PWMs are like separate circuits
3) the 16 bit timer can be configured as an 8 bit timer if you wish
4) You can still use the timer(s) for other things
    for example a timer overflow interrupt might decrement a volatile global variable as a way of keeping time for switch debouncing or some such activity

A must have link for avr users is
http://greschenz.dyndns.org/AvrWizOnline.php
This wizard writes all your initialization code. for timers, baud rates and more!
This wizard Rocks!

Merry Christmas
Kirk

PS
Interrupt hierarchy for the 168 is in the data sheet and is not user changeable
 doc2545 Rev. 2545M–AVR–09/07 page 57

Offline mbateman

  • Full Member
  • ***
  • Posts: 82
  • Helpful? 0
Re: Does PWM run in the background?
« Reply #54 on: December 22, 2008, 11:33:06 AM »
Airman, you are still confusing duty cycle and average voltage. When dealing with LEDs, forget that you ever heard of the average voltage equations. Once you have chosen the series resister values for each LED, then this is a simple on/off (digital) circuit. At no time are you running it at anything other than Vcc or 0V. The average voltage has no meaning.

My suggestion is to choose resisters for each LED such that they appear to be the same brightness on all of all them when connected to Vcc. Then you use the PWM from 0 to 100% duty cycle to determine the perceived brightness. I say, "perceived", because the LED is either on or off. You are just turning it on and off quickly enough that the human eye interprets it as dimming.  As you have already noted, this is not linear, but you have already dealt with that in the equation.

Offline airman00Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: Does PWM run in the background?
« Reply #55 on: December 22, 2008, 12:25:54 PM »
Oh I see now , :P

But how can I set it up for red, blue ,green. It should be a unique equation for each color , shouldn't it?
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,166
  • Helpful? 111
Re: Does PWM run in the background?
« Reply #56 on: December 22, 2008, 01:01:37 PM »
Oh I see now , :P

But how can I set it up for red, blue ,green. It should be a unique equation for each color , shouldn't it?


Well either as mbateman said - by having a larger value resistor for the green. Alternatively: have different PWM lookup tables for each colour. So if green is perceived as brighter then send it, say, an 80% PWM duty cycle to represent 100% brightness; whereas the red gets 100%.

Don't forget this is nothing to do with voltages - its just to do with 'brightness'. So if you can find a PWM setting for each colour that gives the same perceived brightness then you can compute the rest of the figures with the equation I gave before.

At the end of the day - how accurate does this have to be throughout the scale and for each colour? Surely its just a perception and so doesn't need to be a masterpiece of physics - after all 'my eyes' may well work differently to 'your eyes'.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,666
  • Helpful? 169
    • Society of Robots
Re: Does PWM run in the background?
« Reply #57 on: December 27, 2008, 04:18:29 AM »
based on this information I updated the LED tutorial

 


Get Your Ad Here

data_list