AVR Registers explained


The AVR microcontrollers from Atmel are very popular as they are low cost general purpose micro-controller, with an excellent open-source support, fully-featured C compiler. There are several dozen of AVR chips available in different packages.

This series of microcontroller are based on Advanced RISC Architecture. All its ports are 8 bit wide and are Pin addressable it means you can choose a single pin from PORT as input or output. A group of eight pins is called PORT, and ports are named by using Alphabet as PORTA, PORTB etc.

AVRGPIO_zembedded

The AVR microcontrollers are designed to allow dual use of most of its pins (see image). It has the advantage for developer to use these pins as I/O pins if the function they are provided for is not being utilized.

Registers

Basics of AVR programming is nothing but manipulating the bits in various registers. All most everything from configuring the AVR device’s built in peripherals to using it’s pins for input or output is done by using same technique.

REGISTER

A register is a special storage space in which the state of the bits in the register has some special meaning to the AVR microcontroller. Most of the registers are 8-bits wide. Each register has a name and individual bits may also have unique names.

In AVR every port has 3 registers associated with it , each one with 8 bits. This three-register scheme is true of all external ports on all AVR devices. Every bit in those registers configures pins of particular port. There are typically a three configuration registers as follows to set up the hardware the way we want it to work, a register to put data into the device, and a register to get data back from the device.

 

  1. 1.      DDRx

DDR stands for “Data Direction Register” and ‘x’ indicates port alphabet. As the name suggests, this register is used to set the direction of Port pins to be either input or output. For input, set 0 and for output, set 1. If we set DDRB = 0x0F, our upper four pins (PB4-PB7) are set as inputs, while our lower four pins (PB0-PB3) are outputs.

DDRA

In other words 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.

The 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

 

    1. 2.      PORTx

 

It is a 8-bit data register, if our pins are set as outputs, we can write a value to the pins. Writing a 1 to any of the bits will set an output high, 0 sets it low. An interesting thing is that if we have our pin setup as an input, and write our data register high for that pin, an internal pull-up resistor will be activated.

    1. 3.      PINx

 

It is an 8-bit read data 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.is the read data register, it is read only, and returns the actual values of the pins. The 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

 

This three-register scheme is true of all external ports on all AVR devices. <avr/io.h>, which is part of avr-libc, includes the definitions for your register names. At reset, all of these registers are set to 0, so all pins are inputs with pull-up disabled.

As we are using ATmega16 in which following Registers are used for reading and writing to the I/O ports.

AVR_Registers Explained

The values written to the PORTx register is outputted to the pins on Portx that are configured as output pins. The values read from the PINx register is equivalent to the values presently on the pins of Portx which are configured as input pins.

Guy’s Please subscribe with email to recieve  latest post details in you inbox. Please follow us on facebook and twitter…thanks!

 

Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.

Comments

Comments (3)
  1. vikas says - Posted: March 24, 2013

    Very useful information
    but I think the pin diagram is wrong at place pin number 21 this should be port d pin

  2. ovidiu says - Posted: October 28, 2013

    Excellent tutorial, it was very useful for understand how to program my first AVR. Thank you.!

  3. rishabh thareja says - Posted: January 8, 2016

    great info explained in very simple manner. thank you

Leave your comment

Your email address will not be published. Required fields are marked *