Elliot’s project-oriented book lets you start either with an AVR-powered Arduino that you probably already have in your toolbox, or with a bare AVR chip and programmer. To get a taste of the book, check out the excerpt we’ve posted. Start reading the first couple pages below, and download Make: AVR Programming Chapter 2, Programming AVRs when you’re ready to read the rest.
In this chapter, you’ll get set up with everything you need for coding, compiling, and flashing your programs into the bare silicon of the AVR chips that are sitting on your desk right now. To do so, you’re going to need some hardware (a flash programmer) and some software (a code editor, C compiler, and the program that’ll communicate with the hardware flash programmer). Finally, you’ll need to hook up some wires from the programmer to the AVR chip and get set up with a power supply.
In this process, there are a lot of different approaches that will get you to the top of the same mountain. Ultimately, the different approaches are all basically the same at some abstract level, but we’ll step through some details of a few of the most popular options to make things clearer.
On the hardware side, most of the flash programmers work about the same, and the differences there won’t amount to much more than a few tweaks to a file that you’ll use over and over again. Flash programmers, after all, are just USB devices that send bytes of your code across to the AVR chip. On the software side, different development packages will have different looks and feels, but in the end it all comes down to editing code, compiling it, and then sending it off to the hardware programmer.
What You Need
For this chapter, you’ll just need the following:
- A solderless breadboard.
- Wire jumpers to plug in to the breadboard.
- An ISP programmer.
- An ATmega168, 168A, 168P, or 168PA.
- An LED (any color) and an appropriately sized resistor: 200–500 ohms.
- A source of 5 V DC power (if not supplied by your ISP); a 4xAA battery pack is
- One 100 nF (0.1 μF) capacitor to smooth out the AVR’s power supply.
Programming the AVR
The words “program,” “programmer,” and “programming” are overloaded in the microcontroller world. We (as programmers) write programs, compile them, and then use a flash programmer to program the AVRs, which then runs our program. Pshwew! Let’s step through the actual procedure and see what’s actually going on.
It’s a long and winding road from the code you type into your editor to a chip on your desk that turns a light on and off. Getting from typed letters on a computer screen to a working piece of electronic machinery requires a chain of tools called, predictably, a toolchain!
- Write your source code in an editor.
- Turn your source code into machine code with a compiler (and associated software tools).
- Using uploader software on your big computer and a hardware flash programmer, send the machine code to your target AVR chip, which stores the instructions in its nonvolatile flash memory.
- As soon as the flash programmer is done, the AVR chip resets and starts running your code.
Figure 1, “AVR programming toolchain” sketches out the main steps in AVR firmware development along with which tools you’ll use for each step.
The first step in your toolchain is going to be a text editor, or whatever you’re most comfortable writing code in. For the Linux folks out there, gedit is quite nice. On Windows platforms, you’ll probably find the editor that comes with WinAVR, Programmer’s Notepad, will work pretty well, but I prefer the freeware Notepad++. Many Mac coders swear by TextMate. If you’ve already got a favorite code editor, by all means feel free to use it. Nice features to look for include syntax highlighting, automatic formatting and indenting, parenthesis matching, and maybe even code folding. (Put your copy of Microsoft Word away—that’s not what we’re looking for here.)