Page 1 of 1

PWM in AVR atmega32

Posted: Fri May 11, 2012 7:05 pm
by rags
hello sir,

i want to increase and decrease the brightness of a LED..how do i do tat in flowcode avr?? can u upload a demo prog it will be really helpfull thanku!!

Re: PWM in AVR atmega32

Posted: Fri May 11, 2012 8:54 pm
by medelec35
Hi rags,
I can help you with that.
What target device are you using?
If I know that, then I can create a little demo program for you in AVR format.

But the general principle of controlling a brightness of an led is to keep the frequency the same (at lease 100Hz minimum) but vary the duty (on to off ratio).
E.g if duty is 10% ie on time is for 10% and off time is for 90% then the LED will be dim.
If duty is 85% ie on time is for 80% and off time is for 15% then the LED will be nearly, at maximum brightness.

There are two ways of producing a fixed frequency with a varying duty cycle.

1) By far the easiest is use the built-in PWM component assuming your target device supports it. You can find the component within the Mecatronics menu
Just select PWM, then drag a component icon and select PWM(0) and Enable. If only 1 PWM then nldx will be 1
(if two PWMs then ndlx=1 for pwm(0) and ndlx=2 for pwm(1). I wont go any further as I can produce a flowchat to help you with PWM components.

2)Using timer0 overflow. There is an interrupt tutorial here:
http://www.matrixmultimedia.com/article.php?a=55

But the basics of the timer interrupt (not my words)
A interrupt is a signal that stops the current program forcing it to execute another program immediately. The interrupt does this without waiting for the current program to finish. It is unconditional and immediate which is why it is called an interrupt.
So the interrupt could be used to generate the fixed frequency.

If your target has PWM component, would you like a demo program with PWM component or Bit bang timer interrupt method?

Re: PWM in AVR atmega32

Posted: Sat May 12, 2012 3:10 pm
by rags
thanks a lot sir, i am planning to use atmega32 as my target chip!!
and i can use the same principle of led to control the speed on dc motors?? i could do with the simulation but i think a demo prob would be a much better solution, thanks a lot! now i could understand PWm even better!!

Re: PWM in AVR atmega32

Posted: Sat May 12, 2012 3:48 pm
by medelec35
rags wrote:i am planning to use atmega32 as my target chip!!
The chip was in the title. :lol:

Opticians time I think :P
rags wrote: can use the same principle of led to control the speed on dc motors?? i could do with the simulation but i think a demo prob would be a much better solution, thanks a lot! now i could understand PWm even better!!
Yes you can as I have successfully did just that with DC motor controlling a golf trolley (not mine as I don't play)

The only thing is determining correct frequency to drive the motor which has to be much higher than 100Hz. I set the PWM to 10KHz

Although you can then drive LED at this higher frequency.


I have attached a basic flowchart.

I have never used AVR hardware, so I'm only guessing that PWM output will be at pin PB3?


PWM outputs are at fixed pins determined by the manufacture.
This is not selectable like the normal i/p's or o/p's

Martin