Avr Turorial


Avr Studio is IDE is used for programming Avr and Arm two different architectures. Atmel has been manufacturing a lot of ARM Cortex-M and wanted to give its users seamless and easy-to-use environment to write, build and debug their applications written in C/C++ or assembly code. the introduction and creating a new project is describe in this Video.


Before going to programming part we should learn about Pin configuration of microcontrollers. First thing is that we need to know that which microcontroller we used. in this tutorial we will use Atmega 8, Atmega 16, Atmega 32, Atmega 168, Atmega 328.  below there is pin map of   Atmega 16 and Atmega 32 microcontroller. both microntroller are 8 bit microcontroller with 40 pin configuration.


 As we can see from PinMap image there are 4 Ports named asPortA, PortB, PortC and PortD. The Datasheet of Atmega16_32


Note:-  ATMEGA16 and ATMEGA32 MCUs are completely similar, except the fact that ATMEGA16 has 16KB Flash, 1KB RAM and 512B EEPROM whereas ATMEGA32 has 32KB Flash, 2KB RAM and 1KB EEPROM.

Each port consist of 8 Pin 0-7. these are 8 digital Input and output pins.with this port A can also be used as analog to digital convertors pinsnamed as ADC0 – ADC7.

There are Three registers DDRx , PINx and PORTx that are important for digital input / Output operation of AVR microcontrllers.

Selecting the direction of pin:-  DDRx bit  selects the direction of this pin.

  • DDRx =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).
  • DDRx=0, Portx nth is configured as an input pin.


The ‘x’ in DDRx is just for representation. It is replaced by the corresponding port i.e. x = A, B, C, D. Say for the example shown in the diagram above.

DDRA = (1<<DDA2) | (1<<DDA4) | (1<<DDA5);

Same as  DDRA = 0b00110100;

Now we had done with declaration of Port A pin 2, 4 and 5 as output.

PORTx Register -: This port determine the status of output pins of Port x. if the pin x of a port is defined as output pin than this register tell the value of that pin.PIN

this register declare same as DDR

PORTA = (1 << PA1) | (1 << PA3) | (1 << PA6) ;

PORTA = (1 << 1) | (1 << 3) | (1 << 6) ;

PORTA = 0b01001010;

PINx Register: this register is used for reading data from microcontroller pins.



Note : By default all pins of microcontroller declare as input pins.

DDRC = 0b01001000;

PINC = 0b10110111;

Sample code :

#include <avr/io.h>                                      // Includes all the definition of register port etc  #ifndef F_CPU                                                                                                                  #define F_CPU 16000000UL      //Need to include it before <util/delay.h>                            #endif                              //Change 16000000 with your crystal freq. In my case its16 MHz  #include <util/delay.h>    //includes delay functions delay_ms and delay_us                      int main(void)                                                                                                                           {                                                                                                                                   DDRD|= 0xFF; //Configured Port D as Output                                                                    while (1)                                                                                                                                    {                                                                                                                                                 PORTD |= 0xFF; //Output logic 1 on all the pins of Port D

_delay_ms(1000);//1 sec delay

PORTD &= 0×00; //Output logic 0 on all the pins of Port D

_delay_ms(1000); //1 sec delay




Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>