F_CPU in Studio 7

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

Hello all,

let me start by saying I hate having to ask such a newbee question but I just can't seem to find the answer. 

Where should F_CPU be defined when using Atmel Studio 7? I have found many answers for other IDEs and including it in the make file. Do I need to make a custom make file just to define the cpu speed? doesn't seem to be the right way to go, I know there are many wrong way to fix this ( including modifying util/delay) but what is the right way? the code below is what I am working on and still gives the warning F_CPU not defined for <util/delay>.

/*
 * usi_twi_master_1.c
 *
 * Created: 10/22/2019 11:49:23 AM
 * Author : John
 */ 
#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>
#include "usi_twi_master.h"

// Prototypes
char USI_I2C_Master_Start_Transmission(char *msg, char msg_size);

char i2c_transmit_buffer[3];
char i2c_transmit_buffer_len = 3;

int main(void)
{
    i2c_transmit_buffer[0] = (0x40 << 1) | 0 ; //Or'ing with 0 is unnecessary, but for clarity's sake this sets the R/W bit for a write.

    i2c_transmit_buffer[1] = 0x00;  //Internal address

    i2c_transmit_buffer[2] = 0x70;  //Value to write
    
   
    while (1) 
    {
        //Transmit the I2C message
        USI_I2C_Master_Start_Transmission(i2c_transmit_buffer, i2c_transmit_buffer_len);
        
    }
}

I have also noted that without the first line I get 2 warning messages saying the same thing so it must be doing something before it gets there.

I have search this site and read through many posts with very good explanations of what it is and how it works but not how to deal with it in studio 7.

Thanks for any help in advance, I know these newbee questions can be a pain.

ShorelineJohn

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

Is it F_CPU or f_cpu?  
I don’t recall, I’m on my tablet and don’t have access to any code.

any way, you just need your define before the delay.h as you have it above.

what AVR are you using?

welcome to AVRfreaks!

 

jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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


Maybe it's defined multiple times? Anyway you can set it here

 

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Jim, thanks for responding.

I am using F_CPU as that seams to be what util/delay is asking for.  The code shown does give me the warning once, if I take the first line out I get the same warning twice, so its like it trying to compile the util/delay before it get to the first line, if that makes sense. Maybe I'm not linking the files correctly or something? 

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

Oh and I am using the ATtiny 2313.  thanks again.

John

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

This happens for me aswell, if I build it again right after it has finished the warning goes away until I do a clean build, then it comes back again

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

JS

Thanks! Including the picture is so helpful. its an immediate answer. 

ShorelineJohn 

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

just remember this tells the compiler how fast the cpu is running, it does  not set the speed of the cpu (a common newbie error)

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

Just to clarify. When you have a macro that may need to visible to more that one source file (perhaps _delay_ms() in main.c and some baud rate calculation in uart.c ?) then you have a few options:

 

1) You could just put a "#define F_CPU 1234656UL" (or whatever the CPU speed is known to be) at the top of each file. But the downside of this is that say you changed from a 4MHz to a 3.6864MHz crystal and the files had all previously been using "#define F_CPU 4000000UL" then you might forget one and while some of the files now calculate their timing constants based on your new "#define F_CPU 3686400UL" so they come up with the right value, there's one lurking that still does the calculations based on the old 4MHz

 

2) to circumvent this what you could do is just make a single cpuspeed.h or something that contained nothing more than:

#ifndef CPUSPPED_H_
#define CPUSPEED_H_
    #define F_CPU 3686400UL
#endif

then all the places you might have been previously tempted to make the #define just "#include "cpuspeed.h"" instead. Now there's only one place to change it. This is better than situation 1 as it's more maintainable but it is still subject to possible error. A header like <util/delay.h> need F_CPU to be defined before it is parsed which means the definition has to be above the inclusion so if in this scheme you inadvertently used:

#include <util/delay.h>
#include "cpuspeed.h"

that would not work right

 

3) so the preferred solution is a command line -D passed to all the compiler invocations. You can do the equivalent of #define on the command line to avr-gcc just using "-D< whatever you want to define>". This effectively makes the define of the preprocessor macro before the preprocessor even starts to read the .c files, process the #includes/#defines and so on. In AS7 you create -D's under "Symbols". Anything you list there just create -D< whatever > when the prepro/compiler are run. That guarantees these defines are made before any #include is processed so an inlcude of <util/delay.h> could be the very first thing in a .c file and it would still work right.

 

So John's picture in #3 shows this in action. The only downside of this method is that it may not be quite so obvious to the reader. If they read through all the .h and .c in the project they won't immediately see where the mysterious F_CPU being used is coming from. They have to know about this "Symbols" thing to find it there. Another possible downside are that code browsers like Intellisense and Visual Assistmay not "know" about definitions made there so if you had some conditional code based on a -D macro the code browser may not know which path to evaluate.

 

But on the whole -D is generally the "best" method.