LED BLINKING using ATMEGA32 AVR micrcontroller

LED BLINKING using ATMEGA32 AVR micrcontroller.This article will tell you how to proceed towards the basic programming of AVR Atmega32. You will learn how to develop codes  for blinking LEDs through Atmega32. You will also learn how to determine direction of I/O pins and how to make the status of output pins high or low.

DEVELOP THE CODE

First develop the code for “led blinking” by using Atmel studio 6. Here we will write code by using C language. For the programming of AVR Atmel32 two registers are used namely, DDR and PORT. DDR is a data direction register which tells the direction whether the pin of Atmega32 is input or output while the PORT register is an output register which tells whether the pin of corresponding port is active low (0V) or active high (5V).

For writing code open Atmel studio 6, selects “new project” choose “GCC C Executable Project”. After selecting “device”, main C window will open, here you can write your code.

The required code of “LED BLINKING” is shown below:

FOR BLINKING ALL LEDs

The code given below is written using AVR Studio 6.

#ifndef F_CPU

# define F_CPU 16000000UL // clock speed is 16MHz

#endif

#include <avr/io.h>

#include <util/delay.h>

int main(void)

{

              DDRD = 0xFF; // declared port B as output

              while(1) // initialize infinite while loop

              {

                     PORTD = 0xFF; // turn on all LEDs of PORTB

                     _delay_ms(1000); // delay of one second

                     PORTD = 0x00; // turn off all LEDs of PORTB

                  _delay_ms(1000); // delay of one second

    } // while loop end

} // main end

 

For blinking all LEDs once
For blinking all LEDs once

FOR BLINKING LEDs ONE BY ONE

#ifndef F_CPU

#define F_CPU 16000000UL //clock speed is 16MHz

#endif

#include <avr/io.h>

#include <util/delay.h>

int main(void) //main starts

{

       DDRD = 0b11111111; // declaring port D as output

    while(1) // initialize infinite while loop

    {

        PORTD = 0b10000000; // pin 0 of port D set HIGH

              _delay_ms(1000); // delay of one second

              PORTD = 0b01000000; // pin 1 of port D set HIGH

              _delay_ms(1000); // delay of one second

              PORTD = 0b00100000; // pin 2 of port D set HIGH

              _delay_ms(1000); // delay of one second

              PORTD = 0b00010000; // pin 3 of port D set HIGH

              _delay_ms(1000); // delay of one second

              PORTD = 0b00001000; // pin 4 of port D set HIGH

              _delay_ms(1000); // delay of one second

              PORTD = 0b00000100; // pin 5 of port D set HIGH

              _delay_ms(1000); // delay of one second

              PORTD = 0b00000010; // pin 6 of port D set HIGH

              _delay_ms(1000); // delay of one second

              PORTD = 0b0000001; // pin 7 of port D set HIGH

              _delay_ms(1000); // delay of one second

    } // while loop end

} //main end

 

For blinking LEDs one by one
For blinking LEDs one by one

After writing the code, compile it and save.

CIRCUIT SIMULATION ON PROTEUS

To observe the working of this code, simulate the circuit on Proteus by using Atmega32, 8 LEDs, registers, capacitors and crystal from the built-in library of Proteus. Notice that the ground and Vcc (voltage supply) pins of Atmega32 are connected by default in Proteus while working in real time you have to connect the Vcc with 5V and ground pins. Set the frequency of crystal oscillator and Atmega32 “16MHz” and set fuse bits.

How to set frequency and fuse bits
How to set frequency and fuse bits

CIRCUIT DIAGRAM

Circuit diagram of blinking one LED at a time is given below.

circuit diagram of led blinking tutorial
circuit diagram of led blinking tutorial

After making the circuit on Proteus, double click on Atmega32 IC load the HEX file of the code.

How to upload hexa file
How to upload hexa file in microcontroller

After uploading HEX file click the play button present on the bottom left corner. The blinking LEDs will show the result.

Simulation result of led blinking circuit diagra
Simulation result of led blinking circuit diagram

This article is written by our content writer Farida Ahmad. Thanks for her time and sharing useful knowledge with us. Don’t forget to share it with your friends. The more you share, the more you gain. t

5 thoughts on “LED BLINKING using ATMEGA32 AVR micrcontroller”

Leave a Comment