.

INCLUDES RFID interfacing with avr GPS interfacing with avr RF Module interfacing with avr Stepper Motor With AVR

Saturday, December 17, 2011

How to interface LED with AVR Microcontroller (ATmega16)



       ATmega16 has 32 I/O pins to communcate with extenal devices.  Before interfacing with external devices, these pins must be cofigured as input or output pin. This article demonstrates the basic I/O operation of ATmega 16 using LEDs.

       All the four ports can be configured to read an input from some external device or to give output to any external device as per the application. For e.g., a switch is connected to a particular pin, that pin should be configured as input to read the values from the switch (external Device in this case) and if you are connecting a LED to any pin of the port then that particular pin should be configured as output to transmit the signal to the LED (external device in this case). A single port can be configured such that some of the pins of the same port are input and some are output.

Configuring IO Ports:
Every port (PORTx, x = A or B or C or D) of AVR microcontrollers have three registers associated with it:
1.      DDRx: Data direction Register, to set the direction of each pin of PORTx and configuring it to be as input or output.
2.      PORTx: The values which are to be supplied at the output of the port are written in this register. These values acts as input to the device connected at output port of the microcontroller (through PORTx output configured pins).
3.      PINx: This register stores the input value from the external connected hardware, when the port is configured as input port. The input data is read from PINx register.

So the first step in configuring or initializing any of the IO port is to set its direction in data direction register (DDRx) to define the behavior of individual pins as input or output. A high (1) in any bit of the DDRx register means the corresponding pin is set as output and vice versa.

Setting the pull up value:
All the four ports of Atmega16 are equipped with internal software controlled pull up resistors. Every pin of the ports can be pulled up by setting the values into the register PORTx. Following table illustrates the actual state of the pin with different combination of DDRx and PORTx values.
Example: Following commands can be used to disable and enable pull-ups on PORTD.
PORTD = 0xFF;         //PORTD pins pulled high.
PORTD = 0x00;          //Disable pull up registers.

To understand above commands a simple LED blinking experiment is explained below.

// Program to blink LED using AVR Microcontroller (ATmega16)
#include<avr/io.h>
#include<util/delay.h>
int main(void)
{
    DDRA=0xFF;
    while(1)
    {
        PORTA=~PORTA;
        _delay_ms(1000);
    }
}

No comments:

Post a Comment