Tuesday, June 7, 2011

blinky lights with interrupts

In the previous post, I kind of glossed over the problem with using the built in delay function. I said that it would be better to use interrupts so I'll go more in depth about that.

The problem with using the built in delay is that CPU clock cycles are essentially wasted. In such a simple program like we had in the previous post, we are not worried about conserving clock cycles but you can imagine that in a very complex program you would want every clock cycle to do something. Once interrupts are set up and enabled, they will "interrupt" the program when a certain condition is met. In the case of delay, a timer counts clock cycles and once a certain number is reached, the program is interrupted and carries out a "service routine" specified by the programmer. Once the service routine is finished, the program returns to the line of code it was executing when the interrupt occurred.

To illustrate the difference between the two types of delays, I wrote a program that does the exact same thing as the program in the previous post but it uses interrupts rather than the built in delay. This program simply makes the built in LEDs turn on and off at a rate that is perceptible to the human eye:
#include <avr/io.h>
#include <avr/interrupt.h>

int main(void)
{
    DDRB = 0xFF;        //PORT B (LEDs) output
    TCNT0 = 0;          //timer starts at zero
    OCR0 = 200;         //and counts up to 200
    TCCR0 = 0b00001101; //CTC mode, internal clk, 1024 prescaler

    sei();              //global interrupt enable
    TIMSK = (1<<OCIE0); //timer0 overflow interrupt enable
 
    while(1)
        asm("nop");     //stay here....
}

ISR (TIMER0_COMP_vect)
{
    PORTB ^= 0xFF;      //toggle PORTB (LEDs)
}

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

Sunday, May 22, 2011

STK 200

This semester I used the STK 200 from KANDA as an AVR trainer kit. At first I found the kit to be extremely frustrating but it has started to grow on me. I continue to discover useful features built into the unit but these discoveries underline the reason I was frustrated in the first place: there is extremely poor documentation for the kit. After spending countless hours trying to utilize the onboard LCD pins, I stumbled upon example code on the KANDA website which allowed me to understand the pin controls. Perhaps I can detail some of the difficulties I had to work through in future posts and save others the hassle. The good news is that once you understand how to use the kit, it proves to be very robust and handy.