How to change the %$#*#$ F_CPU in AS7/Arduino

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, I've seen some other other threads about this, but nothing that seems to be definitive for me (at least, so far I cannot seem to make it work!).

I have an Arduino sketch that I imported into AS7. The sketch runs, and I can use the AVR Dragon debugger and so on. The sketch has a very simple section where I turn on an LED, delay for 1000 ms, then turn off the LED. However, the LED stays on for two seconds not one, indicating that the timer values are still calculated based upon a 16 MHz clock, instead of 8 Mhz (internal RC clock is what I want to use)

No matter what I try, I cannot seem to find a way to tell the project that I want it to use an F_CPU of 8 MHz instead of 16 MHz.

I did a "search in files" for a line that says "#define F_CPU 16000000" but the search says no results found, even when I widen the scope as much as I can.

I have tried adding #define F_CPU 8000000 to my sketch.cpp file, no good.

I have tried changing the symbols in the makefile section of the project options (both C and C++), no luck.

 

How does one change the F_CPU value?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go to Project Symbols. I always select 'All Configurations'.
You might need to edit both C and C++ Symbols.
.
Incidentally, I suggest that you always put F_CPU in Symbols. You can make a Template.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

davetelling wrote:
I did a "search in files" for a line that says "#define F_CPU 16000000" but the search says no results found, even when I widen the scope as much as I can.

Did you search for the alternative ways that could be written, e.g.

#define F_CPU 16000000UL

?

 

Have you done a search for just "F_CPU".

 

IIRC, Arduino uses Timer/Counter0 for generating "ticks" etc. If they do not reely on the F_CPU preprocessor symbol, and you change the actual clock frequency then the Arduino ticks will not be what you/they expect.

 

If they do rely on the F_CPU symbol, then it should be somewhere in their source...

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can we see the complete build output? (After a Clean + Build, switch to the Output tab, mark everything there, copy and paste in a post here.)

 

If something in the project setup (i.e. not in any source file) is defining F_CPU it should be visible there.

 


 

I had a look into the Arduino sources, and there seems to be a somewhat elaborate scheme to define F_CPU based on what board the code is built for. So, I do not believe you will find any definition of F_CPU in any source file. 

 

Details: in boards.txt there is a "setup" of a "variable" called f_cpu (yes, lower case). E.g.:

uno.build.f_cpu=16000000L

Then, in  platform.txt are "recipies" for building. E.g. for compiling a .cpp source file into a .o object file it looks like:

## Compile c++ files
recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} "{source_file}" -o "{object_file}"

Notice the 

-DF_CPU={build.f_cpu}

So, in the Arduino project the f_cpu is set up depending on what board you have selected, and the value of f_cpu is then used to define F_CPU on the compiler command line. There will most likely not be any F_CPU defined in any source file.

 

Now, how Atmel Studio is handling this when converting an Arduino project to a Studio project I do not know, but it is likely that it has set up a deefinition of F_CPU in the project options. Go have another look. Both for C++ and C.

 

(Sorry, but I'm not on a Windows machine ATM so can't test this myself.)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Thu. Aug 31, 2017 - 08:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It looks as if the OP has imported an Arduino sketch into AS7.
It comes with all the relevant build Symbols, library paths, ... from the board originally chosen.
.
So it will use F_CPU when building the Core. And your sketch might use it too. Personally, I always calculate Timer values rather than use magic values.
.
David.

Last Edited: Thu. Aug 31, 2017 - 08:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your application description is not quite clear.  Are you running the program on an Arduino hardware platform?  In that case, the board defaults to the on-board 16MHz resonator.  There needs to be a change in the fuses to allow the Mega328P on the Arduino to ignore the resonator and instead to use the internal 8MHz clock generator.   Wouldn't a two second period mean that the clock is operating at 8MHz and a one-second LED period mean that the CPU is running at 16MHz?

 

The F_CPU informs the compiler of the speed that should be used by the compiler to calculate delays and intervals.  It does not actually set the clock on the CPU to operate at the F_CPU speed.  This has to be done by the developer by changing the fuses and system clock prescaler register.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What board did you tell AS7 you are using?  Only some of them have a choice of mpu speed...

  ex. PRO-mini  M328  8 / 16 mhz

        Mega64  lets you choose 1 internal, 8 internal, 8 external, 16 external......

 

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:

davetelling wrote:
I did a "search in files" for a line that says "#define F_CPU 16000000" but the search says no results found, even when I widen the scope as much as I can.

Did you search for the alternative ways that could be written, e.g.

#define F_CPU 16000000UL

?

 

Have you done a search for just "F_CPU".

 

IIRC, Arduino uses Timer/Counter0 for generating "ticks" etc. If they do not reely on the F_CPU preprocessor symbol, and you change the actual clock frequency then the Arduino ticks will not be what you/they expect.

 

