1

I would just like to give you the example of the LED blinking (1 in 1000 ms). My microcontroller is ATmega328P and I am using Atmel Studio.

State 1: If I don't define F_CPU, LED-blinking is running true. And I am using default fuse settings (Internal oscillator and CKDIV8=programmed).

State 2: If I define F_CPU 8000000UL, LED-blinking is running 8 times slower and again I am using default fuse settings (Internal oscillator and CKDIV8=programmed).

I don't use an external crystal in both 1.state and 2.state. And same fuse settings. Why do I see this result? Why 8 times slower?

Question 2: Some libraries need F_CPU value, such as UART. I don't use an external oscillator. What should I write to this value?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
alex jla
  • 11
  • 1
  • 2

1 Answers1

4

F_CPU is supposed to reflect the set microcontroller CPU clock speed. It doesn't care whether that speed was set because it using a external clock, or whether it is the internal one, or what div values are set.

So, the F_CPU value needs to be set to reflect whatever the cpu clock is configured to be.

According the datasheet for the atmega328p, the device unprogrammed is shipped with internal RC oscillator on, DIV8, so the CPU clock in that case would be 8MHz/8 = 1 MHz

So if you set F_CPU to 8000000, then any code that uses F_CPU thinks it is running at 8MHz instead of the1 MHZ that is really is.

lostbard
  • 5,065
  • 1
  • 15
  • 17