Saturday, June 4, 2011

STK 200 - blinky lights

* update: I wrote a revised version of this guide using AVR Studio 5. It uses the same code but I added new photos and wording. Check it out: STK 200: First Program (LED Blink)

As I mentioned previously, I was initially frustrated when I tried to program the STK 200 from Kanda because of the lack of documentation. So I thought it would be helpful to post some of my experiences. To start off, I'll explain the way I loaded a super simple, blinky lights program.

So lets assume you just got the kit in the mail and you have not done any AVR programming in the past. Lets start by installing some software. Go to the Atmel website and download AVR Studio 4 (you will have to register but it is free). Atmel released AVR Studio 5 beta in March 2011 but as of now I still use AVR Studio 4 because of the extensive support for it. Next go to the Kanda website and download their AVRISP-U software. Finally, we need AVR GCC (C Compiler). So you can check out the WinAVR website for a link to the download page or just go the WinAVR sourceforge page and find the latest stable release.

So what do we actually want our program to do? Our objective is to simply cause the built-in LEDs to blink. If you look at the kit, there is a row of 10 LEDs numbered from 0 to 7 then "ISP" and "ON." Looking along the edge of the board, there is a row of "PORTS" and across from "PORTB" there is a 10 pin header named "LED'S". We need to connect PORTB to the LED header using the supplied ribbon cable. I marked the two locations on the picture below:
STK 200: LED Layout

This is a good time to point out that on the above picture, I switched my stock ATmega8515 with an ATmega32 which I ordered. For this program, everything, including the code is exactly the same for both. However, for complex programs, you might need more memory or functionality which the ATmega32 could provide. If you are interested in switching your microcontroller, check out my "switching microcontrollers" post (link). And in this blog post, whenever you see ATmega32, replace it with ATmega8515 if you are using the stock STK kit.

Assuming all of the software is now installed, go ahead and open AVR Studio 4. You should see the following screen (except there will be no recent projects):

Pick the New Project option. Now choose "AVR GCC" as the Project Type, "blink" as the project name and make sure that both the "Create Initial File" and "Create Folder" boxes are checked. It should look like:

Finally, click next and select "ATmega8515" from the list of devices on the right side. Click finish and you are finally ready to write some code!!

Essentially, PORTB is an I/O port which will control the LEDs. So our code will just tell PORTB to send out alternating OFF/ON signals. Here is the C code I would write:
#include <avr/io.h>
#include <util/delay.h>

void delay_ms(unsigned int d)
{
    _delay_ms(d);        //make delay portable
}

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
    }
}
I should mention that using the built in delay function is probably not the best idea. I'm using it here because it makes the code look really clean and you can adjust the delay time very easily. You simply write delay_ms(x) where x is the number of milliseconds of delay. The way you should actually do delay is using interrupts but that is a whole other subject. So this works for now. One unfortunate side effect is that he AVR simulator (debugger) in AVR Studio 4 does not always work with the delay library. If you are getting a dialog box that asks you to "please browse to the present location for files originally found at c:\blah blah", you're not alone. But anyway, once you've entered the code in AVR Studio, hit F7 to build the program. As long as you have no errors, you are good to go. Now, lets get set up to program the microcontroller.

Look at this picture to get an idea for the connections:

Basically, there is one connection with the ISP from your computer to the board and another connection with the ribbon cable between the LED Header and the PORT B Header. Both of the cables are provided with the kit.

Next, open the AVRISP-U program on your computer and at the bottom of the window there should be green light and something saying that the device is recognized. If there is a red light, hit the reset button on the upper right and see if that works. Here is a screenshot of the program:
Then, on the top menu, File>Load>Flash... and browse to the "blink" folder you created when you started in AVR Studio. Once you are in the blink folder, open the folder named "default" and you should find a file named "blink.hex". Load this file. Then do Device>Erase. Next Device>Program>Flash. And finally Device>Run.

And thats it! All 8 of the LEDs should be blinking on and off at a reasonable rate. Hopefully it helps somebody getting started with the kit.

Also check out my other STK 200 posts:

7 comments:

  1. Cool!!!!! The SFSU engineering students are very glad that you are sharing your 478 tips and tricks with us!!!

    ReplyDelete
  2. Next time put the assembly code please

    ReplyDelete
  3. Hey Nick
    I'm glad to find such first steps to start using this kit because it is very rare to find any help on other websites. it is my first time to use it and I'd appreciate your help if it is possible.
    I'm using AVRstudio5 to write the code and compile it, I'm also using the AVRISP-U to load the hex file on the kit to make it work, but I'm getting this error
    can't create file C:\Program Files\Kanda AVR\AVRISP-U\Studio projects\LED.prj
    if u faced such a problem could u please provide help ??
    thnx

    ReplyDelete
    Replies
    1. I'm writing a similar guide for AVR Studio 5 right now, should post it within a week. Hopefully that can clear it up, but no I haven't faced a similar problem.

      Delete
    2. thanks, the problem is solved when I re installed the AVRStudio.

      Delete
  4. shouldn't it be DDRB=0xff; ?

    ReplyDelete
    Replies
    1. Wow totally right! I can't believe that was still in there. That was left from a program where PORTB was used for buttons. I guess I grabbed the code and didn't change it back. Thanks!

      Delete