If they do rely on the F_CPU symbol, then it should be somewhere in their source...

 

I did a search for just F_CPU, and there are dozens of instances, avirtually all of which were of the format #ifdef F_CPU ++ xxxxxxxx or a formula to compute timer cycles, with F_CPU as one of the factors.

I also found four instances of this: <Value>F_CPU=16000000L</Value> on 4 different lines in ArduinoCore.cppproj Perhaps I could try changing those....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:

Your application description is not quite clear.  Are you running the program on an Arduino hardware platform?  In that case, the board defaults to the on-board 16MHz resonator.  There needs to be a change in the fuses to allow the Mega328P on the Arduino to ignore the resonator and instead to use the internal 8MHz clock generator.   Wouldn't a two second period mean that the clock is operating at 8MHz and a one-second LED period mean that the CPU is running at 16MHz?

 

The F_CPU informs the compiler of the speed that should be used by the compiler to calculate delays and intervals.  It does not actually set the clock on the CPU to operate at the F_CPU speed.  This has to be done by the developer by changing the fuses and system clock prescaler register.

I am running a standalone ATmega328, using the Atmel Studio 7 debugger/programmer interface (AVR Dragon). I changed the fuses of the 328 to select the internal 8 MHz clock, and ther is no external crystal or resonator being used. I need to be able to tell the compiler that the actual clock frequency is 8 MHz instead of 16.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:

Can we see the complete build output? (After a Clean + Build, switch to the Output tab, mark everything there, copy and paste in a post here.)

 

If something in the project setup (i.e. not in any source file) is defining F_CPU it should be visible there.

 


 

I had a look into the Arduino sources, and there seems to be a somewhat elaborate scheme to define F_CPU based on what board the code is built for. So, I do not believe you will find any definition of F_CPU in any source file. 

 

Details: in boards.txt there is a "setup" of a "variable" called f_cpu (yes, lower case). E.g.:

uno.build.f_cpu=16000000L

Then, in  platform.txt are "recipies" for building. E.g. for compiling a .cpp source file into a .o object file it looks like:

## Compile c++ files
recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} "{source_file}" -o "{object_file}"

Notice the 

-DF_CPU={build.f_cpu}

So, in the Arduino project the f_cpu is set up depending on what board you have selected, and the value of f_cpu is then used to define F_CPU on the compiler command line. There will most likely not be any F_CPU defined in any source file.

 

Now, how Atmel Studio is handling this when converting an Arduino project to a Studio project I do not know, but it is likely that it has set up a deefinition of F_CPU in the project options. Go have another look. Both for C++ and C.

 

(Sorry, but I'm not on a Windows machine ATM so can't test this myself.)

Yes, in fact I can see this: "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-g++.exe" -funsigned-char -funsigned-bitfields -DDEBUG -DF_CPU=16000000L ....."

So, something is telling the compiler to use 16000000, but I don't know what. I'm gong to try the idead about fiddling with the boards.txt and see what happens.

Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK - here's what I found out: I changed the "uno.build.f_cpu" to 8 MHz, as Johan suggested - no change in the compiler command line. I then thought that perhaps that information is imported when the sketch was originally brought into AS7, and then those board files are not used again. So, I modified the uno.build.f_cpu to 8 MHz in boards.txt, then I made a new Arduino sketch using the same code. I then used AS7 native Arduino import to make a project in AS&, and lo and behold, the build output shows F_CPU as 8 MHz! And, the delay is correct.

So, the takeaway is that with the native Arduino import, you can't go back and modify some of the configuration files once they have been used to set up the project; you have to make a new project after modifying the files.

Sheesh, what a goat-rope!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

davetelling wrote:

OK - here's what I found out: I changed the "uno.build.f_cpu" to 8 MHz, as Johan suggested - no change in the compiler command line. ...

 

Sorry, I meant in the build output showing the compiler command line.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Since no-one seems to have read my reply about  "Symbols",  I have looked at an imported sketch:

 

Sure enough.   It contains the imported Symbols as I reported e.g.

F_CPU=16000000L
ARDUINO=10801
ARDUINO_AVR_UNO
ARDUINO_ARCH_AVR
USB_VID=0x2341
USB_PID=0x0043
USB_MANUFACTURER="\"Arduino LLC\""

When a sketch is imported into AS7,  it is standalone.   Any changes only apply to the AS7 project.   Not to your "proper"Arduino IDE.

 

If your Arduino board is non-standard,  add a new description to the "boards.txt".   Select your "Special Board" in the IDE.   Use that "Special Board" when importing to AS7.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, if you need to change clock speed you must change F_CPU in:

 

1. properties-toolchain-AVR/GNU C Compiler

2. properties-toolchain-AVR/GNU C++ Compiler

3. your_project_name.cppproj

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't think the OP has been waiting for a years for an answer. wink

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Probably not, but some other guys might find this useful in the future :-)