0
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ In particular, fresh 328Ps are configured to use the internal 8MHz oscillator, with an /8 divider, resulting in 1MHz, or 16 times slower than the expected Arduino-default. \$\endgroup\$ – marcelm Jun 27 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.