Handling the Digital Input Output in AVR Micro Controllers

I have already discussed about a few chapters necessary to get into AVR programming. Now this is the first article that deals with programming.  Let us start with the basics.

Handling the Digital Input Output in AVR Micro Controllers

Digital input output (I/O) is the basic feature supported by  AVR micro controller. To facilitate digital input output, three registers are associated with each port of the micro controller.

  • Data Direction Register– This register determines which of the pins will act as an output port, and which of them as input.
  • Data Output Register-This register holds the Data output to the port.
  • Data Input Register– Reads data from the port

Now let us look into an example. For Port A the three registers are- DDRA, PORTA & PINA respectively. Similarly for port B, it will be- DDRB, PORTB & PINB.

Now before going to further discussion, there is  one important thing I should mention. One port serves for more than one purpose. It is the designer’s responsibility to eliminate every possible error. If the program and system are not designed carefully, fallacy may appear.

Circuit Diagrams using Atmega32 and Atmega8

Note: Click on the circuit diagram to get a large and clear view.

And find below the circuit using Atmega8

Glow an LED using Avr Microcontroller

Program 1: Basic Program Showing Digital Output functionality.

#include
#define F_CPU 1000000
#include
int main()

{ DDRB=0x0f;

PORTB=0x00;

while(1)

{ _delay_ms(1500);

PORTB =~PORTB;
}

return 0;
}

Description:

This program consists of two simple statements, including one delay loop. The ‘~’ symbol stands for bitwise not operation. Here CPU operating Frequency must be defined for proper implementation of the delay loop, and it has to be declared before the ‘delay.h’  line in the C code. Here I’ve used the #define F_CPU  1000000.

There is another option available to define the CPU Operating frequency. In Avr Studio, go to ‘Project Menu> Configuration Options> General’. Here you can choose your device model and its operating frequency.

As I’ve previously discussed, The Data direction register must be set up before doing any input/output operation.

Handling the Digital Input Output in AVR Micro Controllers schematics

Desired Output:

The LEDs connected to Port B blinks with 3 second time period.

Hex Files: 

Note: All the HEX files given in this article are converted to RAR format (for safety and size reduction purpose). To get the HEX file as such – you may download each  file and EXTRACT it to your computer using Winzip or Winrar.

Read more: Handling the Digital Input Output in AVR Micro Controllers


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top