Electrical – Slow clock speed with Atmega328P AVR microcontroller on breadboard

atmega328pclock-speedmicrocontrollerusbasp

I have a small breadboard with an Atmega328p 28 pin microcontroller with a 16 MHz crystal and two 20 pF capacitors on it (similar to this demo: https://www.arduino.cc/en/Tutorial/ArduinoToBreadboard).

The difference with that demo is I am using a USBasp ISCP programmer to program the chip instead of an Arduino, and I was able to successfully program the infamous 'blink' program to my MCU and I couldn't be more happy.

However, instead of delaying for 1000 ms (1 second), I needed to change the parameter of my wait function to "20 ms" to achieve an on/off time of 1 actual second.

Is there something I'm missing here? Do I need to add anything to my circuit or perhaps change the capacitor values in order to fix the clock speed? Thank you.

Best Answer

The delay functions calculate the clock cycles by looking at the F_CPU macro. You have to set the #define F_CPU 16000000UL so it matches the actual CPU speed.

That actual CPU speed is set by programming the fuses correctly. I think you forgot that step.

Related Topic