clock

Saturday 25 May 2013

Introduction to AVR Microcontrollers (Atmega8, Atmega16, Atmega32)

When we often try to learn a microcontroller, the first thing we learn is to write "hello world" no doubt about that. But this time I will teach you to set and clear a Port.

Why AVR when 8051 series microcontrollers are easy?
1. AVR has inbuilt 10-bit ADC, PWM, SPI, I2C and inbuilt EEPROM.
2. AVR is much faster since its RISC.
3. AVR has inbult Flash which has features of bootloader.
4. AVR is capable of being programmed onboard itself rather then removing (like in 89c51 etc).
5. Atmel Studio 6 is a freeware. Where as 8051 series microcontroller softwares are not all free.

Things we need to put in place first:
1. Install Atmel Studio 6.0
2. Download datasheet of any AVR microcontroller (Atmega16 in my case)
3. Buy a AVR programmer (usb will be great for latest PC)
4. Learning to write to a new program and making your first AVR controller programming successful.

First,
Install the Atmel Studio 6.0 properly. It is a freeware designed specially for Atmel microcontrollers.

Secondly,
Construct a circuit as below to keep the microcontroller ready to be Programmmed. This circuit below is the minimum requirements of the AVR microcontroller to start normally.


Components used:
1. Atmega16A
2. 12MHz Resonator
3. Tactile Switch for reset
4. 10k ohms (R1)
5. 10uF Electrolytic (C1)
6. 0.01uF Ceramic (C2)
7. 6 Pin berg male (For AVR programming interface)

Best thing about this controller is you do not need to remove the microcontroller to get programmed. You can hard solder it and program it onboard itself. This is my model for the circuit below:


So after soldering all the components we are all set to start with Atmel Studio.

So Lets open Atmel Studio: (Check the screen shot)

So after you get the screen click on new project and type the project name as you want. Remember to select only GCC Executable project C/C++


Once this settings are done click OK and you now choose your desired microcontroller. Here ATmega16A falls in megaAVR, 8-bit. So select it. On right hand side you can see the datasheet link as well as devices supported by the ATMEL studio 6 for this microcontroller. The programmer I have mentioned falls in STK 500 category.

After selecting the microcontroller, Click ok. You are now ready to write the program. Atmel studio is so smart that it will give you template of basic c file as well as it will guide you to write instructions all the way long. check the screen shot below :

Since now you're ready to write your first C file for AVR microcontrollers, coming back to start. Lets write code to turn Bit 0 of Port D on and off with 1 second delay.

Here is the C file below:

/*
 * testavr1.c
 *
 * Created: 1/24/2013 12:13:14 AM
 *  Author: MCF009
 */


#include "avr/io.h"   //Contains AVR register details and address
#include "util/delay.h" //Contains delay routines.

int main(void)
{
    DDRD=0x01; //Set direction of port D bit 0 as output.
    while(1) //Infinite loop
    {
        //TODO:: Please write your application code
        PORTD=0x01; //Write to port D bit 0
        _delay_ms(1000); //1 sec
       
        PORTD=0x00; // Clear port D
        _delay_ms(1000); //1 sec
    }
    return 0;
}

Now to compile check the screen shot below. If there are no errors you will get 0 errors in the output console(build). So when successful download the hex file using your usb programmer and check for results on port D bit 0. The pin will be setting high and low at 1 second delay.



You are now officially a AVR programmer. Congratulations!

No comments :

Post a Comment