Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

AVR I/O Ports- (Part 3/46)

By Vinod Bandlamudi October 14, 2013

 

AVR microcontrollers are the advanced microcontrollers. From 1996 onwards these are come into existence. In AVR family there are so many controllers are available. Mainly the AVR family is sub grouped as ATmega, ATtiny, Xmega, UC3, SAM3 and SAM4. In these form 8 to 32 bit controllers are available. All the controllers having some common and some different features, those are coming to common features all are having I/O ports, timer/counters, interrupts and etc, coming to special features those are A/D converters, PWM, D/A converter, on chip I2C, serial communication interfacings, on chip EEPROM and etc. we can use all these features according to our requirement by programming.

First we have to discuss about I/O ports. Generally AVR microcontrollers having four I/O ports named as PORTA, PORTB, PORTC, PORTD. Take example as ATMEGA8 or ATMEGA 16 or ATMEGA32 microcontrollers, these are any having four I/O ports and each port having 8 I/O lines and totally each controller having 32 I/O lines. These 32 I/O lines are bi-directional means we can use these I/O lines either input or output. In addition to each this pin has some of other functions like ADC, timers, interrupts, serial communication pins and other pins. To perform any operation with general purpose I/O (GPIO) pins there is a need to configure three registers. Those registers named as DDRx, PORTx, PINx (here ‘x’ indicates the name of the register A or B or C or D).
Image showing Registers associated with AVR IO Ports
Fig. 1: Image showing Registers associated with AVR IO Ports
Each registers of these three registers are 8-bit registers means generally each port has 8 pins as like each register has 8-bits and each pin refers on bit of register. If we want to configure any pin of the port we can configure the corresponding bits of all three registers. In this article I am taking example as ATMEGA32 microcontroller. Now conceder pins 22-29 of the microcontroller, if we want to configure these 8-pins, there is a need of configure corresponding three registers of 8-bits. Here explaining clearly, in ATMEGA32 pin 22-29 refers PORTC. So if we want to configure PORTC, we need to configure DDRC, PORTC and PINC registers.
Table listing registers associated with Port C
Fig. 2: Table listing registers associated with Port C
These 8-bits are divided into two 4-bit groups and named as lower nibbles upper nibbles. 0-3 bits are called as lower nibbles and 4-7 bits are called as upper nibbles.
Table showing upper and lower nibble in a register
Fig. 3: Table showing upper and lower nibble in a register
Configuring the PORT:
We have discussed above that if want to configure one port; there is a need to configure corresponding three registers of the port. So now we are configuring the PORTc, so we need to configure DDRC, PORTC and PINC registers. Now we can discuss in detail about these three registers here.
DDRx register:
The name of the register is Data Direction Register. The name only indicates complete use of the register that is data direction. There are only two directions. Those are controller to module and module to controller. If data direction is from module to controller that is input, if data is passing from controller to module that is output. So the two directions are input direction and output direction.
Representation Image of Data Direction
Fig. 4: Representation Image of Data Direction
This register is used to assign the pin of the port as either input direction or output direction. In this register the port and pin indicated as “DDRxn”, here ‘x’ indicates the port name and ‘n’ indicates the pin number. For example pin 26 refers to 4th pin of PORTC is defined as DDRC4. Generally these GPIO pins are digital I/O pins that mean these are having only two logics as logic0 and logic1. Same like these DDR also. If DDRxn is written as logic one, the pin is configured as output pin. If DDRxn is written as logic zero, the pin is configured as input pin.
Up to here we are discussing as logic 0 and logic 1, but how to know what is logic 0 and what is logic 1, how both are differs. Let me explain clearly, generally our most of the microcontrollers drives the 5V, and before setting the pin either input or output there is 5V of potential at each GPIO pin of controller. Microcontrollers having some threshold voltage levels. The threshold level is indicated as half of the driving voltage. If the driving voltage of controller is 5V, the threshold voltage of the controller is 2.5V. So the controller knows that if voltage level is below the threshold level, it sense as logic zero and above the threshold level it sense as logic one.
So in this sense logic zero means DDRxn configured as input pin and logic one means DDRxn configured as output pin. If pin is configured as input, the internal pull-up resisters will be on. If we want to turn-off the pull-up resisters, the pin has to be configured as output.
DDRx register
Bit Values of DDRx Register
Fig. 5: Bit Values of DDRx Register
Now take an example of configuring some bits of PORTC as inputs and some bits as outputs. PORTC has 8 pins, which mean DDRC register has 8-bits as 0-7. In the 8 pins configure 0,2,4,6 pins as inputs and 1,3,5,7 pins as outputs. Generally the register initial value is zero means register bits are configured to input. So change require bits to logic one to change as output. Generally we are using shift operation to assign value to bit of register. Take bit0 of DDRC, the initial value of the bit is zero. If we want to change the bit to one, just we can use simple shift operation as 1<<DDRC0. If we want to change remaining bits, that is also same but OR operation is performed between the bits. No we can take the above example and write the code.
DDRC = (1<<DDRC1) | (1<<DDRC3) | (1<<DDRC5) | (1<<DDRC7);
The above line indicates that setting 1,3,5,7 pins of PORTC as outputs and setting 0,2,4,6 pins of PORTC as inputs. There is so many ways to perform this operation mentioned below.
DDRC = (1<<1) | (1<<3) | (1<<5) | (1<<7);
                        Or
