11/28/2012

Blinking LED using AVR ATmega16





         Blinking a LED using 555 timer ic is simple, you can do the similar job using AVR atmega16. This is a simple program, perhaps simplest, and an introduction to ATmega16. To make a led blink you have to set (logic 1) and reset (logic 0) a pin of the controller continuously.
         there's a code for blinking a LED connected to any pin of portA of the controller.



#include<avr/io.h>
#include<util/delay.h>
int main(void)
{
DDRA=0xFF;            // set portA as out put
while(1)                     // run forever
{
PORTA=0xFF;          //All the pins of portA is set
_delay_ms(1000);      //wait  for 1sec
PORTA=0x00;          //all the pins of portA is reset
_delay_ms(1000);      // wait for 1 sec
}
return(0);
}



        DDR is data direct resister, DDR determines whether a particular pin of a port will work as an input or as an output, writing logic 1 to DDR makes the pin behave as output pin, writing logic 1 to DDR makes the pin behave as input pin. As we I wrote DDRA=0xFF, it will make all pins of portA work as output pin. Suppose you need only PA2 ( pin3 of portA ) as output pin, you need to write 1 to Bit 0 of  data direction resister for portA (DDRA). You can write like this


                                                              DDRA=0x01;
                                                                   or
                                                             DDRA|=(1<<PA2);
                                                                   or
                                                             DDRA|=_BV(2);  


No comments:

Post a Comment