Thursday, March 1, 2012

STK 200: First Program (LED Blink)



For this program, you should already have AVR Studio 5 and AVRISP-U installed on your computer. If you don't, visit my previous post Getting Started Programming Guide (link). This is a simple program which simply makes the built-in LEDs to blink. I'm going to use C for this program because the code looks really simple. The assembly version is not really difficult either but we'll keep it simple for now. Note that AVR Studio 5 has a built-in C compiler. You might remember for AVR Studio 4, you had to download AVR GCC and integrate it. If you downloaded AVR Studio 5, you already have the C compiler.

What will the program do? If you look at the STK 200, there is a row of 10 LEDs numbered from 0 to 7 then "ISP" and "ON." These are the built-in LEDs. Our program will make the LEDs numbered from 0 to 7 blink on and off at a rate perceptible to the human eye. In microcontroller terms, this means physically connecting the LEDs to PORTB and sending alternating HIGH/LOW signals to PORTB. Note that is all output - no input. If you don't know what PORTB is, check my Basic Layout Guide (link).

Step 1 - Create a new project: Go ahead and open AVR Studio 5. From the startup screen, go to File>New>Project... and you should see a New Project dialog box. At the top left of the dialog box, select the C templates and then choose "C Executable Project" in the middle of the screen. At the bottom, fill in "blink" as the name and this should automatically fill the solution name field with the same text. Also, make sure that the "Create directory for solution" box is checked. This checkbox will not affect your code at all, but it will keep your AVRStudio folder better organized. Look at the picture below and make sure your screen looks the same:
STK 200: New Project Dialog Box
Click OK and you should see a Device Selection dialog box. Here, you select the name of your microcontroller. If you don't know what microcontroller you have, I'd recommend looking at my post Switching Microcontrollers (link). I'm using an ATmega32, so I selected that. Hit OK and you should reach the actual C file that you are going to edit. The screen should look something like what is below:
STK 200: Blank C Template
Step 2 - Writing the code: As you can see, the program automatically generates some code to make you life easier. Since our program is so simple, we only need to add a couple lines and we are done. Basically, the code section (not including the top comments section) needs to read:

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

int main(void)
{
    DDRB=0xFF;          //PORT B (LEDs) output
    while(1)
    {
        PORTB=0x00;     //LEDs ON
        _delay_ms(1000); //delay
        PORTB=0xFF;     //LEDs OFF
        _delay_ms(1000); //delay
    }
}

Once this code is entered, you need to build it. Hitting F7 is the easiest way to do that. Otherwise, go to Build>Build Solution. At the bottom of the screen you should end up with a message saying "Build succeeded."

Step 3 - Make the connections: First, make sure that PORTB is connected to the LED header using the ribbon cable supplied with the kit. Then, connect the board to the computer using the ISP also supplied with the kit. If this doesn't make sense, look at the picture below and make it look the same:
STK 200: Make the Connections

Step 4 - Transfer program to board: Now, we need to transfer the built program from step 2 to the microcontroller. Back in AVR Studio, go to Tools>AVRISP-U and you should see the AVRISP-U software pop up. Check the bottom left corner for a green light and text describing the correct device (in my case ATmega32).  If you get a red light, check the hardware connections and try again. Now, we need to load the hex file to flash, so hit Ctrl+O or go to File>Load>Flash... and browse to the hex file of the program you just wrote. On my machine (windows xp), the projects are stored in separate files in My Documents\AVRStudio. I named my project "blink" and used the default file location, so the hex file had the following path:
C:\Documents and Settings\Nick\My Documents\AVRStudio\blink\blink\Debug\blink.hex
STK 200: Loading Program with AVRISP-U
Hitting open will load the hex file into the AVRISP-U software. Before you load the program onto the board, you should probably erase what is currently in the microcontroller's Flash, so go to Device>Erase. After that, hit Ctrl+Alt+F6 or go to Device>Program>Flash and the hex file you loaded previously will be transferred to the board.

At that point, the LEDs should start blinking. The program will continue forever (or until you erase it or turn it off).

2 comments:

  1. The screen should look something like what is below:

    ReplyDelete
  2. Awesome Tutorial Buddy... I am totally new in MC programming.
    One thing I want to ask where are LEDs in Your Board?? You Indicated them in Figure as "PORTB to LEDs"

    ReplyDelete