DDRC = 0xAA;
                        Or
DDRC = 0b10101010;
These are the several ways to setting directions of the PORTC.
PORTx register: 
PORTx register is used to control the voltage on the hardware pin. As same like DDRx register, this PORTx register is also having 8-bits. If bits of the PORTx are written as logic one, the port is driven to high means 5V voltage having at the pin. If bits of the PORTx are written as logic zero, the port is driven to low means 0V voltage having at the pin.
Internal Circuit Diagram of AVR Pin
Fig. 6: Internal Circuit Diagram of AVR Pin
This register is very useful for setting output pins either low or high. DDRx register set some bits are input bits and some are output bits. The PORTx register is very useful to set the output bits as either high or low.
Bit Values of Portx Register
Fig. 7: Bit Values of Portx Register
This register declaration is similar to the DDRx register, the only change is name of the register. Take the above example, setting some of output pins are low and some of output pins are high. Here we are setting 1, 5 pins are high and 3, 7 pins are low.
PORTD = (1<<PC1) | (1<<PC5);
                        Or
PORTD = (1<<1) | (1<<5);
                        Or
PORTD = 0b00100010;
                        Or
PORTD = 0x22;

 Steps for Programming

PINx register:
This register is used to check the how much voltage in the pin. The PINxn will be 1 when pin has 5V and PINxn will be 0 when pin has 0V in that pin. If we want to read the data at particular pin, we should read the PINx register. As like above registers, the setting the value to the register is same.
Bit Values of PINx Register
Fig. 8: Bit Values of PINx Register
Taking the same example as above,
PINC = (1<<PC1) | (1<<PC5) | (1<<PC7);
                        Or
PINC = (1<<1) | (1<<5) | (1<<7);
                        Or
PINC = 0b10100010;
                        Or
PINC = 0xA2;
Example:
Interfacing one LED to PORTC and one button to PORTD and when the button is pressed, the LED will glow.
Steps for programming:

 

       1.      Configure inputs and outputs by using DDRx register.
       2.      Setting pins as low or high by using PORTx register.
       3.      Read the Pin value by using PINx register and perform the operation.
First step of the program is initializing the library files and header files.
#include<avr/io.h> this is the header file to load all I/O operations of avr microcontroller.
#include<util/delay.h> to perform the delay operation.
Next thing is configuring the input and outputs. Here we are taking PORTD as input and PORTC as output. We are taking pin PD0 as input pin and pin PC0 as output pin. Configuring these two pins one is output and one is input by using DDRC and DDRD registers.
DDRC = 0x01; setting PC0 as output
DDRD = 0x00; setting PD0 as input
Bit Values of DDRB and DDRC Registers
Fig. 9: Bit Values of DDRB and DDRC Registers
Next thing is setting pins as high or low by using PORTx register. Initially the output pin should be low because when the button is pressed then the light should be glow and input also should be low.
PORTC = 0x00;
PORTD = 0x00;
Next final thing is reading the pin value by using PINx register and perform the operation. Here we are using “while (1)” means infinite loop. The loop will execute infinite times.
while (1) //infinite loop
{
            PORTC=0x01; //initially on the LED
            if (PIND & 0x01) //condition check, reading the PD0 value
            {
                        PORTC=0x00; //turnoff the LED
                        _delay_ms (100); //delay
            }
}

 

Project Source Code

###

/*
 * AVRIO1.c
 *
 * Created: 10-10-2013 Prince 10:46:28
 *  Author: Vinnu
 */ 


#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRC=0x01;
DDRD=0x00;
PORTC=0x00;
PORTD=0x00;
while(1)
{
PORTC=0x01;
if (PIND & 0x01)
{
PORTC=0x00;
_delay_ms(100);

}

}
}

###

 


Project Video


Filed Under: Electronic Projects
Tagged With: avr
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Thermal modelling of repetitive power pulse
  • Redundant XORs
  • No Output Voltage from Voltage Doubler Circuit in Ansys Nexxim (Harmonic Balance Simulation)
  • Discrete IrDA receiver circuit
  • ISL8117 buck converter blowing up

RSS Electro-Tech-Online.com Discussions

  • Behlke swich
  • Simple LED Analog Clock Idea
  • using a RTC in SF basic
  • Can I make two inputs from one??
  • Help with finding unique wire lug(s)

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe