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

Good introduction.
August 9th, 2008 at 9:26 amPlease try to include some example codes too (preferably in WinAVR C) to make it useful!
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.
August 9th, 2008 at 1:45 pmThe 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.
September 16th, 2008 at 4:45 pmHi Atiya,
Thanx
Many tutorials comming up !
Please do post comments and share your thoughts. This makes me improve the site
ThanX again
September 17th, 2008 at 4:54 am[...] Read the Tutorial “Internal Peripherals of AVR” before using ADC of [...]
September 18th, 2008 at 12:35 pm[...] read the “Internal Peripherals of AVRs” to have the basic knowledge of techniques used for using the OnChip peripherals(Like timer [...]
September 18th, 2008 at 1:32 pm[...] For learning about the basic idea of peripherals and their use with AVRs please see this tutorial. [...]
October 24th, 2008 at 6:25 pm[...] 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 [...]
December 12th, 2008 at 7:00 pmHello 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!!!!
January 14th, 2009 at 7:39 am@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
January 14th, 2009 at 10:48 ami 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 !!!
June 13th, 2009 at 11:24 pmsimple, 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
March 6th, 2010 at 10:42 am