Jul-29th-2008

Using Internal peripherals of AVR MCUs.


Each AVR MCU has several internal peripherals that give powerful abilities to your projects. For example internal ADC can be used to convert analog value (say voltage output of some sensor) to a digital value that you can use. And a USART(Universal Synchronous Asynchronous Receiver Transmitter) can be used to connect your MCU with PC. (Example use - A PC serial mouse or A PC controlled Toy Rocket Launcher). This tutorial gives you overview of the process used to interface with these peripherals.

Internal peripherals.

Now you know the basics of peripherals, lets see what peripherals are available in ATmega16 MCU.

  • Two 8-bit Timer/Counters with Separate Prescalers and Compare Modes
  • One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture Mode
  • Real Time Counter with Separate Oscillator
  • Four PWM Channels
  • 8-channel, 10-bit ADC 8 Single-ended Channels 7 Differential Channels in TQFP Package Only 2 Differential Channels with Programmable Gain at 1x, 10x, or 200x
  • Byte-oriented Two-wire Serial Interface
  • Programmable Serial USART
  • Master/Slave SPI Serial Interface
  • Programmable Watchdog Timer with Separate On-chip Oscillator
  • On-chip Analog Comparator

Interfacing Technique.

using internal peripheral of avr mcu

Fig: Using internal peripherals of AVR MCUs.

A peripheral is connected to MCU by some special registers. The different registers of any peripherals can be logically of two types-

1)Data registers – which usually contains some data, say byte received from USART or data just read from internal EEPROM.

2)STATUS and CONTROL register – these are used to configure the peripheral according to your need, and to control them from your program. For example the ADMUX register has following bits.

Bit No.
7
6
5
4
3
2
1
0
Name REFS1 REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0
The ADC (analog to digital converter) has 8 input channels. Any one these can be selected by setting the MUX2-MUX0. To select ADC channel 0 set them to 000. Alternatively, to select channel 4 set them to 100 and so on.

Interrupts.

As we saw the you can use the STATUS registers to see the what the peripheral has to say to the CPU, like it has just completed the conversion Analog value to digital and the data is ready. But in this way you have to constantly poll the registers. But one more advance feature is INTERRUPTs. Where the peripheral halts the CPU whenever it has something interesting. Then the CPU will jumps to a function called ISR (interrupt service routine) which is provided by you. You can configure which events should interrupt the CPU and also provide the ISR.

Steps to use peripherals.

  • First setup the peripheral according to your need.
  • Set interrupts and ISRs (Optional)(beginners may skip for simplicity).
  • Command the peripheral or read / write data.

Download PDF version| Get Adobe Reader Free !!!

What's Next
We will use a very useful peripheral of AVR – The ADC (analog to digital converter).

That's for now. And don't forget to bookmark this page. Please drop in a comment if you like this or you have any doubt. Enjoy !!!


12 Responses to “Using Internal peripherals of AVR MCUs.”

  1. 1
    Abhijeet Says:

    Good introduction.
    Please try to include some example codes too (preferably in WinAVR C) to make it useful!

  2. 2
    Avinash Says:

    Hi Abhijeet,
    ThanX for you comments.

    This is just an introduction so that you have the overview when we jump start using the actual peripheral. This tutorial only provides the concepts that will help beginners. The later tutorials will have all the CODES !!!

    So don’t worry.

  3. 3
    atiya Says:

    The tutorial is so simple and so good.It makes things so easy for a beginner.Thanks a lot.keep postin.I’m eagerly waitin for the next post.

  4. 4
    Avinash Says:

    Hi Atiya,

    Thanx

    Many tutorials comming up !

    Please do post comments and share your thoughts. This makes me improve the site

    ThanX again

  5. 5
    Using the Analog To Digital Converter. | eXtreme Electronics Says:

    [...] Read the Tutorial “Internal Peripherals of AVR” before using ADC of [...]

  6. 6
    AVR Timers - An Introduction. | eXtreme Electronics Says:

    [...] read the “Internal Peripherals of AVRs” to have the basic knowledge of techniques used for using the OnChip peripherals(Like timer [...]

  7. 7
    Timers in Compare Mode - Part I | eXtreme Electronics Says:

    [...] For learning about the basic idea of peripherals and their use with AVRs please see this tutorial. [...]

  8. 8
    Using the USART of AVR Microcontrollers. | eXtreme Electronics Says:

    [...] is same as with any other internal peripheral (say ADC). So if you are new to this topic please see this tutorial, it shows you the basic idea of using [...]

  9. 9
    percy Says:

    Hello I have read all you have write about AVR, because I must program in Atmega32.
    You have help my very much.

    Can you recommend mi a book for learn AVR “Atmega 32” or a page in internet?

    Thanks for the help!!!!

  10. 10
    Avinash Says:

    @Percy
    Welcome!

    Best way to learn deeply about AVRs is their datasheets. Go through tutorial on my site and then when you have basic knowledge abt a topic go an explore the datasheets!

    I also program with ATmega32 and all example you get here applies directly to ATmega8 , ATmega16 and ATmega32. These chips are best to get started with AVRs

  11. 11
    Siddhartha Says:

    i m very impressed with your website .. d tutorials written y u r so simple .n can b understood anyone ..me myself being a beginner can tell u that u have done an amazing job coz making things simple for others is the tougest job !!! keep it up !!!

  12. 12
    Rohit Sharma Says:

    simple, easy to understand , short tutorials

    so these tutorials have all the qualities of good tutorial. you must write a book on avr fo beginners (hint: just combine the tutorials)

    :) liked them

Leave a Reply

Comments

    • Sinopteek: When i try to run this software under OpenSuSe 11.2 (x64), i’ll have next error:...
    • kapil: @Dhananjay I used that its working and it can programme 3 to 4 times after that there is a...
    • Dhananjay: Sir, At present I am using progisp to flash AT89SXX with usbasp(with modified firmware)....
    • Shashi Jain: plz dont fight wid each other…k i accept my fault… neway m not a rich person...
    • vivek: @Avinash “Not every one is as rich as you to purchase Code Vision Compiler. I can’t...
    • Niclas: PS. you need both (and i suppose its WIN-AVR that includes the gcc not AVR-studio) DS.
    • Niclas: and ofc WIN-AVR, google it and youll find it ^^

Video