This is a quick how-to for a low-cost programming solution for Atmel AVR microcontrollers. The following instructions are meant for MacOS X (10.1 or later recommended), although many can be adapted to Linux. OS 9 is (for the most part) incompatible with these instructions.
GNU C Compiler
To create and compile C code for the AVR microcontrollers, I use the GNU gcc compiler. The easiest way of getting the GNU tools is to download my newly updated installer package from my web site. It should work, but if it doesn't work, let me know. Note: The package is completely up to date as of February 3, 2004.
The package contains:
Download: avr_gcc_macos_x.dmg (14.9 MB).
Jaguar (and probably Panther) Users: See note at the end of this section.
GCC can also be downloaded and compiled under MacOS X, although first you must install the developer tools from the Apple CD, which came with OS X, or can be downloaded from the Apple website for free.
A good reference for the AVR GCC tools and the C library can be found at http://www.enteract.com/~rneswold/avr/. However, the instructions for compiling the tools is somewhat outdated. Here is what I use to compile the tools:
First, download the latest version of binutils and gcc or gcc-core (see note) from your favorite gnu mirror (see http://gcc.gnu.org/mirrors.html). Then download the latest avr-libc from http://savannah.nongnu.org/projects/avr-libc/. The first two downloads are quite large, so think twice if you use a modem. Then run the following commands to compile binutils:
tar -xzf binutils-18.104.22.168.tar.gz (or whatever you downloaded, if not already decompressed)
cd binutils-22.214.171.124 (or whatever you decompressed)
sudo make install (you'll have to type in your password to enable root access)
To compile gcc:
tar -xzf gcc-core-3.2.1.tar.gz (or whatever you downloaded)
cd gcc-3.2.1 (or whatever you decompressed)
./configure --target=avr --enable-languages=c
make CC="cc -no-cpp-precomp"
sudo make install
To compile avr-libc:
tar -xzf avr-libc-20020203 (or whatever you downloaded)
cd avr-libc-20020203 (or whatever you decompressed)
sudo ./domake install
- With Jaguar (OS 10.2), the path /usr/local/bin isn't automatically added to the search paths, so add the following to your .cshrc file or create a .cshrc file with:
set path = ( $path /usr/local/bin )
You'll need to do this before compiling any of the tools.
- With versions later than 3.0.3, the options to make when building are necessary for gcc to compile right. You should also make sure you get the latest developer tools from Apple.
- Developer tools before July 2002 or so requre GCC 3.2.x or earlier. Newer versions (cvs as of now) need GCC 3.3 or later.
- Many warnings are typical when compiling the tools. Don't worry, unless you get a fatal error (something that stops the compile).
- I haven't experienced this problem myself, but I've gotten two complaints saying that avr-gcc cannot find the library libiconv.2.dylib in /usr/local/lib. It may be found in the /lib/ directory and be copied into /usr/local/lib. This may or may not work for you.
To edit AVR C code, I recommend using Emacs, the standard UNIX text editor. Although it takes a bit of time to get used to, it has many keyboard shortcuts, syntax highlighting, auto-tabbing and other features. Or you could just use any old text editor, or Codewarrior (if you have it).
You can get the latest Aqua Emacs from http://www.porkrind.org/emacs/. Or for Jaguar (10.3), try http://mindlube.com/products/emacs/index.html
A page of Emacs key shortcuts can be found at http://www.geek-girl.com/emacs/refcard.html.
My Java programmer is obsolete, although instructions and source are still available.
I recommend using uisp to program AVR chips. uisp is the most capable open-source programmer under Linux. Download the latest version of the source from http://savannah.nongnu.org/download/uisp/. The uisp program is also included in my installer.
To compile the program, run (inside the expanded uisp directory):
sudo make install
I've tested this using Jaguar (10.2.3) and uisp 20021201, and it seems to work fine.
If you type uisp --help (you need to open a new terminal window after compiling), you will get a number of options to use with uisp. My typical use of uisp line is as follows:
uisp -v -dprog=avr910 -dspeed=19200 -dpart=auto --erase --upload --verify if=< file.rom >
To debug microcontroller projects, it is useful to have a serial port on the project and spit out data to the serial port. A terminal program is used on the PC to read in this data. I recommend Zterm (http://homepage.mac.com/dalverson/zterm/) for a graphical interface, or minicom (http://alioth.debian.org/projects/minicom/) for a text-based interface. You can use the same USB to serial adapter for this purpose.
To program the AVR microcontrollers on a Mac, you first need a serial (not parallel) programmer. Several of the AVR STK's come with a serial programmer and it is also possible to build your own programmer. Instructions can be found in Application Note 910 on Atmel's web site (http://www.atmel.com).
Another excellent site to look at for programming tools is http://www.avr1.org.
In addition, Digi-key sells a serial programmer that supports almost all AVR chips (unlike the appnote 910 programmer) for $30. It is part number ATAVRISP-ND. Note that this programmer requires a 115,200 bps serial port to work.
USB Serial Adapter
Since recent Macs don't have serial ports, you need a USB to Serial Adapter, availible from several companies. I recommend one from Keyspan, as they are the only company (as of late 2001) that makes OS X and linux drivers for their products.
I use the Keyspan USB PDA Adapter, because it is cheap (about $39.99). It only works up to 57600 baud, which is fine for the homebrew serial programmer. If you use the Atmel AVRISP, a faster adapter is needed, for instance the Keyspan High Speed USB Serial Adapter.
An alternative to getting a serial programmer and a USB to serial adapter is to make (or buy) a USB programmer. I haven't had any experience with them, but in a single day, I received two emails about these. Try these links:
This simple example uses a microcontroller to blink an LED using timer interrupt based timing. The sample code and procedures are based on the hardware shown in this schematic.
The sample code is written in C (C++ is not really supported on AVR, and is not really useful at this scale). AVR specific commands (in the avr-libc library) are described at http://www.enteract.com/~rneswold/avr/. The sample code is relatively well commented, and should be straightforward to understand.
Since the GNU tools are based on the command line, one must use the Terminal application to compile the code. I recommend using make to simplify the compile process. make uses a makefile (it must be called makefile) that contains the dependencies of various targets and the command needed to make them. An example makefile is included below.
Notes and Future Development
This is an ongoing project. If you have any corrections to the information I provided or suggestions or comments, feel free to email me using my contact page.