Handling the Digital Input Output in AVR Micro Controllers Using Atmega

Digital input output

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.

Digital input output

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 300x191

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.

Glow an LED using a Push button switch with Avr

Program 2: Basic Digital Input functionality.

#include

int main()

{ DDRB=0x0f;

DDRD=0x00;

while(1)

PORTB =~PIND;

return 0;

}

Description:

Before, I’ve discussed how to put data into a port configured as an output port. Now let us see how to take input! To read output from a port, we use ‘PINX’. Where ‘X’ ix the designation of the port. Remember, we put output using the variable ‘PORTX’ variable, but we read input using ‘PINX’ variable. For a particular port, these two variables have different addresses. If you write ‘PORTX=PORTY’ it means make the output of the port ‘X’ equals to the output of the port ‘Y’, and not the input.

Desired Output:

If a button is pressed, LED corresponding to that button will light up.

Hex Files: 

IP Handling Atmega32.Rar  and  IP Handling Atmega8.Rar

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

Leave a Comment

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