Tuesday, February 9, 2016

Introduction

             In this tutorial I am going to give an introduction of how to program an AVR microcontroller input/output pins. AVR is a seris of microcontrollers manufactured by ATMEL corporation. Before learning how to program an AVR, the learner must know the organization of the controller. In this first part I am going to explain the port of the AVR mocrocontroller. We know , every microcontrollers have ports to communicate with outside word. A port may be used to provide a data to the outside devices or may be used to take data in to the controller.The first case we can say that the port if an OUTPUT port and later case it is an INPUT port. So in simple word a port can be OUTPUT or INPUT. If a port is output, the data can only be travel form the controller to the outside device. If a port is input, it can only take a data from outside device in to the controller. Every microcontrollers have to communicate with outside world. So in order to communicate, it need port. The following are the typical ports which are usually find in a microcontroller.

  • The parallel Port
  • The serial port


1)The parallel port 


           In the parallel communication, the data bits are transmitted at a time using number of lines corresponding to the data bits. That is if we are using 8bit data , we need 8 lines or 8 pins in order to transmit 8bits at a time.  

AVR Parallel PORT 


            In AVR, there are parallel port for communication. A port can only be INPUT or OUTPUT at a time. The parallel port in avr is programmable. It means that the user can be configure the port as input or output. A port can be 8bit,16bit or 32bit according to the series of the controller. Here we are taking an 8bit port usually found in most AVR microcontrollers. The following points are to noted first 

* A parallel port in AVR is a collection of two or more registers
* Each of these registers can be used to set the port behavior of the port
*The following are the registers associated with parallel port in AVR

  1.         DDR REGISTER 
  2.         PORT REGISTER 
  3.         PIN REGISTER
            So PORT, DDR and PIN are the registers helping the port pins to be configured. Normally the AVR series controllers have more than one ports. In that case each port is named with alphabets starting from A. That is the name of the port will be PORTA, PORTB, PORTC and so on.  So the corresponding port registers also be named as PORTA, PORTB, PORTC. Also DDRA, DDRB, DDRC and PINA, PINB,PINC so on. Now on wards we use x instead of port name. x can be any alphabet name of port. PORTx,DDRx and PINx.

 What is DDRx Register ?


   As already detailed above, the port pins of avr can  be input or output at a time. So before doing any programming, let the controller know whether the corresponding PORT is inputting or outputting the data. In order to tell the microcontroller , we use DDR registers. Wich is Data Direction Register which sets the direction of the PORT pins.

   For example, we are considering PORTA of AVR microcontroller, as shown in the figure 1, it has 8 pins on IC named PA0 to PA7. These 8 bits are used for 8bit parallel communication. As mentioned earlier, each port in AVR is a combination of three registers. For PORTA, it will be PORTA, DDRA and PINA registers. All of these registers are 8 bit because the port bits are 8 bit in IC.  At first stage we have to mention or program wheather the port pins are inputting or outputting the data. For that we are using DDRA register. If we load a one or set a bit in DDR register, the corresponding port bit will act as OUTPUT port line. The DDRA  register is shown in below figure, it has 8bits named PA0 through PA7. Each of the bit is associated with PA0 through PA7 physical port pins of PORTA. So writing a bit “1” in DDRA register makes the corresponding PORTA bit in the physical output to be set as OUTPUT. And if we write a “0” in DDRA register makes corresponding bits in port as input.






Making all pins of PORTA as OUTPUT






  For making all PORTA pins as OUTPUT, only we have to do to set all bits in DDRA register by writing the code as below.

DDRA=0xFF; //hex value
DDRA=0b’11111111; //binary value

 You can use this code in AVR GCC . First one is the hex value which is indicated by 0x at the staring and second is the binary value which is indicated 0b’ at stating. So the whole PORTA is OUTPUTTING the data.

Splitting PORTA into two halves and make one half INPUT and other as OUTPUT

 








  For making half input and half output we use the following code

DDRA=0xF0; //hex value
DDRA=0b’11110000; //binary value

 So after writing this, the lower half(4 bits) of the PORTA is an INPUT port and upper half(4 bits) is an output is output port. 


Making individual pins of PORTA as OUTPUT or INPUT

 




DDRA=0x56; //hex value
DDRA=0b’10011010; //binary value


 By writing this code you make PA6,PA5,PA2and PA0 as INPUT port bits and all others are OUTPUT port bits. So you can also individually program the pins of a port either INPUT or OUTPUT.




What is PORT register ?





   The data written in to the PORT register will be transmitted though the PORT pins to the outside world. Considering the PORTA register, if we write a value to this register, it will be available at physical PORTA pins of the IC only if you made this port as output 
in DDRA register. The code as follows 

DDRA=0xFF; // making PORTA as OUTPUT

PORTA=0x05; // outputting hex data 05 through PORTA 








What is PIN register?

 






 PIN register is also an 8 bit register, which stores the data incoming through the corresponding PORT of AVR, only if the PORT is configured as INPUT in DDR resister.
Considering PORTA, if you want to read an incoming data from PORTA, the first we have to declare a character or integer. The following is the code

DDRA=0x00; //making PORTA as INPUT

Unsigned char Value; // declaring a char to store the PIN register Value

Value = PINA; //the incoming data through PORTA is in the PINA register is Transferred to Value





















No comments:

Post a Comment