On one of my web-wanderings I discovered a piece of software called VMLAB. It allows you to build a virtual prototype of a microprocessor circuit, without any actual hardware! Very cool. Even better - it's freeware. This had to be downloaded and tried out :-)
Since I have recently been messing about with HD44780 compatible LCD displays, I couldn't resist trying that. So here is an example I knocked up:
Not bad ... an ATmega168 driving an HD44780 compatible LCD all done virtually, without all that mucking about with actual hardware. I did try sending some user defined characters to the virtual LCD, but that didn't seem to work, maybe that was pushing it.
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).
There are several ways to get your program onto an ATmega168, the way I'm doing it is with one of these programmers and some jumper wires - essentially I followed the SparkFun tutorial here. This is something that I'm working on at the moment:
But you could also use one of these Olimex development boards, which is another way I have tried (and means that you can dispense with all those jumper wires).
The AVR pocket programmer is good because pretty much any PC has USB these days (you can also use serial or parallel ports to program an ATmega). But the pocket programmer can also provide a 5v power supply from USB.
If you want to build your own programmer (which would probably be the cheapest option) then take a look that this, which should get you able to build a programmer for the cost of some wire, resistors and a parallel port connector (assuming that you have a PC with a parallel port). For me, I found it easiest to just buy something from SparkFun. In my experience the pocket programmer only works when attached to a USB hub, I've never managed to get it to work when plugged directly into the USB port of my PC - I've never bothered to find out why.
Of course, the other thing you'll need is a development tool, so that you can compile your source code and get the resulting output file copied to the processor. I'm using WinAVR, or more specifically the portable version which is here. The portable version means that I can put my development environment on a memory stick and use it on whatever computer I happen to be sat in front of. Very nifty.
So when you're armed with 1) a programmer, and 2) a copy of WinAVR ...you should be ready to go. You'll need some code to compile! I'm going to post that next.
I have been starting to learn to use Eagle, so that I can document my circuit diagrams. Being quite new to designing hardware I'm learning that it's not good enough to just keep a copy of the source code anymore! So, here is the very simple circuit that makes up my improved Annoyatron:
It's very simple to knock this up on a small breadboard, you just need the ATMega168, a speaker and a battery, just like this:
Obviously, the ATmega168 is overspecified, but I have a few of them and it's easier to mess around with one type of processor. I really will try to post the source code and some notes on how to get the source code onto the processor as a program. Honest.
...so like me you've gone and got an ATmega168 from somewhere like http://www.sparkfun.com (which is where I get a lot of my stuff). It probably cost you about £2.50. Obviously you'd like to get it to do something meaningful, so what do you need to do? Take a look at this pinout for the processor:
Drop the processor into a breadboard, making sure that it straddles the middle row. All we need to do is connect pins 1, 7 and 20 to a +5v DC supply and pins 8 and 22 to Ground. That's all there is to it. It's useful to note that the purpose of supplying power to pin 1 is to tell the microprocessor that it is not being reset. Not supplying power to this pin will prevent any code from running, it will remain in reset mode. We could use this pin like a reset button on a PC by adding a button and a resistor, but we won't worry about that right now. I'm saying use a 5v supply because it's a nice easy voltage to find (for example, USB will supply 5v with 1A current, which is fine), but the processor should work with a supply between 2.7v and 5.5v (see the datasheet here if you're interested). I often use a spare PC power supply, because they normally have a 5v output and are not expensive.
OK, so the processor may be running - but we've not put any code onto it yet. So that's the next thing on our todo list.
I think that it's good to keep your hand in with some plain old C programming once in a while. So I ocasionally mess around with microprocessors. I've been working on an improved annoyatron, with my version it will pretend to be a mobile phone - by playing a simple ringtone. This is what it currently looks like:
I've used an AVR ATmega168 microprocessor, which is over spec, but I had a couple of them lying around. In fact all of the other components except for the button cell battery and holder were things that I had sitting around. What tends to happen is that before I throw anything away, I take it to bits to see if I can remove anything. Even a salvaged switch can be useful one day. And it's fun to take stuff apart :-)
So ... what I'll try to do is post more about this - what development tools I've used and what I have discovered along the way. I'll be sure to refer back to it if nobody else does.