In this tutorial i am going to show you, how to generate some pattern using LEDs. The micro controller is ATmega128 and the 8 LEDs are connected to the PORTC of the micro controller.
The code is written in such a way that first the LEDs will be glowing in Right-to-Left fashion and then from Left-to-Right fashion and will continue for ever.
*********************************************************************************
Generating LED Pattern
*********************************************************************************
//Include AVR Header file
#include < avr/io.h >
//Include DELAY Header file
#include < util/delay.h >
#include < util/delay.h >
//Start main function
void main()
{
int i = 0;
//Set a particular port as output
DDRC = 0xFF; //PORTC output
//Set a particular port as output
DDRC = 0xFF; //PORTC output
//infinite loop
while(1)
{
while(1)
{
//Loop 8 times from Right to Left
while( i < 8)
{
while( i < 8)
{
//setting 1 bit at a time in PORTC
PORTC = (1 << i++);
PORTC = (1 << i++);
//apply 100 ms delay
_delay_ms(100);
}
while( i )
{
//setting 1 bit at a time in PORTC
PORTC = (1 << --i);
PORTC = (1 << --i);
//apply 100 ms delay
_delay_ms(100);
}
}
}
}
0 comments:
Post a Comment