Thursday, December 06, 2012

EMBEDDED SYSTEMS TUTORIAL 5- GPIO IN AVR



Hiii………friend this is next tutorial on AVR microcontroller, I know it took some time because I was trying to make it crystal clear for you all. In this tutorial we are going to discuss about basic GPIO( general purpose intput output) in AVRs.

All the interfacing of input and outputs of an AVR MCU is performed through PORTs. Before starting anything take a look of ATMEGA 16.





Here you can see there are 32 I/O pins grouped as A,B,C,D with 8 pins in a group. These groups are known as PORTs. You can see there are some special functions written in bracket  in front of these I/O pins, these are are the special function which our AVR MCU can perform with these specific pins other than I/O. we will discuss about all these functions in upcoming tutorials.


REGISTERS


All the processes in a MCU are performed through the help of registers. Register is a 8 bit location in the RAM. Here there are total 32x8 bit registers in ATMEGA16. Though the size of register is 8 bit that’s why it is called an 8 bit MCU.


HOW TO CONFIGURE INPUT /OUTPUT PROCESS IN MCU

Each PORT contain three type of registers.

DDRX- set weather a PORT is input or output.
PORTX- setting the value of output PORT.
PINX- reading the value of input PORT

DDRX( Data Direction Register)

With the help of this register we can make a port input PORT or output PORT.
To make a pin input or output we set its corresponding bit as

Set bit 1 to make it a output pin
Set bit 0 to make it a input pin.

So if we want to make all the pins of PORT A as input pins and all the pins of PORT B as output pins ,in our code we will write


DDRA=0x00;                          0x for hexadecimal
DDRB=0x FF;

OR

DDRA=0b00000000;                 0b for binary
DDRB=0b11111111;

Similarily if we want to make only first four pins of PORT C as input and last four as output, we will write.

DDRC=0XF0;
OR
DDRC=0b11110000;

PORTX (PORTX DATA REGISTER)

This register is used to set the value of a PORT, now a pin can be input or output let see both the cases

OUTPUT PIN

A pin be set to HIGH OUTPUT +5V  by setting it to 1, and can be set to LOW OUTPUT OV by setting it to 0.

For example we want high output on PC0,PC1 pins of PORTC and low output on rest of the pins of PORT C, then we can configure it as

PORTC=0x02;
OR
PORTC=0b00000011;

No the desired output sequence ia available on PORTC.

PINX (   Data read register)

This register is used to read the value of a PORT, If a pin is set as input then corresponding pin can be read as

0 for low input
1 for high output.

Now suppose PORT C is input port and we have connected a switch or sensor at PC0, then we can read the value of PC0 with the help of PINX register. You can read the state of key/switch by simply reading the PINC. It will be ‘1’ when  pressed and ‘0’ if  un pressed. 



CODING

...
if(PINC & 0b00000001)
{
 //Switch is pressed
 ...
}
else
{
 //Switch is not pressed
 ...
}
...
  
NOTE- all the input output functions needs the header file <avr/io.h> it must be added at the starting of the program.

In case you are not getting this code , I will suggest you to go through basics of C first. Download some tutorials from web or simply get any book on basics of c/c++.



Now its time to  use all this stuff in a code so we are going to implement a simple input output project in which a led will not  glow as output when switch will be pressed as input.
Here we are connecting the LED at PB0 and a push button at PC0. When we push the switch LED does not  glow otherwise it glows.

The source code is given below just compile this code get hex file and program your MCU with this hex file.


/*
 * IO_PROJECT.c
 *
 * Created: 10/16/2012 3:07:51 AM
 *  Author: ABHILASH
 */


#include<avr/io.h>
#include<util/delay.h>

int main(void)


{
 
                DDRB=0xFF;    // define PORT B as output PORT
                DDRC=0x00;   // define PORT C as input port
                  int s1=0;  // initializing and defining s1=0, acquire switch as s1
                while(1)      // create infinite loop
                {  
                                s1=PINC&0b00000001;  // acquire switch status connected at PC0
                                if(s1==0b00000001)   // check if switch is pressed
                                {
                                                PORTB=0b00000000;  // turn of led by setting  PB0 at low output
                                }
                                else
                                {
                                                PORTB=0b00000001;   // turn on LED by setting PB0 as high output
                                }
                                }
}



Now implement this circuit on the breadboard and you will we able to see the desired results…



Result will be like this.



.Switch pressed LED  not glowing.



 Switch not pressed LED glowing






some times there occurs the problem of switch debouncing, we will discuss about that in any upcoming tutorial.

I  hope this article was beneficial for beginners. in next tutorial we will discuss about some other basics of AVR. If you have any doubt, question or critical comment please post here.

THNX...:)

No comments:

Post a Comment