Problem with #define F_CPU

Hi,
I am trying to do a standalone atmega8 project using the 1MHz internal clock. I am using the Sketch->Export compiled binary option to generate hex files. Then I burn them(without the bootloader) to atmega8 using a USBasp.
Clearly I'm NOT worried about the boot loader. I use:

#define F_CPU 1000000L

in my sketch but the

delay(1000);

command results in an approx 16 seconds delay. Which means the new frequency is not in effect(I was just blinking an led with 1 sec interval for testing).
I also tried putting

build.f_cpu=1000000L

in my preferences.txt. Still no change.
Any ideas?

The problem is that F_CPU is already defined. you may try to add this instead:

#ifdef F_CPU
#undef F_CPU
#define F_CPU 1000000L
#endif

However, since your sketch is compiled after the source files, the millis() function (which delay() is based on) have probably been compiled with F_CPU = 16M. You'll hit the same problem if you e.g try to redefine the Serial buffer size in your sketch. Let's say you increase it to 128. if you print the buffer size macro

Serial.print(SERIAL_RX_BUFFER_SIZE)

The serial monitor will print 128. Still the serial RX buffer is 64. Go figure!

Back to the topic; if you want to get this working properly without having to modify Arduino's core files, I've already done it for you! I've created a 3rd party hardware add-on for the IDE that adds extended support for ATmega8, ATmega48, ATmega88, ATmega168 and ATmega328. You can choose preferred clock frequency, BOD option and compiler LTO. You should definitely give it a try! --> GitHub - MCUdude/MiniCore: Arduino hardware package for ATmega8, ATmega48, ATmega88, ATmega168, ATmega328 and ATmega328PB

(If you'll ever want to dive deeper into the AVR family, you can find the rest of my work here)

Right. Thanks man...
I'll definitely try your addon.
I really needed something like that.
Thank you so much for the insight... :slight_smile: :slight_smile:

You usually want to define F_CPU on the command line for the compile, rather than in the source.
How exactly this is done depends on the tools being used.

You can easily add an F_CPU option to the IDE's Tools menu by editing the boards.txt file.

It's explained halfway down this page.

The IDE will then automatically add it as a command line option (you can find this in platform.txt).

Pieter

hansibull:
However, since your sketch is compiled after the source files,

The order does not matter, it's the fact that they're compiled separately. Each source file is passed it's own F_CPU macro from the command line, which the IDE gets from the boards.txt file.

chitti:
Hi,
I am trying to do a standalone atmega8 project using the 1MHz internal clock. I am using the Sketch->Export compiled binary option to generate hex files. Then I burn them(without the bootloader) to atmega8 using a USBasp.
Clearly I'm NOT worried about the boot loader. I use:

#define F_CPU 1000000L

in my sketch but the

delay(1000);

command results in an approx 16 seconds delay. Which means the new frequency is not in effect(I was just blinking an led with 1 sec interval for testing).
I also tried putting

build.f_cpu=1000000L

in my preferences.txt. Still no change.
Any ideas?

What is your clock frequency? The F_CPU define only tells the compiler what CPU speed to use for various delays and functions. It does not change the CPU clock.

For 1 Mhz clock, you need a 1 Mhz crystal, or a 16 Mhz crystal with [b]clock_prescale_set (clock_div_16); [/b]or the internal oscillator with [b]clock_prescale_set (clock_div_8);[/b] or the CKDIV8 fuse bit programmed.