The AVR Microcontroller Digital I/O Ports

Introduction to AVR Digital Input/Output


Atmel AVR 8-bits microcontrollers provide pins to take in/output information form/to the outside world in the form of logic values. These pins are usually organised in groups of eight (8) and referred to as a port. The AVR use the alphabet to name these port, example PortA, PortB, etc. The figure below shows the pins of an AVR 8-bit microcontroller which has four (4) digital I/O ports: PortA, PortB, PortC and PortD. The pins of PortA are: PA0 - PA7.

Note
Notice the alternate name of the Ports pins. The AVR microcontollers are designed to allow dual use of most of its pins. This has the advantage of allowing a developer to use these pins as I/O pins if the function they are provided for is not being utilized.
In this AVR tutorial we are only concern with the pins in there Digital I/O function, so for now just forget they have alternate capabilities.
Important
The pins of the AVR microcontroller are not fixed as input or output at the manufacturing stage, these pins are software configurable which is the topic of the section below.

Associated I/O Registers


Each of the AVR Digital I/O ports is associated with three (3) I/O register. A Data Direction Register (DDRx), A Pin Register (PINx) and a Port Register (PORTx). Where x is the port A, B, C, etc. .


DDRx - Port X Data Direction Register
AVR Microcontroller DDR Register
DDRx is an 8-bit register which stores configuration information for the pins of Portx. Writing a 1 in the pin location in the DDRx makes the physical pin of that port an output pin and writing a 0 makes that pin an input pin.
Note: Each physical pin of a port is configured independently and thus a port can have some of its pins configured as input an the others as output pins.


PINx - Port X Input Pins Register
AVR Microcontroller Pin Register
PINx is an 8-bit register that stores the logic value, the current state, of the physical pins on Portx. So to read the values on the pins of Portx, you read the values that are in its PIN register.


PORTx - Port X Data Register
AVR Microcontoller Port Register
PORTx is an 8-bit register which stores the logic values that currently being outputted on the physical pins of Portx if the pins are configured as output pins. So to write values to a port, you write the values to the PORT register of that port.

Programming AVR Microcontroller Digital I/O in C

Introduction to AVR Digital I/O C Programming


This AVR tutorial looks at AVR programming for digital I/O in C. Before we start looking at actual programming AVR microcontrollers Digital Input/Output (I/O)recall that each AVR Digital I/O port is associated with I/O registers. Namely the DDRx, PINx and PORTx registers, where x represent the port: A, B, C .....


The DDRx register is use to configure the pins on portx as input or output pins. Each pin on a port is independent and thus the entire port does not have to be configured totally as an input or output port. Writing a 1 in the pin position in the DDRx will configure that pin as an output pin and writing a 0 will configure the pin as an input pin.


Logic values written to the PORTx register is outputted to the pins on Portx that are configured as output pins.


Logic values read from the PINx register is equivalent to the values presently on the pins of Portx which are configured as input pins.


Programming the Digital I/O pins of an AVR in C


The AVR C code below shows how to configure the pins on a port
                 DDRA = 0xFF;       //Configure PortA as an Output port
 
DDRB = 0x00; //Configure PortB as an Input port
 
DDRC = 0xF0; //Configure first four pins on PortC as
//Input pins and the others as output
The AVR C code below shows how to write to or read from the pins of a port once they are configured. Assume here the configurations from the C code above.
                 PORTA = 0xFF;      //Write all 1's to the pins of PortA
 
PORTA = PINB; //Read values from pins of PortB and
//write to pins of PortA

Example


Write a program to be downloaded to the Atmel AVR ATMega32 microcontroller which continuously read the logic values on portB and write them to portC.

Solution
The program below, written in AVR Studio 5 and AVR Studio 6, accomplish the task that was asked above. There are several things to note here.
  1. The header file avr/io.h must be included in order for us to use the name of the ports.
  2. The ports must be configured before they are used. The first two(2) lines in the main() function of the program are for configuration.
  3. Once the ports are configured you can then write to or read from them, were applicable.
  4. The use of the while(1) loop allows for the continuous read and write operation.
/*
* Written in AVR Studio 5 / AVR Studio 6
* Compiler: AVR GNU C Compiler (GCC)
*
* Author: AVR Tutorials
* Website: www.projectbandya.com
*/

 
#include<avr/io.h>
 
int main()
{
DDRB = 0x00; //configure portB as input
DDRC = 0xFF; //configure portC as output
 
while(1)
{
PORTC = PINB;
}
return 0;
}


For another Digital I/O C programming example check out the further tutorials

projectbandya

No comments:

Post a Comment