AVR: Introduction to AVR through a simple program.

Post image of AVR: Introduction to AVR through a simple program.
Filed in AVR , Articles , Featured , Tools 6 comments

Guest Article by Kumail Ahmed.

AVR

AVR

Hello everyone, most of you might have heard the word AVR. I’m going to explain you the basics of how to program it and the infinite treasures inside this fabulous microcontroller.

AVR are RISC based microcontroller made by ATMEL. I won’t go into its theory; this is something you all should read by yourself! Theory is important but not at this level.

If anyone has used PIC or 8051, AVR aren’t that much difficult. As an example I would be using ATMEGA8. This controller is cheap and easily available in the market. It contains Digital I/O ports, ADCs, Serial Ports, PWM, timers, external oscillator and SPIs . It has a comparatively large Programmable Flash memory, about 8K.

ATMEGA8 - Pin Diagram

ATMEGA8 - Pin Diagram

I think this much of theory is enough for now. Let’s come toward the real thing and that’s how to program these brats!! You will need two softwares and an ISP programmer, that’s it. The software is free to download from the following sites

1) WIN AVR : http://winavr.sourceforge.net/download.html

2) ATMEL AVR STUDIO 4.16: http://www.atmel.com/dyn/Products/tools_card.asp?tool_id=2725

As far as the programmer is concerned, you may buy one from SparkFun or try this website.

So first install WinAVR and than AVR studio. After the installation you are ready to go.

In AVR studio you can program either using Assembly or C.
Open AVR studio click on New Project > AVRGCC for C, than select your controller from the list, most of them are in AVR Simulator or AVR Simulator 2. Now we are ready to begin.

Your screen should be like this:

Simulator

Simulator

As I told you AVR programming is in C.We are going to write a very simple program which is going to turn on and off the led connected to the first pin of PortB(PB0) with a delay of 25ms. The program follows:

#include<avr\io.h>
#include<util\delay.h>

int main()

{

DDRB=(1<<PB0); //Set the data direction register to be output

while(1)   //This is an infinite loop

{

PORTB=0×01;    //Write 1 to PB0 (first pin on port B)

_delay_ms(25); //no explanation needed!

PORTB=0×00;   //Write 0 to PB0 (first pin on port B)

}

return 1;   //This line is never reached,

//just a requirement of the

//software as we have set the

//return type to be as integer

}

For anyone who has done C, nothing would seem like a Frankenstein monster! The weird symbol “<<” is a shift left operator. This basically shifts n number of bits to the left.

In AVR if we want to use a port as output, simply write 1 to the specific bit in the DDR register of the specific port. So here we want to make the first pin of portB output. Therefore we wrote 1 there. PB0 is a defined value in <io.h>. Something like:

#define PB0 0

So the line DDRB=(1<<PB0) really means DDRB=(1<<0).  In simple English it means “shift 1, zero times towards left”. The final bit sequence looks like 0000 0001. Just for practice try this DDRB=(1<<3). This would result in 0000 1000. i.e., shift 1(0×01), three times towards left.

Just remember that bit-wise operations are at the heart of embedded programming. You should learn them through practice.

Next comes this line: PORTB=0×01;

This simply means to turn PB0 high.

I would urge you to remember this line “We write to a port but we read through a pin”. Although this program doesn’t take any input, but knowing the above fact is important.

The delay function simply takes in time in milliseconds and the next line turns the PB0 low.

The while(1) function is an infinite loop similar to for(;;). The program repeats for every.

After this long commentary, I now tell you how to simulate your code. Go to Build>Build and Run in the Main Menu. You can view simulated result from the I/O view. It looks like this:

IO Screen

IO Screen

Thats for all now as introduction.In the next article we will learn how to take input.

Posted by hamzaazeem   @   12 September 2009 6 comments
Tags : , , , , ,

Share This Post

RSS Digg Twitter StumbleUpon Delicious Technorati

6 Comments

  • creativeelectron

    Thanks Nathan. We already have an article on site explaining the procedure of taking Inputs, both digital and analog. Check them below;

    Reading Digital Inputs in AVR: http://creativeelectron.net...
    Reading Analog Inputs in AVR: http://creativeelectron.net...

    We have noted down your suggestion, and our next article will be on Interrupts .

    Thanks.

  • Nathan

    Nice. Thanks so much for the explanation. Could you also please let me know how to do the 1) input 2) interrupt functions in C with explanations please? Thanks so much in advance. Regards, Nathan

  • GUJJAR 302

    WOW .A GREAT WORK STATRED BY A GREAT PERSON.KEEP IT UP.
    AND DONT WORRY FOR UGV.

  • Kumail Ahmed

    Thank you faizan, I really forgot this....

  • fj

    Kumail wrote:

    while(1) //This is an infinite loop

    {

    PORTB=0×01; //Write 1 to PB0 (first pin on port B)

    _delay_ms(25); //no explanation needed!

    PORTB=0×00; //Write 0 to PB0 (first pin on port B)

    &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    }

    A delay should be added at the line '&&&' I pointed in the program. Because it will toggle instantly (1 clock cycle) from 1 to 0...and the toggling could not be seen.

    Thanks for the program. We people thought AVR a frankenstein...its hell easy...

blog comments powered by Disqus
Previous Post
«
Next Post
»