Simplest ATmega168 program

So how would you know that your code is running?  Just about the simplest thing that you can do with a microprocessor is flash an LED on and off.  So we'll start with that.  You'll need to attach an LED and a resistor in series from pin 28 of the ATmega168 (assuming that you have the DIP format).  So take a wire from pin 28 on the ATmega and attach it to the positive pin of an LED.  Then attach the negative pin of the LED to one  wire of the resistor.  Finally, attach the other wire on the resistor to Ground.  When the program code turns on pin 28, power will flow through the LED through the resistor and then to Ground.  So the LED will come on.  Cool.

Obviously LEDs are diodes, so they only go one way round (the resistor can go any way round).  The purpose of the resistor is to limit the current and prevent the LED from burning out.  The exact resistor value does not matter in this case, anything from 200 to 1,000 (1k) Ohms should be fine.  The lower the value of the resistor the brighter the LED will be.

Don't forget to also wire up the other basic pins on the Atmega (I've blogged about that before).  Now we can get to some code.  I've put the source code here: http://www.codehosting.net/blog/files/led_blink.zip

Extract the contents of the zip file to a folder and open the files using Programmers Notepad from WinAVR.  Connect up your usbtiny programmer, ensure that the power is on and we're ready to go. 

Go to the Tools menu of Programmers Notepad and click on [WinAVR] Program.  The program should compile, get loaded onto the microprocessor and the LED should start to blink!  Woo-hoo.

NOTE: I have also used the "ponyser" serial port programmer.  This can be used as an alternative, by changing these lines of the makefile:

Put a # at the start of line 202 and remove the # from line 201:
AVRDUDE_PROGRAMMER = ponyser
#AVRDUDE_PROGRAMMER = usbtiny

Remove the # at the start of line 206 and make sure that the COM port matches the one you're using:
AVRDUDE_PORT = COM1

Finally remove the # from the start of line 227:
AVRDUDE_FLAGS += -P $(AVRDUDE_PORT)

You should now be able to use a serial port programmer instead (my example uses COM1).