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.
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.
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.
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. 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.
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
- 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.
- 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.
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!