I guess many of you were confused when programming AVR fuse bits. I get many newbie questions like “I programmed Atmega8, but it doesn’t work”. Then my standard answer is: “Did you touch configuration bits?” and if yes, then I am almost 90% sure that he did it wrong. Most of them understand wrongly that programmed fuse bit in configuration should be left unchecked and opposite. Let’s take a look at widely used programming software – PonyProg.
First thing you do before programming the chip is set configuration bits (Atmega8 in the picture):
The first attention should be paid to clock sources. Four bits are controlling Atmega8 clock sources: CKSEL0, CKSEL1, CKSEL2, CKSEL3.
As you may already know, Atmega8 can have 4 different clock sources:
- External Crystal or Resonator;
- External Low-Frequency Crystal;
- External RC Oscillator or Calibrated Internal RC Oscillator;
- External Clock source.
In the table you see adequate CKSEL0..3 settings
As you may notice, for most of them, there is a range of settings left.
External Crystal or Resonator
External Crystal or Resonator may be set from 1010 to 1111 (Note that ‘0’ programmed while ‘1’ means unprogrammed). These ranges are left to select microcontroller startup times to stabilize oscillator performance before first instruction.
If you connect external Crystal oscillator or resonator to XTAL1 and XTAL2
There comes another Fuse bit CKOPT. This bit selects two different modes of oscillator amplifier. If CKOPT is programmed (unchecked), then Oscillator oscillates with full rail-to-rail output. This is handy when the environment is noisy or uses XTAL2 to drive another microcontroller or buffer. But on the other hand, this takes more power. If CKOPT is unprogrammed (checked), then the swing is smaller, and power consumption decreases. Typically if the resonator is up to 8MHz, leave CKOPT (checked=unprogrammed) and programmed (unchecked) above 8MHz. In the table you see selectable crystal oscillator modes:
CKSEL0, SUT0, and SUT1 bits, in this case, are used to select startup times of the microcontroller – how many clock cycles to wait before the execution of the first command. These settings are necessary to ensure the stability of ceramic resonators and crystals.
Note 1. These are fast startup times – not suitable for crystals;
Note 2. Most used with ceramic resonators to ensure stable startup. This is OK to use with crystals as well, but the device is not operating at maximum frequency.
So if you want a stable system startup, it is better to leave CKSEL0, SUT0, and SUT1 programmed (unchecked) what means about 65ms startup time.
There is only one bit set for CKSEL0, CKSEL1, CKSEL2, CKSEL3 – 1001 (unchecked, checked, checked, unchecked) For the Low-frequency oscillator. A low-frequency crystal is a typical clock crystal where the frequency is 32768kHz. The main options for this mode are setting crystal startup times with SUT0 and SUT1 fuse bits.
Note 1. Less stable startup.
External RC oscillator
The frequency in this mode is calculated by formula f=1/(3RC) while C should be at least 22pF. If CKOPT is programmed (unchecked), then Internal capacitor 36pF between XTAL1 and GND is enabled, then there is only a resistor needed.
And again the table of operating modes:
And startup times:
Note 1. don’t use this setting when close to the maximum frequencies.
The calibrated internal RC oscillator
Calibrated internal RC oscillator is pretty often used because there is no need for any external components and it can provide fixed 1, 2, 4, and 8 MHz clocks. This mode is selected by programming fuse bits in range 0001 to 0100. Leave CKOPT bit unprogrammed for this mode (checked).
Note 1. by default this option is selected for all Atmega8 And startup option table:
Note 1. by default this option is selected for all Atmega8
About internal RC oscillator calibration read in device datasheet.
External clock – this is the main mistake the newbies are making when starting their projects. To enable an external clock source, you have to leave CKSEL0, CKSEL1, CKSEL2, CKSEL3 bits unprogrammed – this means checked. People make mistakes checking them and thinking that they are enabling external crystal resonators, and then they are wondering why it is not working.
And again, there is an external clock source startup table controlled by SUT0 and SUT1:
If you uncheck CKOPT then you will enable the capacitor between XTAL1 and GND.
Examples of how to program Atmega8 clock sources correctly
- 8MHz Crystal with slowly rising power:
- 2MHz ceramic resonator fast raising power:
- 32768kHz resonator stable frequency at startup:
- External RC 4MHz oscillator with internal C enabled fast rising power:
- If you want to drive maximum from your Atmega: then select 16MHz fast-rising power:
You may find all this information in datasheets. But like always – people forget to read them.
If there is any questions or concerns don’t hesitate to ask.