about the AVR microcontroller:
AVR is one of the most popular, most powerful 8-but microcontroller systems. There is a wide family
of chips, but the two IC's I use most frequently are the ATMega16 and ATMega8. They're available in
40 and 28 pin DIP packages respectively, and cost around $4. DIP packages are nice because they're
easily socketed; starting out, you want mistakes to be as inexpensive as possible.
These chips contain ~16k of flash memory, an on-board oscillator, 1kb of SRAM, IO ports, integrated
10-bi§t analog to digital conversion, a hardware serial UART, interrupts, and lots of other
perhiphrial features.
Other big advantages include a low power consumption; 1 instruction per cycle execution (meaning
1mhz gets you ~1MIPS), and 32 general purpose hardware registers. Your C programs will be quite
happy living there.
The AVR chips don't require an external programmer; you can program them in-circuit if you have a
port and the right programming tool. This lets you upgrade your programs without removing the IC,
and is very handy for developing. They also support bootloading - with the right software, you can
program your circuit over a serial port, no programmer required at all!
programming kits & tools:
The best kit to purchase if you are getting started with AVR development is Atmel's STK500.
For a portable programmer, or if you just want to experiment with a board like the xMega, the
Atmel AVRISP is perfect. With RoHS/lead-free coming up, these kits might become is short supply -
the AVRISP is already out of stock in many places. We're working on our own ISP tool. Both these
boards can directly program the xMega through their ISP header.
The xMega was inspired by the STK500; I'd develop small boards that piggybacked off the STK500
and then realize I had to do another board to use them in a permanant installation. Rather
than spend time developing things twice, the xMega was born.
|
what do I need to get started?
Getting started with AVR programming is very simple. At a minimum, you need:
-
A programmer. The STK500 is the best choice to get started fast. The STK comes with the software
you need to load your compiled program onto the chip. It also comes with a IC, cables, and a set of
very handy jumper wires. You'll need to supply +12V @ 1A, so this means a trip to radio shack to get a
wall adapter. The STK is best ordered from
Digikey (pt.# STK500-ND)
- If you're having problems locating a programming kit, consider something like the xMega board with the ability to self-program with examples ready to go.
- A compiler. The most common is WinAVR, based on GCC. It comes with everything you need.
If you're using mac or windows, there are AVR distributions for these as well.
Things that you probably should pick up at a minimum:
- A good quality multimeter. Any will do, but I've very partial to the Fluke 110 series. This is your
basic tool for debugging problems. Mouser carries fluke hardware.
- A DC power supply with a regulated current output. You can find these for $50-100. This will let you
limit the current going to your projects - it helps when testing a board, and helps prevent the magic
smoke from escaping the ICs. BGMicro has a nice one, PWR1166.
- A prototype board. This is handy for prototyping small circuits and general experiments. BGMicro has an inexpensive good quality ones.
Things that you probably DON'T need (right away):
- An oscilloscope. This is a excellent tool to have, but if you're working from known good schematics
and hardware, you can build projects without one. They're handy when building new boards. The high
cost of a scope doesn't justify the utility at least initially.
- A function generator. This is another nice tool, but you can make due without one.
|
|
installing development software:
There's two major pieces of software you need installed to get started.
The first program comes with your STK; AVR-Studio. It's best to install the version that comes with the
development kit at first. Newer versions may require you to upgrade the firmware on the STK board.
This isn't anything to be scared of, but it does introduce another layer of complexity to the process.
This software lets you load compiled programs onto the microcontroller.
The second program you need is the compiler, AVR-GCC. If you have windows, download the WinAVR distribution. If you're using linux, I'm assuming you can get the software installed OK from the avrgcc sourceforge
project site.. Once you have WinAVR
installed, it will put it's programs in your path. From there, you can open a cmd window (start:run:cmd.exe)
and go to the directory containing the program. Type 'make' and the program will run the
makefile and compile your program.
There are IDEs and such to automate this procedure; I still
do it the old-fashioned way. Have a look at AVRfreaks.net for some tutorials on
how to get these up and running.
If you're building a program for Windows, you would compile to an .exe. Since we're compiling for
an embedded system, you get a file that's ready to be loaded into the microcontroller's memory.
This is the .hex file. This .hex file is what you load into AVR studio's programming tool.
how do I use the board?
Once you've read the manual that comes with the STK kit, what next?
The next step is to confirm that the AVR is set up properly. When an AVR leaves the factory,
it has a set of internal fuses that configure it's behaviour. You'll need to change these for
most applications. In order to do this, you need to install your chip.
- Install a ATMega16 chip into SCKT3100A3. The examples below are compiled for this IC.
- Make sure the STK500 is connected to a suitable power source, and the LED is on.
- Connect your PC to the STK500's serial port. Make sure to use the "CTRL" header.
- Connect up the 6 pin programming header. See the picture below.
- Confirm that AVR studio can see your STK properly. Open the 'AVR' icon and confirm it's detected.
Your setup should look more or less like what you see here. Remember; you'll need to find an
approriate 12V 1A wall adapter as this is not included with the kit. They do include leads, if
you have access to a standalone DC power supply.
|
You should see something like this when you click on the AVR icon:
Next, confirm that your STK500 is set to generate a clock signal for the board. Make sure that the
"clocksel" jumper is set to the on-board SW clock system. Go to the board configuation window
and set the on-board oscillator to 3.68Mhz. This will generate the proper speed setting for the
examples below.
If you need to cheat, have a look at the back of the STK500. You'll find a silkscreened set of
reference drawings showing what the various DIP switches should be set to do. These guys
thought of everything. They even show you the default jumper locations. :-)
|
No, clicking the little AVR icon will put the chip in
programming mode, then open up the fuses tab. We'll configure the new ATMega16 correctly to work
with the example software.
The IC uses onboard fuses to configure some of it's hardware functions.
Don't worry; these can be reset as many times as you like. Make sure that the JTAG interface
is disabled, and the chip is set to use an external oscillator on the STK. The other settings
are Ok. AVR chips ship from atmel with the JTAG interface enabled, and set to use their on-board
oscillator. This can cause problems if you don't remember to set them for your application.
|
You should now have a functional microcontroller, and a working setup to program the board.
To program the examples, you'll need to put AVR studio in in programming mode and then take the .hex
file included in the zip file and program it to the STK.
During this step, you may see a communications error dialog. If you do, power cycle your STK
and then click on the "erase" button to clear the microcontroller's flash memory, then try
to program again.
quick-start gcc examples:
Some time ago, WinAVR shipped with a set of 10 or so example applications that were ready to go.
These were deprecated over time, so I built up some more the more common ones to help get you started.
You'll need to make sure that the STK board is configured up correctly first, though:
- Confirm that you have the "PORT B" header connected to the "LED" header.
- Confirm that you have the "PORT C" header connected to the "SWITCH" header.
- Use a 2-wire jumper to connect PortD0/D1 to the RS-232 RX-TX header. The orientation of the wire
is important - see zoomed-in picture.
- Try to program the "flash LEDs" example .hex file. You should see the LED's flash on and off.
Flashing lights is interesting and useful, but one of the best capabilities the AVR has is the
ease of doing serial communications. This lets you network your AVR's together, speak to
a PC, log data - all sorts of things.
Serial communications are dependant on knowing the frequency that the AVR is operating at. In
order to use the pre-compiled examples, you need to make sure your AVR is set up properly.
Double check the fuse settings to make sure that the AVR is set to use an external oscillator,
and that the STK board is set to generate a frequency of 3.68Mhz. Confirm that the D0/D1 pins
are connected to RX/TX respectively as shown above.
Program your board with the "send a string" example. You should see the famous "Hello World"
appear on a terminal program connected to the STK at 9600 8N1. If you don't have a seperate
computer, or two serial ports in your PC, you will need to disconnect the STK500 and then close
avr studio before you can use the serial communnication features. This is annoying, to say the
least, so consider investing early on in a second serial port.
Your PC should be wired to the RS232 connector marked "SPARE".
If you program the serial example, the program will display the ASCII value of the
character you send to the LEDs. Try counting up from 0-9 on your keyboard to see the
progression of the ASCII values in binary.
If nothing happens when you run the serial example, confirm that the STK jumper wire from PORTD0/D1
to the RX/TX header is correct. In my example setup, this is the yellow and green pair of wires.
The jumper should be set so that PORTD0 (the first pin) goes to the RX pin.
example code downloads:
Example when running the "switch" demo example; pressing the button will light the corresponding LED.

