Pages Menu
TwitterRssFacebook
Categories Menu

Posted by on Jun 9, 2011 in Atmel AVR, Microcontrollers | 36 comments

AVR Basics

AVR Basics

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.

Datasheet

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! :)

Max

Max is the founder and admin of maxEmbedded. He describes himself as an 'embedded electronics freak' and an Apple/Linux fan. He likes to experiment, learn and share new things in this field. Furthermore, he likes to write stuffs on his website for techie newbies and loves to teach others. In his spare time, you will find him with volunteering somewhere!

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle Plus

36 Comments

  1. Another great post…………

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

    • Simple. Use macros!
      #define portc.0 pulse_1

      To remove a macro defination, use undef
      #undef portc.0

  3. really good mayank…

  4. Really nice thanks for this great work!

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

      • i have burn timer1 code for led blinking with 2 sec delay but it is showing more than 2 sec delay. please help me. i have set low fuse bit=0xFF and high fuse=0xC9

        • Make sure that the F_CPU value you specified in the software is the same as that of the external crystal.

          • thanks sir for reply but i have specified correct F_CPU value but still delay is more than 2 sec

          • Then there’s something wrong with your code. Share your code with us using pastebin.com and we’ll try to look into it.

          • sir i am using your code given in timer1 post for led blinking in 2sec interval.

          • Which microcontroller are you using? And what is the frequency of your external crystal?

  6. i am starting to love this blog !

  7. How to determine which pins belong to which PORT?

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

  8. thats good stuff

  9. Hi, I don not know anything about AVR and I about to learn it.
    WHAT SHOULD I DO?

    • Hamed, I am really surprised by your question.
      THEN WHAT BRINGS YOU HERE?

  10. Hi, i like your style of explanation. Good!

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

  12. Dude, I love your style. I started with Arduino and now learning about programming AVR in C from your website.

    • Great! Let me know how it goes!

    • Aakash, Would you mind translating your comment to a language we understand i.e. English? Thanks!

  13. Hey,
    I though you are Indian, Well, Can you please provide detailed explanation of AVR Peripherals
    Cheers!

    • Doesn’t matter from where I am. This is a public forum and people from around the world should be able to read what you type.
      Anyways, did you read through this first? Let me know what are you facing trouble with. Thanks!

  14. All the information about of atmel avr very good thank you.
    Sorry if my grammer very but, because studying English language.

    • Thank you Nodir. Good luck with your English lessons! :)

Trackbacks/Pingbacks

  1. Port Operations in AVR « maxEmbedded - [...] port operations in AVR. Before going further, I suggest that you read my previous post regarding AVR Basics. The …
  2. AVR Timers – TIMER0 « maxEmbedded - [...] they can read about it from this page. To have an idea about AVR Peripherals, view this page (you …
  3. Using AVR Studio 5 « maxEmbedded - [...] AVR Basics [...]
  4. Using Atmel Studio 6 « maxEmbedded - [...] AVR Basics [...]

Post 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>