You are on page 1of 26

Input Output Operations using AVR

Submitted by bOtskOOl on Sun, 06/21/2009 - 04:25

This is the very first tutorial of the AVR (Atmega-16/32) tutorial series. Note that in this
and subsequent tutorials most of the examples will be based on Atmega32, though they
can be implemented on Atmega16 too without any major changes. If and when some
changes are required to be made it will be mentioned. So let’s begin, first of all we will
be discussing AVR studio which is an editor, assembler, compiler, simulator and much
more. In this tutorial we will discuss the coding part in assembly language. Many people
run away from assembly language programming but when you start learning
microcontrollers properly and thoroughly and write your own programs the assembly
gives much easier ability to think, anaylse and apply the logic to make your programs and
you can directly take advantage of microcontroller’s architecture while writing your
program. If you want you can shift to C very easily after that, it is just a step away after
you have learnt assembly. We shall be launching AVR C tutorial series in a few days.

Atmega-16/32 Pin Description

This is the basic pin description diagram. There are a lot of labels marked in the figure
above but it isn’t that difficult. The alternate functions of the pins are marked in bracket.

(This may not be exactly same for the ATmega16-32, but we will not be discussing the
alternative functions in this tutorial.)
Physical Ports of Atmega-16/32

The Atmega-16/32 has four physical ports on the chip as marked in the figure above.
These ports are named as PortA, PortB, PortC and PortD. Each port is basically a
collection of 8 pins grouped together.
Basic Circuit of AVR

The pins marked ‘Gnd’ are to be grounded, ‘Vcc & AVcc’ are to be given 5V. The reset
pin is also high but we usually prefer to put a switch at this point for the reset of the
chips. If the switch is pressed for a minimum pulse of time then it will reset the chip.

Note: ‘AVcc’ should be connected to 5V supply through a capacitor when using PORTA
pins as ADC, though in simple applications capacitor is not necessary.

This circuit is used whenever we wish to do anything with the µcontroller. In addition to
this you will have to add other hardware required depending upon what do you want to
Now for the given tutorial we will be using the PORTB for connections. We will connect
8Leds at each pin of PORTB.

The above picture depicts how each LED will be connected to corresponding pin of
portB of Atmega32. So let’s see the complete circuit…
Complete Circuit Diagram

Photograph of switch

The switch is required so that we are able to ‘reset’ i.e. restart the microcontroller at any
time if required.

People who have worked on some other microcontroller must be familiar with word

In short, registers are used to store information temporarily. That information can be of
one byte (or two) of data to be processed or an address pointing to data to be fetched.

The AVR has 32 general purpose registers and many other registers also. There are three
registers associated with each port that are required for the Input-Output operation for
AVR. These registers are DDRx register, PORTx register, PINx register where x can be
(A, B, C, D). These register are very important and are used for reading and writing to the

Note: The physical ports and the register PORTx has same name but the user must not
confuse between them, from now on while discussing register PORTx it will be referred
as ‘PORTx Register’ to avoid confusion.

These registers will be discussed later on in detail but before that let’s write our first
program and try to understand it.
Download the AVR Studio from Atmel and install it. We will be showing you how to
start working directly with the software. It is an environment where can write your
programs, build corresponding hex codes, do debugging and much more!

You will be also needing the datasheet of Atmega16 or Atmega32.

Using the datasheet

If you are beginner do not go through the datasheet now as you will find it quite difficult
to understand. Luckily you are not supposed to do that, either. The datasheets are
complete technical documents that you should use as a reference when you are in doubt
how a given peripheral or feature works.

We will be covering the necessary theories and relevant concepts as we move on and in
no time you will have confidence to write your own programs.

After you have downloded and installed ‘AVR Studio’, just open the it and follow the



When you will launch the AVR Studio a splash screen will appear as shown below. In
case it doesn’t choose ‘New Project’ option from the ‘Project’ menu.

Choose ‘New Project’ as shown above.


• Choose ‘Atmel AVR Assembler’ option in ‘Project type:’

• Write the ‘Project name’ (It doesn’t accept a space in name).
• Choose a suitable ‘Location’ & hit ‘NEXT>>’.

• Choose the debug platform in this case – ‘AVR Simulator’.

• Choose the device (Here we have chosen Atmega32 for our project) & click on

Now we can start our first project and understand the basics of I/O (Input/Output)

We will learn how the value of the I/O registers changes. AVR Studio has a fantastic tool
for showing step by step execution. Let’s get started!
As shown in the figure above

• Region 1 – Write your program here

• Region 2 – Menu bars (obviously you know this )

• Region 3 – You know what it is yes toolbar section

• Region 4 – I/O View
• Region 5 – Project directory and distribution
• Region 6 – Name of the microcontroller which is currently in use

When you will write your code you will notice that the instructions, comments, etc will
be written in different colors. This is a very important feature of an ‘editor’. This
remarkably helps in distinguishing between types of syntaxes and lines.
Below is a screenshot of our very first AVR code.

Let’s understand the program before going further:

Below there is a line by line explanation of the whole code.
There are three registers associated with Input/ Output Ports in AVR.

Selecting the direction of pin:- DDxn bit (in the DDRx Register) selects the direction
of this pin.

• DDxn =1, Portx nth pin is configured as an output pin (since there are eight pins
in all in a particular port so ‘n’ can be any number between 0-7).
• DDxn=0, Portx nth is configured as an input pin.

Activating the pull resistors:-

• PORTxn= 1, the pull up resistors are activated (while the nth pin is configured as
input pin)
• PORTxn= 0, the pull up resistors are deactivated (for nth pin).

