Thursday, February 18, 2010

Starting with Microcontrollers!

My very first day with ATmega16.. Well, I mean including hardware. Last time, when I programmed the 'SNAKE', I just did the programming part, but had no idea about the hardware and how to actually burn the code into it. It almost feels as if a new era is starting for me, the era of controllers. It feels wonderful!

Started with the 'Hello World' for microcontrollers, a small program in which we blink an LED at a frequency of 1KHz. It's really very simple, just switch on and off a port. When the LED started blinking, I felt on top of the world.

I'm using Atmel's ATmega16 and USBasp as programmer. I am using WinAVR. Needed lots of help, which I got from my ready resource, Merin. There are so many things you need to know in order to program a microcontroller! First you need to edit the 'makefile', which I couldn't understand how! Really, sometimes I can be so dumb! After I got that (thanks to Merin) things appeared simpler. You just need to select 'Enable Editing of Makefile' and save it in the same directory as your C-program. Oh, and don't forget to set the name of your program in 'Target file name'. You can edit the same from 'Makefile'->'Main File Name...' Next from Programmers' Notepad select 'Tools' -> 'make all' and 'program'. The program's programmed into the microcontroller and up and running in no time.

Here's the code for this 'Hello World' program. I am using port b, pin 0 as the output port:

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

void main()
{
    DDRB = 0xFF;        //set port B as o/p port
    PORTB = 0x00;       //initialize LED as off
   
    while(1)                 //loop indefinitely
    {
        PORTB |= 0x01;       //set LED on
        _delay_ms(500);      //delay in mseconds
        PORTB &= 0x00;       //set LED off
        _delay_ms(500);
    }
}

Ok, here's a question for you guys (only for those new to microcontrollers) - why do we need a usb programmer rather than serial programmer on laptops? Answer on next post, so keep a look out!

No comments:

Post a Comment