Ghetto AVR Programmer

Getting started with AVR microcontrollers on the cheap By Elliot Williams

Microcontrollers are so cheap these days. If only there were a way to program them up just as cheaply….

In this Instructable, find out how to build up a complete AVR microcontroller 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 with the help of servo-based 4-legged walkers: www.instructables.com/id/EYLB2BCA9BEUGCWIZL.

Note: I’ve gone USB! www.instructables.com/id/EDRQZ56F5LD8KDX is a better picture of what I’m using now, and is a real improvement because it will work with computers that don’t have parallel ports. That said, the basics here are still applicable, especially all the software details.

The endpoint is not quite as swanky as the suite from Atmel (the maker of the AVR microcontroller), but it’s gonna run you about $150 less and takes 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. Figure A shows the completed project.

1. Go order parts!

Required parts list (stuff to buy or scrounge):

image Atmel ATtiny2313 chip: $2.50-$3

image Socket for chip: $1

image Parallel port connector (DB25): $3 at RadioShack or $0.95 at www.sparkfun.com

image Header pins: $1-$2 (at least one strip each male and female, maybe 2 female)

image Some LEDs and resistors: $1

Image

Figure A: The Ghetto AVR Programmer

Total cost, around $10, even if you go deluxe. Even less if you can find an old parallel port cable.

Add-ons:

image A pushbutton switch or two: $1

image A piezo speaker: $1-$5

image A few light sensitive photocells: $2-$6

image Breadboard for making complex circuits: $8-$10

Other stuff you oughta have:

image Computer: the older, the better because it needs a parallel port

image Hookup wire, solder, soldering iron

image Super-duper glue

image Source of ~5V DC: batteries will work, and old computer power supplies are perfect.

I get a lot of stuff from SparkFun Electronics because they’re fast, reliable, and fairly priced. They carry AVR chips and have everything on the “required” list in one place and a nice website to boot.

If you’re gonna be ordering a lot of chips, you can get a deal from Digi-Key or similar. For instance, you can find the ATtiny2313 (you need the DIP package form factor) for $2.26 each: http://search.digikey.com/scripts/DkSearch/dksus.dll?KeyWords=ATtiny2313+dip.

Go order the parts now, and let’s set up the software side while you wait.

The waiting is the hardest part.

2. Get and install software: Linux version

If you run Windows, skip this step. 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

Ubuntu and other Linux flavors has packages for the toolchain. To install it, run the command:

sudo apt-get install avrdude

avr-libc binutils-avr gcc-avr

Bam! The versions that come with Ubuntu 8.04 are good all around. Woot Ubuntu! For other Linux versions, check out the documentation at the AVRFreaks Wiki: www.avrfreaks.net/wiki/index.php/Documentation:AVR_GCC.

The following script sets up your parallel port so that you don’t need to be the superuser to use it. If you’re Linux-savvy, you can add the same commands to a boot script to enable the parallel port every time you boot up. Otherwise, just run it by hand:

sudo modprobe parport

sudo modprobe ppdev

sudo chmod 666 /dev/parport0

3. Get and install software: Windows version

The Microsoft Windows software toolchain is based on the same compiler as Linux, but with some optional extra GUI friendliness.

WinAVR (http://sourceforge.net/projects/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:winAVRininstall_giveio.bat by double-clicking on it. A window will flash open and close, and then you’re done.

4. Make a programmer cable

The cable you’re going to make is a “Direct AVR Parallel Access” or DAPA cable.

The web site www.captain.at/electronics/atmelprogrammer has a nice schematic of the parallel port pins for your reference.

Here are the pins I used:

Table 1

Parallel Port   

AVR Function   

Color

2

MOSI

Orange/Grey

11

MISO

Orange

1

SCK

Green

16

RESET

Brown

18

GND

Brown/Grey

Note: That pin 1 (SCK) is on the upper-right hand side when you are looking at the solder pins from the back (Figure C). It’s upper-left when you’re looking at it in Figure B and in the schematic at the previously mentioned link.

Also, the schematic 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.

Note: Sometimes you’ll find yourself programming these chips in a working circuit. In that case, you’ll want to make sure that there isn’t any way that higher voltage in the device can make its way to your computer. So if you have a robot with a 12V motor drive, double-triple check that the motor power lines don’t connect to any of the programming pins. But if you’re programming in the cradle, you’ll be fine.

Image

Figure B: 1. Pin one goes here 2. Top to bottom: MOSI, MISO, SCK, RESET, GND

Image

Figure C: 1. Pin one: Reset

Image

Figure D: 1. GND 2. MOSI 3. 5V (top) and ground (bottom) 4. These connect directly to the AVR pins to make them easily accessible later 5. Socket for AVR

Image

Figure E: 1. Clock 2. +5V 3. Reset 4. MISO 5. MOSI 6. Ground

5. Make the programming cradle

Now use the 20-pin socket and female headers to make a cradle that connects the pins from the cable to the corresponding pins on the chip.

The first thing to do is superglue all the headers and the socket to the circuit board. That way, it’s easier to solder it all together. Figure D shows the sockets from above, and the solder joints are shown in Figure E. 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:

Table 2

Cable          ATtiny2313 Pin
MOSI 17
MISO 18
SCK 19
RESET 1
GND 10

Consult the ATtiny2313 datasheet at www.atmel.com/dyn/resources/prod_documents/2543S.pdf to double-check the pinouts.

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. It just goes pin to pin, and then connect all the header pins to the closest pins on the socket. Fortunately for me, a pre-printed circuit board from Radio Shack did the trick. The cradle is versatile too. Figure F shows my ATtiny13 programmer cradle to the right of the one we’re building in this Instructable.

Image

Figure F: Cradles for the ATtiny2313 (left) and ATtiny13 (right)

6. Make some Blinkenlights

An integral part of the Ghetto Development Environment is the plug-in LED (see Figure G). 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Ω) 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 / 150Ω = 24mA, which is just about right. Make a few; I made eight. You only need one here.

