As mentioned before, fuses are used to set a couple of things that might need to be set before the actual software starts to run. As a beginner you should not tinker with most of those settings, because it is possible to lock yourself out of in-system programming.
The most dangerous fuse bits in that respect are SPIEN (SPI enable) for obvious reasons and RSTDISBL (reset disable). The latter disables the reset pin and converts it to a normal port pin. But without external reset, you can not enable ISP mode.
The most interesting bits for beginners will be the CKSEL bits. They are used to select the source of the system clock (internal / external oscillator; crystal / RC oscillator - see data sheet). Also, for some AVR there is a CKDIV8 fuse, which divides the system clock by 8.
Be aware that there is another potential pitfall when setting fuses, though. As stated before, the system clock is needed to successfully perform in-system programming. So if you select a non-working clock source (e.g. an external crystal, which isn't there), ISP won't work anymore. The controller's fuses in that case can't be changed anymore and you have locked yourself out. It might be possible to "revive" it by attaching a crystal in this example. High voltage programming would also work (if you have a programmer for this), because for HV programming uses its own clock.
One thing that is confusing about the fuses is that they are “1” when not active. It is really difficult to determine which bits should be set and which shouldn’t. Fortunately, there are tools to help you. AVRDUDESS e.g. points you towards http://www.engbedded.com/fusecalc, an online calculator for the fuse settings. It is really comfortable, because you can determine the fuse bytes by entering the configuration that you want.
Another online fuse calculator can be found here: http://www.frank-zhao.com/fusecalc/
Also, the AVR Studio from Atmel has a similar function.
Setting the fuses is really simple. Set all programmer options as before, but leave the Flash section empty.
Start by reading the fuses like this:
Note the default fuse settings for the ATMega8: Low fuse: 0xe1, high fuse 0xd9. Let us assume that the only change we want to make is set the clock frequency from “internal RC oscillator 1MHz” to 4 MHz. Then the new setting for the low fuse is 0xe3. Enter this value in the corresponding field, activate the field “Set fuse” and press Start!
So now let's see what happens, if things go wrong. Let's start with a wrong MCU setting (here: setting is ATtiny13, but controller is ATMega8:
avrdude.exe: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.02s
avrdude.exe: Device signature = 0x1e9307
avrdude.exe: Expected signature for ATtiny13 is 1E 90 07
Double check chip, or use -F to override this check.
Ah! The wrong device signature is detected, no data is written. Check device setting.
WARNING: Never ever use the –F option unless you know exactly what you are doing!
If reading the signature fails, something is wrong with your set-up. Fix it first! Using the –F option can make your controller unusable. Potential causes for a mismatch of the signature can be:
- Problems with your ISP wiring
- Wrong chip selected
- Wrong SCK frequency selected
- Power supply unstable or too low
- System clock unstable
Next, we disconnect the target hardware alltogether:
avrdude.exe: error: programm enable: target doesn't answer. 1
avrdude.exe: initialization failed, rc=-1
Double check connections and try again, or use -F to override this check.
So the programmer finds no target. Check ISP wires, including RESET wire, check power supply, check that the controller sits well in its socket.
And finally we disconnect the programmer:
avrdude.exe: error: could not find USB device "USBasp" with vid=0x16c0 pid=0x5dc
Fair enough, no programmer found! Check driver installation (device manager), check the USB connection.
And this concludes this tutorial. If you have any suggestions as to how this tutorial can be improved, do let me know.