Taking the ATMega8
for an introductory spin
Date: March 7, 2009 | Comment
In 2007 I played around with the PICAXE, a microprocessor sold by a non-profit in the United Kingdom that comes loaded with a BASIC-like interpreter (you can read more about it here and here). I built some lighthouse beacons and a railroad crossing light using the device. It had one drawback: I had to write the code and program the chip using, sigh, Windows.
In 2008 I learned a little about Microchip’s PIC series of microprocessors because that was the favored chip by most of the developers in the DIY Christmas lighting world. Though I didn’t actually program PICs, I did learn how to burn HEX code into them using an ADM programmer. These too, had a drawback: you had to write the code and program the chip using, ugh, Windows.
Along the way, though, I heard about the AVR series of chips from Atmel Corp., which had a distinct advantage over PICAXEn and PICs: there was a small community of people who used Macintoshes to make them go. There was a full tool-chain for programming in C (a language I had no experience in) and there were USB devices that allowed for burning the code. Well, hello, sweetheart.
So, somewhere along the line in 2008 I bought myself an USBtinyISP programmer from Adafruit Industries; today’s price in March 2009 is $22 but I think I paid a little less for it than that. I didn’t build it after I got it; the board and components sat unworking for a couple of months while I finished the Christmas lights.
But once the lights were up and running, I decided to get moving on learning to program an AVR in C (maybe someday I’ll tackle assembly code, but I’m really a path-of-least-resistance guy). My first step was to set up a breadboard and get a chip in a position to program.
And herein I ran into a small problem: I had bought a bunch of ATMega8s to learn on and lo and behold, I couldn’t find how to connect a USBtinyISP to a Mega8.
Through trial-and-error and posting questions on Adafruit’s forums, I worked out a method to hook them together, but thought it would be worthwhile to post here not only the method, but draw up a schematic as well.
I should point out that for my initial set up I used a six-pin header that I soldered to six wires, which I then plugged into the breadboard; today I would advocate buying the header adapter sold by Sparkfun Electronics. Though initially designed to be an interface between 10-pin and six-pin headers, it has been adapted to allow you to put a row of header pins into one side that can plug directly into a breadboard. They’re only a buck, so I was forced to buy some other stuff to justify the postage
…
The code that I wrote for the ATMega8 is pretty rudimentary (it flashes a single LED) but I reproduce it here for full disclosure.
/* Name: main.c * Author: David M. Cole * License: 2008-2009 Creative Commons Attribution-Noncommercial-Share Alike 3.0 U.S. * Target: ATMega8 * Compiler: AVR-GCC */ #include <avr/io.h> /* A file that defines inputs and outputs */ void delay_ms(uint16_t x) /* Declare substitute delay function, bring in variable x */ { uint8_t y, z; /* Declare the variables y, z */ for ( ; x > 0 ; x--) /* Loop while x is less than zero; decrement */ { for ( y = 0 ; y < 90 ; y++) /* Loop while y is less than 90; increment */ { for ( z = 0 ; z < 6 ; z++) /* Loop while z is less than six */ { asm volatile ("nop"); /* Inline assembler code: no operation performed; i.e.: do nothing */ } } } } int main(void) /* Main function; every program has a main */ { DDRD = 1 << 4; /* make Pin 6 (PortD4) an output */ for(;;) /* Loop like forever */ { char i; /* Define the variable i */ for(i = 0; i < 10; i++) /* Loop while i is less than 10 */ { delay_ms(50); /* Hang on for a moment */ } PORTD ^= 1 << 4; /* toggle the LED; if it's off, turn it on */ } return(0); /* never reached */ } |

Leave a reply