Sunday, April 26, 2009

Starting with the AVR microcontroller

I thought it'd be fun to do some microcontroller (uC) programming.  I've been following the Make blog (and a few others) for a while & love the idea of interactive electronics or even something just a little more complicated than vibrating yogurt pots and wired up LEDs.  I can program already, so it seemed like the logical next step.

Well, firstly I spent a fair bit of time being confused by the huge amount of choice (Arduino, BASIC stamp, PIC, AVR etc.).  The Arduino platform is featured heavily in the Maker community, but it seems a bit heavyweight for what I've been thinking about and besides, if you want to make a few "objects of interest" each one would require their own Arduino board which, at $35 a pop and multiple kids on the rampage, could get very expensive.  PIC and BASIC stamp are both good choices but I decided on the AVR series of uCs for a number of reasons:
  1. They're cheap ($1.50 upwards) and readily available.
  2. You can program them in C (one of the languages I understand).
  3. The compiler and programmer software is available free (I'm using WinAVR, the GNU toolchain is available from the Atmel website).
  4. There's a large and active community of users (e.g. AVRFreaks).
  5. There's an Eclipse plug-in for AVR programming (Eclipse is my favourite IDE).
Unfortunately, in my rush to dive in, I didn't do quite enough reading and ended up buying a batch of 10 ATtiny13s (from eBay). These are great because they have 8 pins (small size), but the problem I found was that they only have two PWM (pulse width modulation) outputs - more on that in a later post though.

So, on top of the software and the chip themselves you also need a way to program the chips.  There are dedicated boards available for this, but I chose to use a USB ISP (In-System Programmer).  With these you can have the chip already placed on a breadboard with the project all wired up, program the chip and see what the effects are immediately.  With the dedicated board, you'd have to program the chip, take it out and place it in the project before you could see the effects.  I made another mistake when buying the USB ISP; in my love of eBay I ended up buying an STK500 copy rather than something genuine from DigiKey (e.g. AVAVRISP2); my knock-off has some compatibility issues with the Eclipse AVR plug in which has meant that I now program and build in Eclipse but use AVR Studio to send the program to the chips.  Ah well, you live and learn.

Anyway, onto the obligatory "hello world" for micro-controllers; the blinking LED!

Firstly, rather than re-iterate what others have said very well, just do what I did and follow the first couple of sessions of the sparkfun tutorials.  I love how ghetto it looks having the wires poked into the programming cable and then into the breadboard; I love it so much that I still have it set-up this way.  The only differences are where to attach the leads to the chip, you can work it out by looking at the ATtiny13 datasheet and comparing the pin layout with that of the ATmega8 used by sparkfun.

The picture below shows what I ended up with.  I'd already added the power supply suggestion from the first lesson, which is the purpose of all the electronics at the head of the breadboard, but I'm using a 9v battery rather than an ac adapter.

Here's the code to make the single LED blink:

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

//LED is wired into pin 7 (PB2)
#define LED PB2

int main(void){
//set data direction register for pin 7 to output
DDRB |= _BV(DDB2);

//infinite loop
while (1) {
//turn on the LED
//wait for 1/4 of a second
//turn off the LED
//wait for 1/4 second

Compile (Project->Build in Eclipse), upload to chip (using AVR Studio) and voila! A blinking LED... not particularly amazing, but it's a good start.

Update: Whilst writing this post I figured out how to get Eclipse to program the uC as well!  AVR Studio can autodetect your programmer, apparently I have an AVR ISP V2.  AVR Studio will also tell you what port it's connected on (is there a way to get the Eclipse plugin to autodetect this?).  In Eclipse I had to open up Project->Properties->AVR->AVRDude->Programmer and edit the configuration.  I set the programmer to "Atmel AVR ISP V2" and overrode the default port (first dialogue box below the programmer list) to com3 - which is the one reported by AVR Studio for my set-up.


plinehan said...

Paul, I don't want to alarm you, but I think I found an infinite loop in your code. PANIC!!!!!!!!!

Unknown said...

Thank you for this. There as are a lot of articles about using AVRs, but most of them seem to skip the "hello world" of electronic: the blinking led.
With this i can make my first step into programming AVRs