To Blink LED in AVR microcontroller, you need to create a LED blinking program for AVR, I hope all of you guys have already installed Atmel Studio 6.0, Proteus. If you have not installed them please install before you proceed further. The links are given below:

Atmel Studio

Proteus: Although, Labcenter Electronics offers a free demo version of Proteus.

Now that you may have installed the software, we are ready to proceed further. Follow these instructions:

  • Open Atmel Studio 6.0

Atmel Studio

  • Click on New Project.

  • In the new window, click on the name column and give whatever name you want to and select the location as well and hit OK.

  • The device selection tool must appear, in the search column search for Atmega 16, select it and then click ok.

Welcome Screen Atmel Studio

  • There you go, a text editor must have appeared on your screen in which you are going to write a program.

General Structure of LED Blinking Program:

To blink led in AVR, you guys should know about the general structure of the LED Blinking program. AVR microcontroller has a register named DDR(Data Direction Register), which acts as a switch between two registers-PORT and PIN. If the particular bit is 1, it means it is allowing data transfer from PORT to PIN register and if that particular bit is ‘0’it means that data transfer from pin to port register is not possible. Also, if you have to write data to the output port, you have to give that value to the PORT register(Output port), and if you have to read data(Input Port), you have to read it from the PIN register.

For those of you guys who don’t know about registers- a register is a memory used to hold data. There are different types of registers-General purpose, Special Purpose and much more. The register I mentioned here is Control register because it is controlling the Data flow(Just like the switch) between Pin register and Port register used for data transferring.

There are various ways to assign the value to DDR register but I am going with the most basic way to do it currently and rest I will write about it in the later post.

DDRB = 0b00000001;

“B” is the name of the port in which you have connected the  LED. 0b signifies to the compiler that the entered value is in binary form and the value signifies that I have given permission to flow of data in Pin 0 of Port B. As these pins can accept input only in binary form, by binary form I mean you can assign only 5v or 0v. There can’t be any voltage between these two values. So by writing this, I am assigning Pin 0 of Port B to 5v.

PORTB = 0b00000001;

According to the points mentioned above, the program should be-

#include <avr/io.h>
int main(void)
{
 DDRB=0b00000001;
 PORTB=0b00000001;
while(1)
       {                //For infinite execution of Program        
       }
}

It’s simple right. Now your work is almost done here. One thing though, this program will only keep the LED ON but it will not blink it. For LED blinking, we have to introduce delay. For delay, we just have to call delay function and enter the parameter in it.

_delay_ms(1000);

The parameter I passed in delay function is in Mili Second, therefore above command should generate the delay of 1 second. One thing though, delay function is defined in util package, therefore you have to include proper header file in the program which is-

#include <util/delay.h>

I hope you guys get the general idea of the program. Full LED Blinking  Program with comments as follows-

#include <avr/io.h>
#include <util/delay.h>//it has delay function
int main(void)
{
DDRB=0b00000001;
while(1)
       {
     PORTB=0b00000001;
     _delay_ms(1000); //so that LED would remain in ON state for 1 sec and then  turns off  for  1 sec.     
     PORTB=0b00000000;
     _delay_ms(1000);
       }
}

After the program, click on Build and then Build “Project name”. If there is no error, then it means your program is successfully build.

Blink LED

When the program is Build, there is a generation of file-“Project name.hex“. For the execution of Program, click on a link given below:

Execution of the program-simulation

Execution of the program-Hardware

Related Post:

AVR Tutorial for Beginner

Share This