Using Internal peripherals of AVR MCUs.

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

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

Avinash

Avinash Gupta is solely focused on free and high quality tutorial to make learning embedded system fun !

More Posts - Website

Follow Me:
FacebookLinkedInGoogle Plus

About Avinash

Avinash Gupta is solely focused on free and high quality tutorial to make learning embedded system fun !

35 thoughts on “Using Internal peripherals of AVR MCUs.

  • By Abhijeet - Reply

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

  • By Avinash - Reply

    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.

  • By atiya - Reply

    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.

  • By Avinash - Reply

    Hi Atiya,

    Thanx

    Many tutorials comming up !

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

    ThanX again

  • Pingback: Using the Analog To Digital Converter. | eXtreme Electronics

  • Pingback: AVR Timers - An Introduction. | eXtreme Electronics

  • Pingback: Timers in Compare Mode - Part I | eXtreme Electronics

  • Pingback: Using the USART of AVR Microcontrollers. | eXtreme Electronics

  • By percy - Reply

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

  • By Avinash - Reply

    @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

  • By Siddhartha - Reply

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

  • By Rohit Sharma - Reply

    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

  • By Muzaffar Baig - Reply

    This tutorial very simple and good for beginners. i think you include some programe example in assambly language because there are many people include me we are use assembly language.

  • By deepak gupta - Reply

    hello sir your portal is superb!
    here i am beginner in avr atmega32
    here got confused with term uint16_t,why we used or is this type we used for specific platform.
    so please guide me.
    thanks in advance

  • By Dewsworld - Reply

    I’m learning a lot from this page :) . Thanks…

    • By Gebreabzgi - Reply

      Thank for ur information,I have learn a lot from this but I need detail of free running mode since am beginner for Atmega32 avr.

      and I need it badly to do summer project by this controller..thank in advance

  • By avrbestever - Reply

    hello thax 4 the intro can you recommend any good books on the subject?

    • By Avinash - Reply

      @AvrBestever,

      If you wait for a book to come out, then you will find that chip in Museum.
      Tech world is fast and furious.

  • By kaya - Reply

    It’s great site for me to learn everything i want…thanks a lot for this effort..

  • By arulmani - Reply

    excellent tutorials ! i became a fan of you !! cheers !!! what will be the difference between the MCU’s.eg: difference between atmega88 and atmega32 etc bro?

  • By Divyansh Khunteta - Reply

    Hi…..
    nice work, i started working on AVR microcontrollers after reading ur tutorials and they made it easy…….can u upload a tutorial related to concepts of UART programming………..and I am always in doubt related to PIND register…can u elaborate it with some more examples…????

    Thanks

    • By Avinash - Reply

      @Divyansh Khunteta

      Hey man ! U need “UART Programming” concept ? Put “UART Programming avr” in google before asking me …

      Their my “elaboration” already done comes on the 1st rank !

      I never help anybody catch fish, rather I teach him/her how to catch a fish so he/she can catch it all their life time !

  • By Ankit - Reply

    @ Avinash,No doubt your work deserves appreciation, i’m following your website from a while i admire your work, i cant help but notice one thing constantly, why are you so cocky? i mean why do you give this attitude when some one(mostly newbies) ask you something which in most of the cases sounds “stupid” to you.
    May be i’ll be getting one of those replies but, i cant understand why would you do that, on the one hand you’ve formed this very informative site, you’ve included tutorials so i believe you want to help people who are new at this, than giving them such harsh replies that’s kind of an irony to me.

    • By Avinash - Reply

      @Ankit,
      Because those stupids need that only !

      • By shenawy -

        Avinash

        believe me when you deal with people with good ethics ,that’s better

        and remember God will not waste the reward of your work and helping people.:)
        So I wish GOD helps you in a good life for helping us dear :)

    • By Avinash - Reply

      @Shenawy,

      Ethics does not implies I have to please all the people in this world. Only the bird of same wing folk together. Ask my close people so know about my ethics.

      • By shenawy -

        :) u r honest man

      • By Avinash -

        @Shenawy,

        This world is and always will be divided ! Sometimes race and religion or some times good and bad !

        Not people are same, not all people follow same approach.

        I don’t talk with Those who have approach or solution different than me!

        They may be right or wrong I don’t know.

        Their is no absolute in this world except life and death. Only those to are common between all the being of this world, those are common between worst of enemies!

        Everything else is relative good, bad, brightness darkness everything.

      • By shenawy -

        u r right ,I agree with u :)
        and I wish 4 u and my self to be good,helpful,beloved people and a happy life in this world :)

  • By anto - Reply

    very usefull, thanks,

  • By anil - Reply

    really nice man,any one can understand this tutorial.thank u so much

  • By Sounak Das - Reply

    Thanks for the information
    you have written it easily

  • By Shuvamay - Reply

    Thanks for your tutorials. Please provide a detailed tutorial on FUSE BIT programming of AVR.

  • By Pari - Reply

    Thank you very much! Great help for a beginner…

  • By Sarothy - Reply

    Thanks! Since I am intended to learn µc by my own. It helped me a lot.

Leave a Reply

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


seven + 9 =

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>