So if I wanted to best describe the first program that ran on my DEC PDP-11/53 ... then it would have to be:
"Do nothing. Do nothing. Do nothing. Stop."
But at least it worked. I entered those commands as machine instructions in ODT (Octal Debugging Technique) and it ran. Not much of a "Hello World" program though is it?
Here are the details of the instructions:
001000/240<lf> ; NOP
001002/240<lf> ; NOP
001004/240<lf> ; NOP
R6/1000<cr> ; set to start code at 1000
R7/1000<cr> ; set PC to start address so we can use P and not clr PS
RS/340<cr> ; set PS to disable interrupts
P<cr> ; start program
That program was a hacked version of something I found here. Now I need to make it do something better.
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).