AVR Adventures

A while ago, I decided that microcontrollers were interesting. I understate. They're cool. Very, very cool. For a few dollars, you can have yourself a tiny computer, capable of driving surprisingly advanced projects. Windowsill menorah? Check. Wandering pumpkin? Check. LED display? Check.

I decided that I should learn about them, especially after working with the Motorola 68HC12 in Digital Systems II. So, a while back, I bought myself a USBTiny programmer and a pile of AVR's. Weeks back, I soldered the board together and wired myself a target board. I got as far as talking to the chip before classes overwhelmed me again:

[ben@earthbound test$] sudo avrdude -c usbtiny -p t85

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e930b

avrdude: safemode: Fuses OK

avrdude done.  Thank you

Finally getting the programmer working was exciting. Up until that moment, I didn't know if my soldering was any good, if the programmer worked, or if the pins were lined up correctly. But output! Validation!

Fast forward to today. You don't have to, of course, since you're just reading this now. But imagine weeks passing. The sad cardboard box of chips sitting unloved in the corner. Done imagining? Good. Cause we're moving along now.

With classes over, I decided to pick up the chips again, and try my hand at actually pushing code. It was surprisingly easy. I used AVRDude to feed the chip a demo piece from LadyAda. It downloaded perfectly! (Not that it did anything, since the code is for the ATTiny2313, and I'm using the ATTiny85.)

Next, I tried my hand at writing C for the chip, starting with a really simple function:

#include <avr/io.h>

int var;

int main (void) {
    return var + SP;
}

Okay. So C. Now what? AVR-GCC is the compiler for these chips, and I got it to spit out a .o file. I don't know what a .o file is, but it isn't hex.

Poking around online, I found a makefile for AVR chips online. Whaddaya know, it worked. After customizing a few lines, it happily built and downloaded code to the AVR. My code, even! (Not that it did anything, since it wasn't supposed to.)

Finally, let's try doing something! I peeked through the AVR libC library for useful functions and kludged this together:

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

int var;

int main (void) {
    while (1) {
        _delay_ms(500);
        PORTB = !PORTB; //PortB is the ATTiny85's output. This should blink every pin.
    }
}

The makefile ran happily, and the code downloaded. A little poking with an LED later, and I had blinkiness! It blinked every eight seconds or so, instead of once a second - I think the clock rate is off - but it works!

Finally, something a little more special: binary counting.

#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>

char dir = 0;

int main (void) {
    DDRB = 0xFF;
    PORTB = 0xFF;
    while (1) {
        _delay_ms(50);
        PORTB += 1;
    }
}
That #define line tells the timer how fast the chip is running. Setting it made delays work right. And as expected, the chip counts up in binary. It's pretty spiffy. I have five bits of output clicking on and off merrily. (Port B is 6 bits, but the highest bit is the RESET pin, and I don't want to change the fuses to make RESET an output.)

Blinking lights. This afternoon. I am satisfied.