Home » Embedded Systems

AVR | Blinking 4 LEDs alternatively

In this program, we will learn how we can blink four LEDs alternately with the help of AVR. This program is almost similar to the last program that we learnt i.e. blinking a single LED using the AVR.
Submitted by Suryaveer Singh, on June 20, 2018

The only difference would be here we would require 4 LEDs and we will do some changes in our programming.

Program to blink 4 LEDs Alternatively

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

int main(void)
{
	DDRA=0x0F;

	while(1)
	{
		PORTA=0x01;
		_delay_ms(1000);
		PORTA=0x02;
		_delay_ms(1000);
		PORTA=0x04;
		_delay_ms(1000);
		PORTA=0x08;
		_delay_ms(1000);
	}
}

Explanation

The detailed explanation of similar program has been done previously so hereI would explain you things shortly.

The DDRA = 0x0F represents the pins that are given to our four LEDs here F represents 1111 in binary i.e. 4 inputs are given to LED.

PINS:	7	6	5	4	3	2	1	0
INPUTS:	0	0	0	0	1	1	1	1

Here 7, 6, 5, 4, 3, 2, 1, 0 are the Pins of the A slot, the ATMEGA16 has four slot A, B, C, D and each port has 8 pins.

In the above program we have given our input to the pins 3,2,1,0.

Inside the While loop, we have written codes such that at a time only one of the LED glows. PORT = 0x01 represents that the input has been given to the 0th pin.

PORT = 0x02 represents that input has been given to 1 pin, and so on...

Simulation

Blink 4 leds in AVR

Explanation

  • Devices required:
    1. ATMEGA16
    2. Four Resistors (330 ohm)
    3. Four LEDs
    4. Ground terminal
  • Arrange the devices as shown above or you can arrange them more beautifully :)
  • Debug the created HEX file in the ATMEGA16 and play the Simulation. You will find that all the LEDs will blink alternately.

In the similar way using more creative Ideas you can create more beautiful designs/patterns using LEDs. Decreasing the delay time will blink LEDs more quickly.

Just for an Example, you can try to blink 2-2 LEDs at a time the only change in the program would in the while loop, there in the PORTA you have to include two inputs at a Time.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.