Friday, March 9, 2012

Generating LED Patterns


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 >

//Start main function
void main()
{
   
     int i = 0;

     //Set a particular port as output
     DDRC = 0xFF;            //PORTC output
 
     //infinite loop
     while(1)              
     {
          //Loop 8 times from Right to Left  
         while( i < 8)      
         {
              //setting 1 bit at a time in PORTC
              PORTC = (1 << i++);     

             //apply 100 ms delay
             _delay_ms(100);

         }
          //Loop 8 times from Left to Right  
         while( i )      
         {

              //setting 1 bit at a time in PORTC
              PORTC = (1 << --i);     

             //apply 100 ms delay
             _delay_ms(100);

         }  
     }
}

0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews