AVR Basics

Dear readers, please note that this is the old website of maxEmbedded. The articles are now no longer supported, updated and maintained. Please visit the new website here and search for this post. Alternatively, you can remove .wordpress from the address bar to reach the new location.

Example: If the website address is http://maxEmbedded.wordpress.com/contact/, then removing .wordpress from it will become http://maxEmbedded.com/contact/.

We apologize for the inconvenience. We just want to give you a better viewing and learning experience! Thanks!

Hello everyone! From now on, we will be working with AVR microcontrollers. I hope you have an AVR MCU development board and an ISP with you. And I also assume that you have the elementary concepts of a MCU. If not, then I suggest you to go through the following posts:

Now that you have a development board and an ISP with you, I also hope that you have a tested AVR MCU in that. This is because new AVR MCUs need to be initialised. In technical terms, we need to set the fuse bits, or else it won’t work. We will discuss this thing later as it requires some skill which you will be getting now. For now, let’s get into the basics of AVR.


A datasheet of any electronic item/component/device is a document in which the manufacturer gives information regarding the product to its users. The data given in it is in detail. All the features, technical specs, design, register Summary, expected usage, troubleshooting, pin details, etc, everything is given in detail. It is the best source of info for that particular device!! All electronic components have a datasheet published by the manufacturers. You can Google them out!! They are free. Check out the datasheet of the following AVR MCUs:

Pin Configuration

The pin configurations of an ATMEGA8 and ATMEGA16/32 MCU are shown below.

ATMEGA8 Pin Configuration

ATMEGA8 Pin Configuration

ATMEGA16/32 Pin Configuration

ATMEGA16/32 Pin Configuration

Please note that we will be dealing with DIP packages. For more information of types of IC Packages, visit this site.

Now, let’s analyze the pin configuration of ATMEGA16/32. First of all, note that the 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.

Hey, don’t get scared after looking at the pin configurations. The MCU has 40 pins. It’s not that complicated. Here, let me simplify it as follows.

ATMEGA32 Pin Configuration Simplified

ATMEGA32 Pin Configuration Simplified

So here, as you can see, I have split the pins into four major parts. Remember about ports? I said that ports contain the pins of a MCU. ATMEGA32 has four ports, PORTA, PORTB, PORTC and PORTD, each one having 8 pins. Don’t worry about the other details, we will discuss them as and when needed. Right now, concentrate on PA0…PA7, PB0…PB7, PC0…PC7 and PD0…PD7. These are the four ports that make up the GPIO pins (General Purpose Input Output). These concepts are discusses in my previous post, Basics of Microcontroller.

So, how many pins over? 4 × 8 = 32. How many remain? 40 – 32 = 8! The remaining 8 are mostly consumed by supply pins (VCC, AVCC), ground (GND), reset, XTAL pins, and other minor stuffs. So, all the 40 pins over, isn’t it?😉

P.S. Every MCU has an internal oscillator which determines its frequency of oscillator. But we need not stick to it. We can connect an external crystal oscillator to generate higher frequencies and clock pulses. This external oscillator is connected across the XTAL pins (XTAL1 and XTAL2).

AVR Peripherals

Okay, now have a look again at the pin configuration of ATMEGA32. Have a look at all the GPIO pins of all the ports. Can you see some things written in the brackets? Like PA0 (ADC0), PB5 (MOSI), PC2 (TCK), PD1 (TXD), etc. Well, these are the extra features that the MCU can offer you apart from GPIO. In other words, these pins show dual behavior. If nothing is specified, they act as GPIO pins. The secondary features of these pins become active only if you enable certain bits of some registers. These are called peripherals. There are several peripherals that AVR offers in ATMEGA32, some are as follows:

  • ADC – Analog to Digital Converter – usually 10-12 bit
  • Timers – 8 bit and 16 bit timers
  • JTAG – Joint Test Action Group
  • TWI – Two Wire Interface (or) I2C – Inter-Integrated Circuit
  • USART – Universal Synchronous Asynchronous Receiver Transmitter
  • UART – Universal Asynchronous Receiver Transmitter
  • SPI – Serial Peripheral Interface
  • WDT– Watchdog Timer …and many more!

    SPI Communication using UART

    SPI Communication using UART

You can get a complete list from the datasheet. Let me give you brief idea regarding them. Detailed information will be given in later posts. For now, the following is enough.

ADC stands for Analog to Digital Conversion. The term is self-explaining. This feature converts Analog signals into Digital signals.

Timers are something which are a consequence of clock frequency. We can manipulate the clock pulses in order to generate timers of required resolution.

JTAG corresponds to testing of the circuit. When we make a circuit and fix the MCU onto it, we use JTAG to verify whether our connection, soldering, circuit design, etc is correct or not.

TWI/I2C (its actually I-square-C) is a revolutionary technology by Philips, in which two devices are connected and communicate by using two wires! USART/UART is related to serial communication in which the MCU sends and receives signals from another device based on serial protocol. SPI is something which helps to interface I2C, UART, etc. The adjoining figure showing SPI Communication using UART is just for representation purpose.

WDT (Watch Dog Timer) is something interesting it seems. The name is also quite interesting. Just like a watchdog always keeps an eye on it’s master to protect him from any harm, WDT keeps an eye on the execution of the code to protect the MCU from any harm. A WDT is a computer hardware or software timer that triggers a system reset or other corrective action if the main program, due to some fault condition, such as a hang, neglects to regularly service the watchdog (writing a “service pulse” to it, also referred to as “kicking the dog”, “petting the dog”, “feeding the watchdog” or “waking the watchdog”). The intention is to bring the system back from the unresponsive state into normal operation.

That’s all for this post folks! In the next post, we will discuss how to read and write data using the GPIO pins of the AVR. And don’t forget to post comments here!🙂

25 responses to “AVR Basics

  1. Pingback: Port Operations in AVR « maxEmbedded·

  2. Pingback: AVR Timers – TIMER0 « maxEmbedded·

  3. hello, i have a questios, how I can assign “alias” to a specific pin?, for example, portc.0 is called pulse_1

  4. Hey bro!!! Nice work!!! I have one question how can i diable the JTAG of atmega16 to used the PORTC as an I/O!!! Im using PONGPROG2000, i do disable it from the security option but still it doesnt work!!!
    Plus i have also tried this statment:
    MCUCSR|= (1<<JTD);
    MCUCSR|= (1<<JTD);
    in my programm but still doesnt work!!!

    • Burn the following fuse bits
      LOW Fuse = 0xFF
      HIGH Fuse = 0xC9

      This will disable JTAG on PORTC and enable the external oscillator connected across the XTAL pins.
      BUT BEWARE! Burning wrong fuse bits will render your microcontroller useless.
      Visit my post “Using AVR Studio 5” wherein I have described how to use the AVRDude GUI. You can program the fuses using that.

  5. Pingback: Using AVR Studio 5 « maxEmbedded·

  6. Pingback: Using Atmel Studio 6 « maxEmbedded·

    • Hi Reachkk
      You can check this out from the datasheet of that microcontroller. In the datasheet, you will see the pin configuration drawn.

  7. your posts are really beneficial for the beginners ..i would like to know the code for the generation of fast pwm signal,how it works??could u pleas help me

We'd love to hear from you!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s