Friday, May 23, 2008

Varying brightness of LED using PWM

We did it by using timer/counter2 register in atmega8.

Timer/Counter2 is a general purpose, single channel, 8-bit Timer/Counter module. The main
features are:
• Single Channel Counter
• Clear Timer on Compare Match (Auto Reload)
• Glitch-free, phase Correct Pulse Width Modulator (PWM)
• Frequency Generator
• 10-bit Clock Prescaler
• Overflow and Compare Match Interrupt Sources (TOV2 and OCF2)
• Allows Clocking from External 32 kHz Watch Crystal Independent of the I/O Clock

our program for varying brightness of LED which is connected to the pin PB3(oc2) is given below

#include< avr/io.h >
int main(void)
{
TIMSK=0XC0;
TCCR2=0XE1;
DDRB=0X08;
PORTB=0XFF;
OCR2=0X7F;
return(0);
}

the above program create a 50% duty cycle of pwm. And lights the LED with half the brightness.
By varying the value of OCR2 register we can change the brightness.
For eg by giving it as 0x00 LED stops lighting and by giving 0xff LED lights with full brightness.

for understanding the above program you must know something about the use of registers and bits in them which is explained in complete datasheet of atmega8.

2 comments:

sreeraj said...

good posts

keep it up.
don't loose the momentum

suji said...

Thank U srj...