Input Output Operations using AVR Studio

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 


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


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


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

Now for the given tutorial we will be using the PORTB for connections. We will connect 8Leds at each pin of PORTB. 


LED basic circuit


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


AVR 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 ‘Registers’. 

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

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 steps…




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.


Starting a new project in AVR Studio



Choose ‘New Project’ as shown above.




Choosing AVR Assembler


  • 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>>’.



AVR Simulator debug platform

  • Choose the debug platform in this case – ‘AVR Simulator’.
  • Choose the device (Here we have chosen Atmega32 for our project) & click on ‘FINISH’.


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

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!


AVR Studio


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.  


My first AVR Program


Let’s understand the program before going further:


First AVR Program Assembly

Below there is a line by line explanation of the whole code. 


Explanation of AVR code 


There are three registers associated with Input/ Output Ports in AVR.  


AVR Port Control Registers


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


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.


Building and Running AVR Program in AVR Studio


  • Region 5 - This confirms that are program has been successfully completed, Congratulation!!
  • 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 PORTB. 

Now press F11 once, the arrow jumps to the next position as shown..


AVR Program in AVR Studio


  • 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… 


AVR Simulator in AVR Studio


And Pressing F11 again...


Changing PORTB value upon execution of AVR code 


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

Pressing F11 again we can see that…


How different register values changes upon execution of AVR code


  • 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…


PortB values of AVR


  • 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


Hex File of AVR code in AVR Studio


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


Member since:
21 February 2009
Last activity:
9 years 23 weeks

Thank you.
More tutorials on AVR are coming up soon!  

I was searching for such an excellent tutorial. Everything has been explained to the extreme level of simplicity...
Great work dude...