You are on page 1of 7

Why avr?

Speed and ease of use. I prefer AVRs because one major reason. They are fast. When a PIC and an AVR is running with same frequency lets say 12 MHz, then the AVR is actually executing four times faster than the PIC ! Yes 4 times faster.
In addition, I like the AVR architecture because of its consistency. It makes using the most advanced feature of AVR very easy to use. These chips are easily available and they are cheap

Free C compiler. One more important thing, generally microcontroller programs are written in assembly language for efficiency. Which you may know is a very low level and unstructured language. Therefore, to achieve a small thing lots of code need to be written and the programmer cannot concentrate on program logic. This makes the things harder. However, mid to high end AVR mcus are powerful enough to support high level language such as C efficiently. To write programs in c for AVR or any other microcontroller we need a c compiler for that MCU. Generally these compilers are priced so high that they are out of reach of hobbyist and small company. But fortunately for AVR mcus there is a very high quality c compiler for free. It is GNU C compiler. It is extremely popular and it has a large user base. It is part of the free and open source software revolution that we are witnessing (like Linux, PHP, Apache, OpenOffice.org etc). Providing excellent software at no cost. They are the dedicated work of programmers around the globe. Free C compiler. One more important thing, generally microcontroller programs are written in assembly language for efficiency. Which you may know is a very low level and unstructured language. Therefore, to achieve a small thing lots of code need to be written and the programmer cannot concentrate on program logic. This makes the things harder. However, mid to high end AVR mcus are powerful enough to support high level language such as C efficiently. To write programs in c for AVR or any other microcontroller we need a c compiler for that MCU. Generally these compilers are priced so high that they are out of reach of hobbyist and small company. But fortunately for AVR mcus there is a very high quality c compiler for free. It is GNU C compiler. It is extremely popular and it has a large user base. It is part of the free and open source software revolution that we are witnessing (like Linux, PHP, Apache, OpenOffice.org etc). Providing excellent software at no cost. They are the dedicated work of programmers around the globe.
y y

AT MEGA16 [ 40 PIN, 16K Flash, 1KB Ram, 512 Byte EEPROM] AT MEGA32 [ 40 PIN, 32K Flash, 2KB Ram, 1KB EEPROM]

You can choose any one of them according to your need.

Features like
y y

Timers with PWM. Serial Communication

1. USART for connection with PC and other micros 2. SPI can be used for connection with other chips like DataFlash, Graphical LCD, MemoryCards. 3. TWI

MCU based development process

Therefore, the tools you need are Hardware


y

A PC running Win98 or Better.

An In-System programmer (ISP): This is the device which connects your mcu to your PC .You can easily make a serial or parallel port programmer. You can also make a USB programmer (little complicated) or buy one (see our shop !). A Target Board: It is nothing but you project which has an ISP header so that programmer can be connected and detached easily. In most simple case, it has a MCU with its power supply, a few basic connections, and ISP header. Last but not the least an AVR MCU.

Software
y

y y y y

A C compiler (free)This will compile your program which is written in a high-level language to machine language. After successful compilation and build you will get a file with .hex which you have to burn into the MCUs flash A programmer software free (free)This software will help you burn the above mentioned hex file to mcu flash.That is all for now. AVR Studio A GUI IDE for AVR(www.atmel.com, http://www.atmel.com/dyn/resources/prod_documents/AVRStudio4.13SP2.exe) WinAVR a free C compiler for AVRs (http://winavr.sourceforge.net/download.html) PonyProg Serial Device Programmer (www.lancos.com)

y y y y y y y y y y y y y y y y y y y y y y y y y y

#include <avr/io.h> #include <util/delay.h> void Wait() { uint8_t i=0; for(;i<23;i++) _delay_loop_2(0); } void main() { //Set PORTC0 as output DDRC=0b00000001; while(1) { //Set PC0=High(+5v) PORTC|=0b00000001; Wait(); //Set PC0=Low(GND) PORTC&=0b11111110; Wait(); } }

y if(PIND & 0b00000010) { //Switch is not pressed ... } else { //Switch pressed ... } ... Internal peripherals.

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

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

What is a timer ?
A timer in simplest term is a register. Timers generally have a resolution of 8 or 16 Bits. So a 8 bit timer is 8Bits wide so capable of holding value withing 0-255. But this register has a magical property ! Its value

increases/decreases automatically at a predefined rate (supplied by user). This is the timer clock. And this operation does not need CPU s intervention.

Fig.: Basic Operation Of a Timer. Since Timer works independently of CPU it can be used to measure time accurately. Timer upon certain conditions take some action automatically or inform CPU. One of the basic condition is the situation when timer OVERFLOWS i.e. its counted upto its maximum value (255 for 8 BIT timers) and rolled back to 0. In this situation timer can issue an interrupt and you must write an Interrupt Service Routine (ISR) to handle the event.

Fig.: Basic Operation Of a Timer.

Using The 8 BIT Timer (TIMER0)


The ATmega16 and ATmega32 has three different timers of which the simplest is TIMER0. Its resolution is 8 BIT i.e. it can count from 0 to 255. Note: Please read the Internal Peripherals of AVRs to have the basic knowledge of techniques used for using the OnChip peripherals(Like timer !) The Prescaler The Prescaler is a mechanism for generating clock for timer by the CPU clock. As you know that CPU has a clock source such as a external crystal of internal oscillator. Normally these have the frequency like 1 MHz,8 MHz, 12 MHz or 16MHz(MAX). The Prescaler is used to divide this clock frequency and produce a clock for TIMER. The Prescaler can be used to get the following clock for timer. No Clock (Timer Stop).

No Prescaling (Clock = FCPU) FCPU/8 FCPU/64 FCPU/256 FCPU/1024 Timer can also be externally clocked but I am leaving it for now for simplicity.

TIMER0 Registers.
As you may be knowing from the article Internal Peripherals of AVRs every peripheral is connected with CPU from a set of registers used to communicate with it. The registers of TIMERs are given below. TCCR0 Timer Counter Control Register. This will be used to configure the timer.

Fig.: TCCR0 - Timer Counter Control Register 0

As you can see there are 8 Bits in this register each used for certain purpose. For this tutorial I will only focus on the last three bits CS02 CS01 CS00 They are the CLOCK SELECT bits. They are used to set up the Prescaler for timer.

TCNT0 Timer Counter 0

Timer Interrup Mask Register TIMSK

This register is used to activate/deactivate interrupts related with timers. This register controls the interrupts of all the three timers. The last two bits (BIT 1 and BIT 0) Controls the interrupts of TIMER0. TIMER0 has two interrupts but in this article I will tell you only about one(second one for next tutorial). TOIE0 : This bit when set to 1 enables the OVERFLOW interrupt. Now time for some practical codes !!! We will set up timer to at a Prescaler of 1024 and our FCPU is 16MHz. We will increment a variable count at every interrupt(OVERFLOW) if count reaches 61 we will toggle PORTC0 which is connected to LED and reset count= 0. Clock input of TIMER0 = 16MHz/1024 = 15625 Hz Frequency of Overflow = 15625 /256 = 61.0352 Hz if we increment a variable count every Overflow when count reach 61 approx one second has elapse.
y