Showing posts with label io. Show all posts
Showing posts with label io. Show all posts

Sunday, February 3, 2013

Switch and LEDs

In this tutorial I am going to show you, how to blink a LED whenever a switch is pressed using AVR micro controller. I am using ATmega128/ ATmega64 (both of the micro controller can be used interchangeably as they differ in the size of flash memory only). The LED is connected to pin 0 of PORTC (PC0) and the switch is connected to the pin 0 of PORTA (PA0) of the micro controller. I have assumed that the LED is connected in sinking logic i.e. whenever +5V is provided by the controller the LED will be OFF and whenever the 0V is supplied the LED will be ON and the switch is ACTIVE LOW i.e. it provides a 0V on being pressed.

The step to program are as follow-

1. Set the corresponding PORTs as input or output.
2. Enable pull-up resistors for the input pin.
3. Check for the input continously.
4. Whenever the switch is pressed, turn on the LED.

Here is the code:

//Include AVR Header file
#include"avr/io.h"

void main()
{ 
//Set pin 0 of PORTC as output
DDRC |= (1<<0);

//Set pin 0 of PORTA as input
DDRA &=~ (1<<0);

//Enable pull-up resistor so that false inputs can be ignored
PORTA |= (1<<0);

  //infinite loop
while(1)
{
//Check for any input if an input (0v) is detected, turn ON the LED
if( !(PINA & (1<<0)))
PORTC &=~ (1<<0);

else //turn OFF the LED
PORTC |= (1<<0);
}
}
In place of LED you can connect different output devices such as Buzzer, Relays etc.

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

         }  
     }
}

Thursday, March 1, 2012

Blinking LEDs

In this tutorial I am going to show you, how to blink LEDs using AVR micro controller. I am using ATmega128/ ATmega64 (both of the micro controller can be used interchangeably as they differ in the size of flash memory only). The 8 LEDs which I am going to blink/toggle are connected to PORTC of the micro  controller and they will toggle after 100 ms. So let's start with it............

*********************************************************************************
Blinking LEDs
*********************************************************************************

//Include AVR Header file
 #include"avr/io.h"
 
//Include DELAY Header file
#include"util/delay.h" 

//Start main function
void main()
{
   
     //Set a particular port as input or output
     DDRC = 0xFF;            //PORTC output
 
     //infinite loop
     while(1)              
     {
          //set all the pins of PORTC to 1  
         PORTC = 0xFF;      
 
          //apply 100 ms delay
         _delay_ms(100);        

         //set all the pins of PORTC to 0  
         PORTC = 0x00;            

         //apply 100 ms delay
         _delay_ms(100);      
     }
}

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