AVRDUDE is a small tool for programming AVR microcontrollers. It allows reading and writing flash memory and EEPROM, and programming the fuse bits and lock bits. It is a console application, so it may cause some problems for those just starting out with microcontrollers. All options and parameters are set from the command line, which forces us to remember (or at least have at hand) all the u201cswitchesu201d. The AVRDUDE documentation is quite substantial and you can find various descriptions on many forums, but sifting through the documentation can be troublesome. Wanting to program (usually) the same type of microcontroller with the same programmer that I have on my desk, it is more convenient to rely on notes. To make the fun easier, I prepared a set of the most commonly used commands, which I simply copy into the console window.
I most often use the ATmega8 and the USBASP programmer, so the u201cready-madeu201d commands below will work only for such a set. Changing the programmer will only involve setting the u201c-cu201d and u201c-Pu201d parameters appropriately.
Warning: before you start changing the fuse bits, think three times, because you can lock the chip for sure and it will then be necessary to use a special programmer.
Thatu2019s enough by way of introduction u2013 now weu2019ll get down to specifics 🙂 AVRDUDE does not require installation u2013 after downloading the application from the website it is enough to unpack it and copy the avrdude.exe file to a chosen directory. In my case it is u201cC:avrdudeu201d.
1. Connection test:
avrdude -p atmega8 -c usbasp -P usb -t
If the program displays u201cDevice signatureu201d, then most likely everything is fine.
2. Reading the ATmega8 flash memory contents and saving to a BIN file:
avrdude -p m8 -P usb -c usbasp -U flash:r:plik2222.bin:r
3. Reading the ATmega8 flash memory contents and saving to a HEX file:
avrdude -p m8 -P usb -c usbasp -U flash:r:plik3333.hex:i
4. Writing the HEX file to flash, i.e. the actual programming:
avrdude -p m8 -c usbasp -P usb -U flash:w:plik4444.hex
5. Reading the fuse bits and saving them to the u201cfuse_highu201d and u201cfuse_lowu201d files:
avrdude -p m8 -c usbasp -P usb -U hfuse:r:fuse_high.bin:b -U lfuse:r:fuse_low.bin:b
6. Write the ATmega8 fuse bits for an 8 MHz or 16 MHz crystal:
avrdude -p m8 -c usbasp -P usb -U lfuse:w:0xFF:m -U hfuse:w:0xC9:m
7. Write the ATmega8 fuse bits for the internal 1 MHz oscillator (default settings):
avrdude -p m8 -c usbasp -P usb -U lfuse:w:0xE1:m -U hfuse:w:0xD9:m
The above commands will allow you to program the chip and should be enough to start with. If you want to know a little more, keep reading.
The nightmare of the fuse bits
I donu2019t know about you, dear reader, but for me the biggest problem when starting out with AVR microcontrollers were the fuse bits. You can imagine them as 16 switches (the lower eight and the upper eight) with which we set various options of the chip. The fact that in the microcontroller documentation a programmed bit is marked as 0 and an unprogrammed bit as 1 does not make the task easier. I will not discuss all the bits in detail, because that is thoroughly presented in the documentation, and I will focus only on the most commonly used ones.

RSTDISBL
With this bit we configure whether the PC6 pin is to be set as RESET or as a regular I/O port. We donu2019t change this, unless we want to deprive ourselves of the ability to program the chip via the most popular SPI, because SPI programming requires an available RESET.
SPIEN
Enables programming via SPI. We donu2019t touch this either.
CKOPT
The operating mode of the crystal oscillator. Programmed (0) sets the u201cfull rail-to-railu201d mode dedicated for operation in a noisy environment and for operation at frequencies above 8 MHz. We will use this option.
CKSEL3,CKSEL2,CKSEL1,CKSEL0
With these bits we set the clock source. In the table below I have presented the most commonly used settings.
|
CKSEL3 |
CKSEL2 |
CKSEL1 |
CKSEL0 |
Clock source |
|
1 |
1 |
1 |
1 |
Crystal oscillator from 3 to 16 MHz inclusive |
|
1 |
0 |
1 |
1 |
Crystal oscillator from 0.4 to 0.9 MHz |
|
1 |
1 |
0 |
1 |
Crystal oscillator from 0.9 to 3 MHz |
|
0 |
0 |
0 |
1 |
Internal RC oscillator, 1 MHz clock (default) |
|
0 |
0 |
0 |
0 |
External clock source |
And what if something goes wrong?
The worst thing (besides damaging the electronics, of course) is changing one of the bits responsible for programming via SPI (RSTDISBL, SPIEN). In such a case it will not be possible to program the chip via the interface of popular programmers. Then it will be necessary to recover the chip via an external programmer.
In writing this post I made an effort to ensure that the information contained here is as simple as possible, and at the same time reliable and useful. If that is not the case, please get in touch.