forums:
The best forum to get started on is AVRFreaks.net. There's lots of projects to look at,
lots of help files, and a community of - as you might guess - AVR freaks.
specification sheets:
The biggest resource, and most overlooked starting point - is the specification sheet for the AVRs themselves.
This is a ~170 page reference guide covering the chip's capabilities and background in minute detail.
It's well written, and is a must-read for becoming framiliar with the advantages and limitations of the
AVR microcontroller. I recommend printing out the Mega16 guide and having it in a binder as it will
become a constant reference when working with these chips.
Before asking for help, make sure to read the relevant sections of the spec sheet thoroughly. Most
common questions are addressed, and you'll need to check to here to figure out the nuances of the
avr's configuration registers.
If you're new to the embedded game, specification sheets often have very educational and handy examples
listed towards the back. It's a great way to come up with ideas and applications. Have a look at my chip guide for some good examples.

AVR Mega16 Specification Sheet (pdf)
magazines:
Once you get started, there's lots of magazines dedicated to the electronics hobby - and these days,
that usually means microcontroller and embedded projects. The ones I read regularly and contriubte to
are Circuit Cellar and Nuts and Volts.
building successful projects:
Developing on an embedded platform is a different, and very rewarding experience when you've built a
project that works well from the ground up. It can be equally as frustrating and maddening when things
don't go well. To that end, here are some helpful tips:
Be patient. Odds are, what you are doing WILL eventually work. One thing to remember when things aren't
going well; ANYTHING could be wrong. Software, hardware, timings, specifications, power, you name it.
So you need to allow for more time debugging and embedded system than you would a PC program.
Milestone frequently and make known-good code. Start with examples here and elsewhere on the web before
building your own projects from scratch. Work from "known good" schematics and designs to help remove
some of the doubt as to why a project isn't running as it should. When you make headway, write down
how things were configured and make a seperate directory for that code. When things go bad later, you
can work back and confirm that old pieces still work.
Keeping "known good" reference designs around can greatly speed development; it helps to know that your
basic communications and power systems are working as you expect. The xMega board came about from
frustration in working with one-off project prototyping.
As soon as you're comfortable with an idea, consider doing a dedicated circuit board, even for small
prototypes. A lot of problems can be caused by loose wires and intermittant connections. These will
go away when you move to a circuit board. With a quick turn of prototype boards in the $60 range,
how much is your frustration worth?
A great combination for rapid development is an xMega board connected to an inexpensive PCB via a
ribbon cable. ExpressPCB's $51/3 mini-board service is ideal
and I highly recommend it. With the AVR on it's own board with a power system, there's lots of room
to connect sensors, buffering logic, relays, etc.
need help? comments?
email: avrhelp@xdesignlabs.com and I'll see what I can do. If this tutorial was any help, or you'd like
to offer a suggestion, please, contact me.