I learned how to program microcontrollers (MCU for short) a couple of years ago. I actually wanted to build a robot. I could program it just fine, I just never actually built the robot.
MCUs can be used for so many different things. Many of the projects we've been talking about can be completed with them VERY easily. The solar panel sun tracking... I could program a MCU to do that in 20 seconds. It'd literally be just a few lines of code. It's a GREAT tool for anyone who does these types of projects, and I think that everyone should learn how to do it. It's not as hard as you would think either.
The great thing about MCUs is that they are CHEAP!!! The one I'm demonstrating with is (if I remember correctly) $1. Only ONE dollar. It's crazy they'd sell this tiny thing with so much potential and technology going into it for 1 dollar! I'll get into the costs tomorrow, but you could program your own microcontroller for probably under 40 or 50 bucks. That's buying the programmer (25 bucks) and breadboard (probably 20 bucks) and MCU (1 dollar). Now, you don't NEED the breadboard, but you'll want one. It's invaluable.
Anyway, I wanted to teach you guys, step by step, how to get started with MCUs. I'm going to be doing this in installments. My first video or two will be getting the hardware, getting the software, hooking it all up etc. I'm literally taking you through EVERYTHING you'd have to do.
First, the prerequisites.
- -You have to have a computer.
Second, the "should haves"
- -You should know how to solder. If you're doing anything with MCUs, you're going to have to solder, so learn. It's extremely easy. Tons of tutorials on youtube. BUT... you can get away with not knowing for now...
-You should know how to program. If you don't, that's fine, I'll teach you the basics. But it'd make it a lot easier. I'm going to be using C, but I believe you can use other languages as well.
Now, I want to get this straight as well. I am not an expert at this. I'm pretty much a Novice. I know how to do it, and I've done one or two small projects with it, but I am no where near an expert. I'm SURE there are other people on here who are more experienced than me, and I hope they'll chime in to help me out sometimes. The programming is the easy part. Getting the MCU to do EXACTLY what you want... that's a bit trickier. Not hard, but trickier.
Ok, with that done, I'm heading to bed. But I should have the first video up tomorrow, after work, and later if it's sunny.
********UPDATE #1... Hardware and Software*********
To begin any project, you need to know where to get the materials. I'm going to list where I bought my materials, but there are many other places you could find the same things. I'll also quickly show where I got them in the first video. I am using an Amtel AtMega8 for my programming. It's small, cheap, and it works in a breadboard. (More about that later)
To begin programming MCUs, you need
- An AtMega8
- A programmer
- A computer
- A breadboard
- Software
For the AtMega8. I bought mine from digikey.com. I bought it while I was ordering a bunch of other electronics for another project. It's not really a "buy one thing" kind of store (although you probably could). But you can find the AtMega8 (not the L version) a bunch of different places online. You want the PDIP version. Don't ask, that's the version you want. It has through hole connections (meaning it has a bunch of spikes sticking out of it which can be put through holes in project board you can find at radioshack). I believe the other two versions are a surface mount version, and a socket mount connection. I'm not sure though. Just buy the PDIP one. The PDIP one is rectangular, while the other two are square. You want the rectangle.
For the programmer, I used USBTinyISP. Here is the website for it. http://www.ladyada.net/make/usbtinyisp/index.html I like it because you build it yourself. They ship you a box of parts, and you solder the entire thing together. It's a great learning experience. They also sell fully assembled ones(I think), but it's fun (and cheaper) to put it together yourself. Here is the link to buy it. http://www.adafruit.com/products/46 This is by no means the only option, you can buy an official programmer from Amtel, but it's more expensive, and this gets the job done. You can also just buy the PCB and microchip and source the rest of the parts yourself if you really wanted to. (But hey, it's $22, you really can't beat it...) Oh, I don't rememeber if it comes with a usb cord or not... so if you don't have a normal usb cord, usually used for printers, then you'll have to find one somewhere. One that has these ends.
For the computer... I'm using a PC with Windows XP on it, but don't let that discourage you. You can program microchips from Mac OS X, and Linux/Unix. I don't know any other way of doing it besides through the console in os X and Linux though. It can work, but I'm not going to go through it in this walkthrough. (Ok maybe I'll try OS X, eventually). I'll get into the software later. Oh, you also need an open powered USB port. Which is basically any, unless it's on a keyboard or something.
For the solderless breadboard... This thing isn't NEEDED, but you'll want one. There is a picture of one below. Basically this allows you to make circuits without soldering. It's invaluable because I doubt you'd get the circuit right the first time without testing it anyway. If you don't know how they work, google it, or just check out the wiki. http://en.wikipedia.org/wiki/Breadboard You can buy them from digikey, but I didn't. I bought mine from a site called electronic goldmine. http://www.goldmine-elec.com/ I also bought a little pog (yes pog, who doesn't remember pogs?) container filled with LEDs. I think it's a semi surplus website, cause the LEDs were only.. I think 5 bucks... for maybe a hundred of them or so. It's easy to find the stuff on the site. You can find solderless breadboards at radioshack though. Although they're (much) cheaper online.
. Image borrowed from wikipedia.
For the software... Since I'm using XP, I'm using a program called AVRStudio. If you like the console, you can use AVRDude. Both of these are on the website for the programmer. And it also has instructions on how to get it to work with both programs. I like AVRStudio, although it needs some fanangling to get it to work with the non-official programmer. (It's not hard, and I'll have instructions in my vid.) I also like AVRStudio cause it has this cool feature that let's me physically see which bits and registers I'm turning on and off. I LOVE this tool for debugging. Cause you can see EXACTLY when something will happen and why it's not working. I often step through my program just to make sure everything gets turned on at the right time. I know you probably don't know what that means now, but you will, once I start programming.
Now, without further adieu... here's the hardware and software video
Ugh, terrible video quality. The output from the screen recording program did not like to be made into other forms. But you get the point. I'll get better at it, I was in a hurry for this one.
********UPDATE #2... Wiring AtMega8 for programming*********
********UPDATE #3... Installing the software for programming*********
Please know the software I use is just what I like. There are lots of different programs you could use. I am by no means promoting these products, I just like them for my small projects.
********UPDATE #4... Your first program sent to the microcontroller!!!*********
********UPDATE #5... Bit Manipulation!!!!*********
Yes, this stuff is useful...
********UPDATE #6... Using a 7 Segment LED Display*********
Video six is how to hook up a seven segment display, and video seven is how to program it.
********UPDATE #7... Functions and Header files*********
********UPDATE #8... Timers*********
Timers "theory" if you will Part 1.
Part 2
Sample program