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 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

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.

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

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

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

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
When i uploaded that code the delay was closer to 15 seconds even though i stated 1000 inside delay_ms. Why is that?
Check your crystal oscillator settings or if you are using external make sure to use the correct value.
Can we make this code shorter?
Good day, i`m asking for tutorials for using micro Python on any microcontrollers
Please check here:
https://microcontrollerslab.com/category/raspberry-pi-pico/