I take already discussed nigh a few chapters necessary to get into AVR programming. At present this is the start commodity that deals with programming.  Permit us commencement with the basics.

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

  • Information Direction Annals– This annals determines which of the pins will deed 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 permit usa look into an example. For Port A the three registers are- DDRA, PORTA & PINA respectively. Similarly for port B, it volition be- DDRB, PORTB & PINB.

Now before going to further discussion, in that location is  one of import matter I should mention. I port serves for more than one purpose. It is the designer'southward responsibleness to eliminate every possible error. If the program and organisation are non designed carefully, fallacy may announced.

Excursion Diagrams using Atmega32 and Atmega8

Notation: Click on the excursion diagram to get a large and clear view.

Circuit Diagram using Atmega32

And detect beneath the circuit using Atmega8

Circuit using Atmega8

Glow an LED using Avr Microcontroller

Program ane: Bones Programme Showing Digital Output functionality.

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

{ DDRB=0x0f;

PORTB=0x00;

while(1)

{ _delay_ms(1500);

PORTB =~PORTB;
}

return 0;
}

Description:

This plan consists of two unproblematic statements, including one delay loop. The '~' symbol stands for bitwise not operation. Hither CPU operating Frequency must be divers for proper implementation of the filibuster loop, and it has to be declared before the 'filibuster.h'  line in the C code. Hither I've used the #define F_CPU  1000000.

At that place is another option bachelor to define the CPU Operating frequency. In Avr Studio, go to 'Project Menu> Configuration Options> Full general'. Here you lot tin can cull your device model and its operating frequency.

 As I've previously discussed, The Data direction annals must be gear up up before doing whatever input/output functioning.

Desired Output:

The LEDs connected to Port B blinks with 3 second fourth dimension flow.

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 equally such – you may download each  file and EXTRACT information technology to your computer using Winzip or Winrar.

OP Handling Atmega32.Rar andOP Treatment Atmega8.Rar

Glow an LED using a Push switch with Avr

Program two: Basic Digital Input functionality.

#include

int principal()

{ DDRB=0x0f;

DDRD=0x00;

while(1)

PORTB =~PIND;

return 0;

}

Description:

Earlier, 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 utilise 'PINX'. Where 'Ten' 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 take different addresses. If you lot write 'PORTX=PORTY' information technology 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 (four Curlicue) Driving Sequence.

#include

#define F_CPU one thousand thousand

#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%four; // PORTB=ch[(i++)%four];

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

}

render 0;

}

Clarification:

Stepper motor has precision position and speed command capability. For DC stepper motors, the field coils are supplied directly from the ability supply. The current through the armatures are controlled by the micro controller with the assistance 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 equally follows:

Coil→

A

B

C

D

Hex Code

Step 1

0

0

0

1

0x01

Step 2

0

0

1

0

0x02

Step3

0

i

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 plan, 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 lath will be sequentially turned on and will echo the sequence. Each LED would stay turned on for 1 second. Later on that, LED adjacent to it will plow on up, and so on. The first LED turns on after the last LED.

Hex Files:

 Stepper Atmega32.Rar andStepper Atmega8.Rar

Read a Cardinal printing (Key debouncing) using Avr

Program 4: Primal input with Debounce.

#include

#ascertain F_CPU million

#include

int main()

{ char ifCondition;

char chkValidity;

DDRB=0x0f;

DDRD=0x00;

while(1)

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

if(ifCondition)

{ _delay_ms(100); // Look for some time and then any transient

// input voltage lapses from input

chkValidity =0x0f&(~PIND); // Read the pivot data over 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-billowy means taking a clean input from a key/button by eliminating faux signals generated by electrical dissonance and transient electric signals. Ane of the most pop methods of debouncing technique is to recheck the fundamental later few moments.

In the plan above, later on taking an input, it is checked if any primal is pressed! If pressed, subsequently 100mS, the keys are read again. If both of them are equal in value, the LEDs, corresponding to the cardinal(southward) pressed, are turned on.

Desired Output:

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

Hex Files:

Debounce Atmega32.Rar   andDebounce Atmega8.Rar

Further Reference

You can use these ii files (PDF) for farther reference in Treatment 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