Introduction: Ghetto Programming: Getting Started With AVR Microprocessors on the Cheap.
Microprocessors are so cheap these days. If only there were a way to program them up just as cheaply...
*wavy dream-sequence lines*
In this instructable, find out how to build up a complete AVR microprocessor toolchain: compiler, programmer software, programmer hardware, and some simple demos to get your feet wet.
From there, it's just a hop, skip, and a jump to world domination.
The endpoint is not quite as swanky as Atmel's suite, but it's gonna run you about $150 less and take only a little more work to get it set up.
This instructable is based on the Atmel ATtiny 2313 chip, mostly because it's one of the smaller chips (in size) while still being beefy enough to do most anything. And at $3 a pop (non-bulk), they don't break the bank.
That said, most of the steps are applicable across the AVR family, so you'll be able to re-use most everything when your programming needs outgrow the ATtiny and you reach for the $8-$12 ATmegas.
Update Aug 24, 2007: I've gone USB! It's neither ghetto nor particularly deluxe, but a nice middle ground. If you've got a computer without a parallel port, check this http://www.instructibles.com/id/EDRQZ56F5LD8KDX/ out.
If you've got a parallel port, or are just feeling cheap, read on!
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Go Order Parts!
Atmel ATtiny2313 chip: $2.50 - $3Socket for chip: $1Parallel port connector (DB25): $3 at Radio Shack or $0.95 at SparkfunHeader Pins: $1-$2 (at least one strip each male & female, maybe 2 female)Some LEDs and resistors: $1
= Around $10, even if you go deluxe. Half that if you can find an old parallel port cable.
A pushbutton switch or two: $1.
A piezo speaker: $1-$5
Light sensitive photocells: $2-$6
Breadboard for making complex circuits: $8-$10(?)
Other Stuff You Oughta Have
Computer: The older the better because it needs a parallel port.
Hookup wire, solder, soldering iron
Source of ~5v DC: Batteries will work, and old computer power supplies are perfect.
I get a lot of stuff from Sparkfun because they're
fast, reliable, and fairly priced. They carry AVR chips (but seem to be out
of ATtiny13's at the moment). They've got everything on the "Necessary"
list in one place, nice website.
If you're gonna be ordering a lot of chips, you can get a deal from Digi-key or similar. For instance: ATtiny 2313 for $2.36 each. They've only got 27,853 more in stock, so order quick. (Make sure you get the DIP form-factor.)
Go order stuff now, and we can set up the software side while you wait.
The waiting is the hardest part.
Step 2: Get and Install Software: Linux Version
If you run Windows, skip this page. Or install Linux, then come back. :)
The Linux AVR software toolchain used is:
1) an editor of your choice (emacs, gedit, kate, etc)
2) AVR-GCC compiler and libraries
3) AVRdude programmer
4) A nice Makefile to tie it all together
May 2008 Edit: Ubuntu has packages for the whole toolchain. Your installation is now as easy as: "sudo apt-get install avrdude avr-libc binutils-avr gcc-avr". Bam! The versions that come with 8.04 are good all around. Woot Ubuntu!
If you want to do it "by hand", the script below automates most of the work for you. Note that as of May 2008, it's a year old, and doesn't support the newer ATTinyx5 series chips. You can mess around with updated versions of all the software is you'd like. (It's saved with a .txt extension b/c Instructables wants it. Doesn't matter.)
You're going to have to edit the first command in the install script depending on whether you're using an apt-based package system or a yum-based one. Just uncomment the relevant line. It needs to make sure you've got some packages which are required for the compilation stage.
Have a read through the script to make sure it's not doing anything stupid, then get root and run it and you should be golden.
It will create a directory /usr/local/AVR and install all the AVR tools there.
The last thing the script does is to add avr-gcc to your path so that you can run avr-gcc directly by adding a line to the tail of your /etc/profile.
The other file, setupParport.txt, is just a (very) simple script to set up your parallel port for user use. If you're savvy, you can add the same commands to a boot script so that it enables the parallel port every time you boot up. Otherwise, just run it by hand.
Websites with similar, but different and possibly outdated, procedures include: Psychogenic.com, AVRWiki, and this (old) Linux Focus article.
Alternatively, I hear cdk4AVR has a complete set of compiler tools in RPM format. I haven't tried it, but it could save you the 30 min of compiling time on a slow, old laptop.
Step 3: Get and Install Software: Windows Version
The MS Windows software toolchain is based on the same compiler as Linux one, but with some optional extra GUI friendliness.
WinAVR is the compiler and programmer software. It also includes a nice programming editor, Programmer's Notepad.
Download and install it. It's that easy. Almost.
As the last step in setup, you'll need to make the parallel port user-accessible. WinAVR includes a program for you to do this. If you accepted the default installation options, run C:\winAVR\bin\install_giveio.bat by double-clicking on it. A window will flash up and down, and then you're done.
Step 4: Make Programmer Cable
I think I got the pinouts from somewhere else, but this site has a nice schematic of the parallel port pins for your reference.
Mine goes something like this:
Parallel Port AVR Function Color2 MOSI Orange/Grey11 MISO Orange1 SCK Green16 RESET Brown18 GND Brown/Grey
Only tricky bit here: Note that pin 1 (SCK) is on the upper-right hand side when you are looking at the solder pins from the back. It's upper-left when you're looking at it here, and in the circuit diagram.
Also, the guy's website above has ground connected to 20 and 21 while mine (and others) use 18. Many of the pins connect to ground, and it doesn't matter which of them you pick, as long as you get ground.
If you look around the web, you'll find that most people put resistors in either the cable or the cradle (next step) to protect their computer's parallel port from excessive voltages on the AVR chip for use when programming it in-circuit. We will be using strictly 5v here, so there's no such worry, and I leave them out for simplicity.
However, if your chip ever comes near >5v, DO NOT USE THIS CABLE WITHOUT RESISTORS! A computer with a burnt-out parallel port is no fun. That said, I've been using it without incident for 6 months now.
Step 5: Make the Programming Cradle
The first thing to do is superglue all the headers and the socket to the circuit board. That way, it's easier to solder to. You can even make an extension for the header like I did if you need more room on your circuit board.
The wiring is as follows:
Cable ATtiny2313 PinMOSI 17MISO 18SCK 19RESET 1GND 10
Get yourself the short ATtiny2313 overview to double-check the pinouts.
And remember: you're soldering up the wires on the underneath, and it's mirror-image. It might help to mark where pin 1 is on the bottom side before soldering. (I did it wrong once. Once.)
That said, the wiring is very simple. Just pin to pin, and then connect all the header pins to the closest pins on the socket. Fortunately for me, this pre-printed circuit board from Radio Shack did the trick.
The cradle is versatile too. See the last picture for my ATtiny13 programmer cradle. I have another for an ATMega8 too.
Step 6: Make Some Blinkenlights
An integral part of the Ghetto Development Environment is the plug-in LED.
The AVR chips can all source a fair amount of juice (50mA or more), which is enough to burn out your standard red LED, so it's a good idea to put protective resistors inline with your LEDs. Enter the Ghetto Blinkenlight.
To make them, simply solder a smallish (150 ohm) resistor to the negative lead of an LED, then you can just plug it straight into 5v and it won't burn out. (5 - 1.4) v / 150ohm = 24mA, which is just about right. Make a few -- I made eight. We only use one here.
Step 7: Set Up the Programming Project
Linux and Windows, the procedure for starting a new project is basically the same.
1) Make a new directory. Here, call it LED_Demo or something.
2) Copy your Makefile into that directory.
3) Download the code (LED_Demo.c) into the directory, or start writing new code from scratch.
4) Edit the Makefile to reflect the chip you're using and the name of the project.
Then you're ready to program.
Editing the Makefile
Windows: The Makefile's set up for you. All you have to do to make the Makefile work is remove the ".txt" from the end of the filename. Open it up in Wordpad or Programmer's Notepad anyway, just to have a look at the options you can change later.
Linux: Rename the Makefile.txt to Makefile. Then open the Makefile up in an editor. Un-comment the line that has "/dev/parport0" and comment out (with a #) the line that has "lpt1".
Step 8: Get Your First Demo Circuit Set Up
Put the chip in the cradle. You'll notice that the pins all angle outwards a bit when the chips come from the factory. Bend them in (gently) on the table so that they're all even and parallel. Then it fits in the socket nicely.
Notice the alignment of the chip. The little dot marks pin 1. Is it where pin 1 should be?
Plug in your 5v power and the programmer cable. Plug the programming cable up to your computer.
Take a Ghetto Blinkenlight and put it between pins 8 and 10 so that the resistor (negative) is in pin 10, the ground for the chip.
Step 9: Program the Chip
1) Get in the LED_Demo directory.
2) Cross your fingers.
3) Type "make"
1) Open up LED_Demo.c with Programmer's Notepad.
2) Cross your fingers.
3) Select "Tools...Make Program"
Watch the log as the text goes scrolling by. There are two things you're looking out for here.
One is if the program compiled sucessfully. If there are no errors, it did. If it didn't, why? Did you mess around with my code?
Two is if the chip programmed sucessfully. For this, it writes the code in, then verifies the chip's memory. It should say "Contents Verified." If it says something about the parallel port or "giveio.sys", did you enable the parallel port back a couple steps ago?
Success? Yay! There's nothing like the sweet smell of blinking LED's in the morning.
Step 10: Explaining the Software
The code's pretty simple, as far as AVR code goes, but still displays a few tricks of the trade. If you're still basking in the glory of your blinking light, you can come back later.
The #include lines load up some of the extra functions and definitions in the AVR-libC suite. In particular, the delay function _delay_ms() is in delay.h. Interrupt.h has all the pin definitions which make life easier. You're always going to want to include it.
The program always starts from the function main(). In this simple case, it's the only function we have.
The first command in main(), DDRD = _BV(PD4);, seems pretty cryptic, but here's what it's doing. DDRD is the Data Direction Register for port D. All the input/output pins are broken up into different ports for easier access. The one we're using happens to be in D. We need to enable the pin PD4 for output for the LED.
The DDRs are set up so that they have 8 bits, one for each pin in the port. You set a pin up for input by writing 0 to its bit in the register. You can set it to output by writing a 1. We want the contents of DDRD to be 00001000, or output only on pin 4 (read right to left). So how do we do this?
_BV(i) takes a number, i, and converts it to an 8-bit binary number where the i'th bit is a 1 and the rest are zeros. Just exactly what we need. And PD4 is the number corresponding to pin 4 on this port. So we set DDRD to _BV(PD4), and then pin 4 (and only pin 4) is set up for output -- blinking our LED.
The rest of the program repeats forever in a loop. It alternates between turning pin D4 on and off, with a delay in-between.
You can turn the individual pins on (and off) by writing a 1 (0) to the PORT register. The syntax is just like above with the DDR -- PORTD = _BV(PD4) sets the fourth pin in port D to 1.
The _delay_ms() function then waits for a bit. It may not be quite 1ms, though. Depending on what clock speed your chip is set at, it may be a lot faster. The timing's not critical here, so let's overlook that for now.
Finally, PORTD &= ~_BV(PD4); turns pin PD4 off without affecting the rest of the values in PORTD. Let's look in detail at how it does it.
_BV(PD4) creates a binary number with the 4th bit (from the right) as a 1 -- 00001000. "~" is the logical complement operator. It turns the 00001000 into 11110111. "&" is the bitwise "and" operator. It compares two bits, and returns a 1 if and only if both bits are 1. If either is 0, it returns a 0.
The "&=" in PORTD &= ~_BV(PD4) is a very common shortcut. It stands for PORTD = PORTD & ~_BV(PD4). This last command compares the current value of PORTD (00001000) and the value (11110111) described above. The zero in the 4th place in ~_BV(PD4), when used with the & operator, always makes the 4th bit of the result = 0, effectively turning off bit PD4.
The 1s in the rest of ~_BV(PD4) make it so that the & operator doesn't clobber the rest of the contents of PORTD. Since the & returns 1 only if both inputs are 1, the remaining bits in PORTD are re-assigned whatever value they already have -- leaving them unchanged.
Could we have set PORTD = 0? Sure. Since we're only using the one pin in D, it would turn it off just fine. But it would have the side-effect of turning off _all_ the pins on port D, and it wouldn't have provided such a nice example. The bit-masking techniques ( &= ~_BV() and its opposite, |= _BV() ) are pretty useful to learn for chip-level programming.
The last part of the code, return(0), never gets reached. The while(1) command ensures that the chip is always going to be stuck in the while loop. I just included the return() command because the compiler complained when I didn't include it -- the main C function in a program is always supposed to have a return value, even if the chip will never get there.
That's a lot of programming for one day. Take some time to admire the simple beauties of the flashing LED.
Step 11: The End & Web Resources
So there you go -- your first AVR application.
I'll post up some more tutorials and code showing off different aspects of the ATtiny2313. At least one for the Analog/Digital converter, some on Timers, and one for using its built-in serial interface.
In the meantime, if you want to learn more about the AVR chips, here's some good web resources:
AVR Freaks is the motherlode: A community of friendly users with a forum.
Cornell's EE476 class webpage is a tremendous source for info, and their final projects are a treasure-trove of crazy, cool project ideas, all well-documented.
Psychogenic.com is good for AVR & Linux.
The AVR-libC demo program is not a bad one to learn from either, but maybe a bit advanced if you're just beginning with microprocessors or C.
Just found Building a USB Sensor Interface which has good instructions, and heads in the direction of USB connectivity.
Dec 2 Update: If you run Linux with KDE and want an integrated GUI environment, have a look at KontrollerLab. It's very similar-looking to AVRs AVR Studio for Windows.
Jan 2, 2007: AVR Tutor has a short getting-started-with-AVR tutorial written in C. AVR Beginners has a very complete tutorial, but it's based on assembly language instead of C, so I'd consider it an advanced tutorial. You might consider looking at them in order.
The photo is of me using the ghetto dev kit to run a 2400 baud wireless transmitter. Takes only a couple minutes to run a few wires to the breadboard, and off you go!
Participated in the
The Instructables Book Contest
1 Person Made This Project!
- banman11 made it!