How to Blink an LED Using AVR and Atmel Studio?

In this tutorial, we will learn how to blink an LED (ON and OFF) using an AVR microcontroller and Atmel Studio, and how to write a C program for an AVR microcontroller to blink an LED. By Suryaveer Singh Last updated : May 12, 2023

Blink an LED Using AVR and Atmel Studio

This is one of the basic programs, or we can say that a type of "HELLO WORLD" program of an Embedded System. The programming in Embedded System quit simple, it is almost similar to the Basic C programming that we use. The programming of Embedded System (AVR) is done using the application ATMEL STUDIO 6.2.

C Program to Blink an LED using AVR

#include <avr/io.h>
#define F_CPU 1000000
#include <util/delay.h>

int main(void)
{
	DDRA = 0x01;
	
	while(1)
	{
		PORTA = 0x01;
		_delay_ms(1000);
		PORTA = 0x00;
		_delay_ms(1000);
	}
	
	return 0;
}

Screenshot of the program from editor

Program to blink an LED using AVR

Explanation

  1. Here <avr/io.h> is the header file for the AVR, it includes all the functions, classes that are required for our program.
  2. F_CPU indicates the CPU speed of our ATMEGA16(AVR).
  3. The header file #include <util/delay.h> provides the delay that is required in the program. It is basically used to delay the operation performed.
  4. Below the int main(void) we write the pins that are given to our ATMEGA16 board by the LED and is denoted by DDRA = 0x01, where 0x represents the hexadecimal and 01 is the is the position of the LED attached to it.
  5. Inside the while(1) we write the coding which we want to repeat continuously.
  6. Inside the while loop PORTA = 0x01 represents the condition when our LED blinks i.e. the LED will be on when we will receive 1 in our PA0 port and will be OFF if we receive 0.
  7. _delay_ms(1000) shows the delay is micro seconds which we have used 1000.

Simulation

Blink an LED using AVR - Simulation

Simulation Explanation

  1. Open the PROTEUS Professional and from the file Menu open a NEW design
  2. Then go to the left side in the selection Mode, click on P and select the following devices:
    • ATMEGA16
    • RESISTANCE
    • LED of any colour
  3. From the terminal mode on the left side, select the Ground terminal and place it as shown in the figure.
  4. Arrange all the components as shown in the aboveimage.
  5. Here we have not connected any power terminal, because during the simulation it is not required.
  6. Now double click the resistance and change its value from 10k to 330 also double click the ATMEGA16 and a popup will appear there select the file of the that has been created in your respective directory through ATMEGA STUDIO 6.2.
  7. Open the debug file and select the HEX File and open it.
  8. Now go to the left bottom corner and click on play.
  9. Through these Steps the program and Stimulation will be executed and your LED will burn with the respective delay.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.