Inputs of the AVR are generally in Hi-Z state. This makes them prone to catching noise
and picking up false signals. So it is advisable to activate the pull up resistor to reduce
Status of pin when configured as an output pin:-

• PORTxn= 0, the output is driven high.

• PORTxn= 1, the portx nth pin is driven low (zero).

PINx Register:-

To put it bluntly, the PINx register contains the status of all the pins in that port.

• If the pin is an input pin, then its corresponding bit will mimic the logic level
given to it.
• If it is an output pin, its bit will contain the data that has been previously output
on that pin. (The value of an output pin is latched to PINxn bit, you can observe it
when we do step by step execution in AVR-Studio shown below)

Click on ‘Build and Run’ option present in ‘Build’ menu bar.

We can observe the values of registers, Program counter, Cycle Counter, Port registers
and much more after execution of each instruction!

We can do step by step execution in AVR Studio by pressing F11 after code has been
successfully built and is running… Let’s do it.
• Region 5 - This confirms that are program has been successfully completed,
• Region 1 - This mark indicates the instruction to be executed in RAM.
• Region 2 - This indicates location of Flash memory (ROM), the program are
burned in EEPROM and executed in RAM. (The beginners may find this concept
little confusing but don’t worry for now just move on, we shall discuss it some
later on).
• Region 3 - You can look at the current values of register by pressing the (+).
• Region 4 - You can also get the values of the register associated with the

Now press F11 once, the arrow jumps to the next position as shown..
• Note the value of R16 by scrolling down, you will find it zero.
• Clock Cycle is shown in blue colour on left hand side. This indicates the number
of clock cycles after the program started executing. An instruction can take one or
two clock cycles.

On Pressing F11 again you will observe this…

And Pressing F11 again...

• The PORTB is set as an output… (the register DDRB is set)

Pressing F11 again we can see that…

• The PORTB register is loaded with 255, this output to the pins of PORTB (1111
1111b) and all the LEDs start glowing…

If we Press F11 again we will observe this…

• The PINx register is latched with the value which was the output before this. This
proves the fact that the output values from pins are latched to register bits PINxn
after one cycles…

Keep on playing with this by pressing F11 again and again & observe things till you
understand everything properly and are completely satisfied.

Burning the ROM of microcontroller

To get the hex file (that you will need to burn to your microcontroller) just open the
folder where you have saved your project.

In this case it is - D:\testprog\1

You can buy a board to burn the AVR or build your own ISP, this topic will be covered
in next tutorial and by that time you can check out this tutorial - Parallel Port Programmer
for ATMEGA 16/32 - by one of our users - heloitsadi.
Parallel Port Programmer for ATMEGA
Submitted by heloitsadi on Sun, 06/07/2009 - 07:18

In-system programmer means we can design a programmer circuit using simple parallel
port interfacing such that our controller can be directly burned with the program while in
the designed system or circuit board.

Following pins of Atmega16 are used in programmer circuit-

• SCK – Port B, Pin 7

SCK: Master Clock output, Slave Clock input pin for SPI channel. When the SPI is
enabled as a Slave, this pin is configured as an input regardless of the setting of
DDB7.When the SPI is enabled as a Master; the data direction of this pin is controlled by
DDB7. When the pin is forced by the SPI to be an input, the pull-up can still be
controlled by the PORTB7 bit.

• MISO – Port B, Pin 6

MISO: Master Data input, Slave Data output pin for SPI channel. When the SPI is
enabled as a Master, this pin is configured as an input regardless of the setting of DDB6.
When the SPI is enabled as a Slave, the data direction of this pin is controlled by DDB6.
When the pin is forced by the SPI to be an input, the pull-up can still be controlled by the
PORTB6 bit.

• MOSI – Port B, Pin 5

MOSI: SPI Master Data output, Slave Data input for SPI channel. When the SPI is
enabled as a Slave, this pin is configured as an input regardless of the setting of DDB5.
When the SPI is enabled as a Master, the data direction of this pin is controlled by DDB5.
When the pin is forced by the SPI to be an input, the pull-up can still be controlled by the
PORTB5 bit.

• RESET-Pin 9

Reset Input. A low level on this pin for longer than the minimum pulse length will
generate a reset, even if the clock is not running.

• VCC -Pin 10

Digital supply voltage(+5V).

• GND-Pin-11


Parallel Port-

Parallel Port interfacing is the simplest method of interfacing. Parallel Port’s are
standardized under the IEEE 1284 standard first released in 1994. It has data transfer
speed up to 1Mbytes/sec. Parallel port is basically the 25 pin Female connector (DB-25)
in the back side of the computer (Printer Port). It has 17 input lines for input port and 12
pins for output port. Out of the 25 pins most pins are Ground and there is data register (8
bit), control register (4 bit) and status register (5 bit).
Figure-Pin diagram of Parallel Port

Following Pins are used in parallel port-

Interfacing -

In programmer circuit pins of parallel port which are above described has to interface
with pins of ATmega16 microcontroller which are responsible for in-system
programming. The parallel port can be interfaced directly with microcontroller. To avoid
reverse current we can use Schottkey diodes as safety precaution for pc motherboard.

Following pins of Parallel Port and ATmega16 are to be interfaced-

Figure-Circuit Diagram of ATmega16 Programmer


• WinAVR– WinAVR is open source package in which we use two sub-programs.

o Programmers Notepad
o Mfile.
• Version-
• Creator- Simon Steele
• Purpose-
o To write code.
o To compile coding.
o To generate Hex Code.
o To burn Hex code.

The next tutorial will make you aware with how to install and use WinAVR for
compiling and burning the program.