7. Set up the programming project

In Linux and Windows, the procedure for starting a new project is basically the same:

1) Make a new directory called LED_Demo

2) Copy the Makefile (www.makezine.com/go/avrprogmakefile) into that directory

3) Download the code, LED_Demo.c (www.makezine.com/go/avrprogdemoc), 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.

Image

Figure G: Four blinkenlights

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 the line that has “lpt1” by placing a # in front of that line.

8. Set up your first demo circuit

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) by pressing flat against a table so that they’re all even and parallel as shown in Figure H. Then it fits in the socket nicely as shown in Figure I.

Notice the alignment of the chip. The little dot marks pin 1. It must go to pin 1 on the cradle.

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 (see Figure I).

9. Program the chip

If you’re using Linux:

1) cd to the LED_Demo directory

2) Cross your fingers

3) Type “make”

Windows:

1) Open up LED_Demo.c with Programmer’s Notepad.

Image

Figure H: Gently straightening the pins

2) Cross your fingers.

3) Select “Tools…Make Program”

On both Linux and Windows:

Watch the log as the text goes scrolling by. There are two things you’re looking out for here:

1) Did the program compile successfully? If there are no errors, it did. If it didn’t, why? Did you make changes to the original code?

2) Did the chip program successfully? In this step, the computer writes the code to the chip, then verifies the chip’s memory. It should say “Contents Verified.” If it says something about the parallel port or “giveio.sys”, confirm that you enabled the parallel port back a couple steps ago.

Success? Yay! There’s nothing like the sweet smell of blinking LEDs in the morning.

10. Understanding 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 that make life easier. You’re always going to want to include it.

An AVR 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.

Image

Figure I: 1. A small notch and round dot mark pin one 2. Pin eight, Port D4 3. Pin ten, Ground

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 variable, i, and converts its value 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 roughly 1 millisecond (ms). 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:

1) _BV(PD4) creates a binary number with the 4th bit (from the right) as a 1 (00001000).

Image

Figure J: My 2400 baud modem

2) ~ is the logical complement operator. It turns the 00001000 into 11110111.

3) & 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.

11. The end and web resources

So there you go, your first AVR application. If you want to learn more about the AVR chips, here are some good web resources:

image AVR Freaks (www.avrfreaks.net) is the motherlode: a community of friendly users with a forum.

image Cornell’s EE476 class webpage (http://instruct1. cit.cornell.edu/courses/ee476) is a tremendous source for info, and their final projects (http://instruct1.cit.cornell.edu/courses/ee476/FinalProjects) are a treasure-trove of crazy, cool project ideas, all well-documented.

image Psychogenic.com (http://electrons.psychogenic.com/modules/arms/sec/1/AVR) is good for AVR programming on Linux.

image The AVR-libC demo program (www.nongnu.org/avr-libc) is not a bad one to learn from either, but maybe a bit advanced if you’re just beginning with microcontrollers or C.

image www.sensorwiki.org/index.php/Building_a_USB_sensor_interface has good instructions, and heads in the direction of USB connectivity.

image If you run Linux with KDE and want an integrated GUI environment, have a look at KontrollerLab (www.cadmaniac.org/projectMain.php?projectName=kontrollerlab). It’s very similar-looking to Atmel’s AVR Studio (www.atmel.com/avrstudio/) for Windows.

image www.avrtutor.com has a short getting-startedwith-AVR tutorial written in C. www.avrbeginners.net has a 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.

Figure J shows 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!

By day, Elliot Williams is a mild-mannered government economist. By night he’s a hacker, alchemist, professor, roboticist, bartender, skater, and charter member of HacDC, a Washington, DC, tech cooperative.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset