Compile and upload, set fuse bits

For compilation and uploading, it is best to use GNU Make and a Makefile. An example Makefile is given.

To compile the code, there are many not so straightforward steps to be taken. The simplest is certainly to just take this Makefile and trust whatever you're getting.

How to use the makefile

Put the Makefile with filename `Makefile' or `makefile' in the same directory as the C code file (.c).

You can either compile first or directly enter the uploading target. To compile, you call make with the file you want to have generated. That is your filename with a `.hex' ending.

Example: If your source file is called `myfile.c', change to the directory with the source file and the Makefile and call the command

make myfile.hex

Watch for any error output. If succesful, a file `myfile.hex' will have appeared in the same directory. The whole chain of .c -> .o -> .elf -> .hex will be run automatically.

To upload the program to the chip, connect the chip via the ISP to the parallel port, supply the chip with power and call

make myfile.up

No file is generated for this target -- it is a dummy target to upload a file. Watch what avrdude is saying. If succesful, your ATMEGA should immediately start running your program. The whole chain of .c -> .o -> .elf -> .hex -> upload will be run automatically, as necessary.

Note that on some systems, you might have to call (as root)

modprobe ppdev

once after each reboot to be able to use the parallel port. The command loads the kernel module ppdev. If your kernel, however, has no parallel port support in it, you have to get a new kernel (distribution's binary package or compiled from source).

Set fuse bits

BE CAREFUL WITH THE FUSE BITS -- You might end up putting your ATMEGA in a lock state. Be sure you have read the datasheet.

An ATMEGA has two bytes of fuse bits, which determine a couple of very basic configurations. The main reason for me to set the fuse bits is to enable the external quartz oscillator.

Figuring out the fuse bits is a matter of binary logic and making sense of the datasheet. The Makefile above illustrates how to write the fuse bits using avrdude:

avrdude -P /dev/parport0 -p m8 -c stk200 -U hfuse:w:0xd9:m -U lfuse:w:0xe1:m

This command uses our "stk200" ISP to write to an ATMEGA8 (`m8'). `-U' means Upload, hfuse means the higher fuse byte, lfuse the lower. `:w:0xHH:m' means to write a byte to memory, where HH is replaced by the hexadecimal number to write.

In the same manner you can read the fuse bits -- see in the makefile above.

Get intelligent about it all with the command

man avrdude

For the ATMEGA8, 0xc9ef (h = 0xc9, l = 0xef) enables a 4 MHz external oscillator, where 0xd9e1 is the factory default using the internal RLC oscillator, as given in the datasheet.

1.9 kb Makefile 2010-08-13 03:03
[Next section]
login 2010-08-13 03:03