Electronic – ATMEGA8 with 8Mhz crystal just running at 1Mhz

atmegaavrfrequency

I have a ATMEGA8A-PU with a 8Mhz crystal oscillator (T8.000 is 8Mhz isn't it?) and the following C code:

#define F_CPU 8000000UL

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
        DDRC = 0b00100000;
        while(1) {
                PORTC ^= 1 << PORTC5;
                _delay_ms(1000);
        }
        return 0;
}

So, just a simple code, that switches the LED on and off every second.

But when I write this program to the controller, the LED switches on/off after about 8 seconds. When I replace F_CPU to 1000000UL, it switches nearly every second.

So I thought, it's a problem with the fuse bits, but they are set to F1 (low) and D9 (high) which looks good to me.

Anyone who has an idea how I get the full 8Mhz?

Best Answer

When I replace F_CPU to 1000000UL, it switches nearly every second.

That should give you a clue: the CPU is running at 1MHz and not 8MHz

Your lfuse settings are wrong.

lfuse => 0xF1
SUT[1:0] => 11 
CKSEL[3:0] => 0001

This selects the internal RC Oscillator (see sec. 8.2 "Clock Sources" in the Datasheet), and SUT is set to a value that is not allowed for this value of CKSEL (Table 8-9)

To have it run off the 8MHz ext. crystal, you need these settings:

From Table 8-3:

CKSEL[3:1] = 111 (freq. range is [3.0, 8.0] MHz)
CKOPT = 1 (unprogramed, for low-power mode; if set to 0, you need to change hfuse as well)

From Table 8-4:

CKSEL[0] = 1
SUT[1:0] = 11 (Crystal Oscillator, Slowly rising power: change this if you want)

This gives lfuse = 0xFF

Don't forget the 22pF capacitors!