Handling the Digital Input Output in AVR Micro Controllers

Mathew Kevin July 10, 2017 9 Comments

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

Circuit Diagram using Atmega32

And find below the circuit using Atmega8

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.

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.

OP Handling Atmega32.Rar      and   OP Handling Atmega8.Rar

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

Generate Stepper Motor Driving Sequence using Avr

Program 3: Stepper Motor (4 Coil) Driving Sequence.

#include

#define F_CPU 1000000

#include

int main()

{ char ch[]= {0x01,0x02,0x04,0x08};

int i=0;

DDRB=0x0f;

while(1)

{ PORTB =ch[i]; // These three lines of code

i++; // is equivalent to

i= i%4; // PORTB=ch[(i++)%4];

_delay_ms(1000); // Delay loop of 1 Second

}

return 0;

}

Description:

Stepper motor has precision position and speed control capability. For DC stepper motors, the field coils are supplied directly from the power supply. The current through the armatures are controlled by the micro controller with the help of transistors. Now to make the armature rotate properly, there is some sequence to be maintained while energizing the coils. Say a motor has coils A,B,C & D. One solution to the driving sequences is as follows:

Coil→

A

B

C

D

Hex Code

Step 1

0

0

0

1

0x01

Step 2

0

0

1

0

0x02

Step3

0

1

0

0

0x04

Step 4

1

0

0

0

0x08

So if this sequence is generated at a port, a stepper motor can be driven using that port.

In my program, an array of characters are declared first, and they are put into a port sequentially and repetitively to drive a stepper motor. A delay loop is also used. It helps to control the speed.

Desired Output:

The LEDs on the board will be sequentially turned on and will repeat the sequence. Each LED would stay turned on for 1 second. After that, LED next to it will turn on up, and so on. The first LED turns on after the last LED.

Hex Files: 

 Stepper Atmega32.Rar  and  Stepper Atmega8.Rar

Read a Key press (Key debouncing) using Avr

Program 4: Key input with Debounce.

#include

#define F_CPU 1000000

#include

int main()

{ char ifCondition;

char chkValidity;

DDRB=0x0f;

DDRD=0x00;

while(1)

{ ifCondition = 0x0f&(~PIND); //check input pins

if(ifCondition)

{ _delay_ms(100); // Wait for some time so any transient

// input voltage lapses from input

chkValidity =0x0f&(~PIND); // Read the pin data again

if(ifCondition==chkValidity)// Check if the values red

// before and after is equal

{ PORTB=chkValidity;// Put new input value to the

// output after debounce

}

}

}

return 0;

}

 Description:

Key de-bouncing means taking a clean input from a key/button by eliminating false signals generated by electrical noise and transient electric signals. One of the most popular methods of debouncing technique is to recheck the key after few moments.

   In the program above, after taking an input, it is checked if any key is pressed! If pressed, after 100mS, the keys are read again. If both of them are equal in value, the LEDs, corresponding to the key(s) pressed, are turned on.

Desired Output:

Initially, all the LEDs will stay off. If any key is pressed, Corresponding key will glow. If new key is pressed, the LED, corresponding to that key/button will glow and the previously on LED will go off. Multiple keys can also be pressed; in this case, multiple LEDs will glow.

Hex Files: 

Debounce Atmega32.Rar   and Debounce Atmega8.Rar 

Further Reference

You can use these two files (PDF) for further reference in Handling the Digital Input-Output in Atmega32 and Atmega8.

1. How to Handle DIGITAL ports in ATmega32.pdf

2. How to Handle DIGITAL ports in ATmega8.pdf

Comments
  • Kshitij
    June 11, 2017

    How do I control two LED’s with one push button , there’s always some error .

  • M.S.S.RAMA KRISHNA
    September 3, 2014

    pls anyone kindly mail me the c-code for atmega8 with usbasp programmer

  • abalfazl
    May 28, 2014

    i have not seen such a good article at web

  • Rakesh
    July 9, 2012

    The crystal and the capacitors together make a Colpitts tank circuit for frequency generation.

  • justi
    April 16, 2012

    sir what is the use of 22uf capacitor below crystal.

  • Rakesh
    March 7, 2012

    🙂 It was nice to see all of you. I’d be glad if you benefit from my articles.

  • John Obonyo
    March 7, 2012

    hello

  • bassam haddad
    March 7, 2012

    hello

  • bassam haddad
    March 7, 2012

    iam pleased to be one of your friend

Leave a Reply

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