F_CPU not defined

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

Hi everybody. I can't understand why the compiler give me this warning:
warning: #warning "F_CPU not defined for "
In mylcd.c I try with:

#ifndef F_CPU
   #define F_CPU 8000000UL     
#endif
#include "mylcd.h"
#include 
#include 
#include 

Then in main.c I use

#include 
#include "mylcd.h"
#include 
#include 
#include 

Can anybody explain the correct use of

#define F_CPU

Thanks.

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

Think about it.

"mylcd.c" uses the _delay_xx() macros. Fine because it knows F_CPU.

"main.c" uses the delay_xx() macros. But it does NOT know F_CPU

Either put F_CPU in the Studio Configuration (or makefile)

Or place in ONE header file that is common to every file in your project that uses _delay_xx() ...

Normally you have a "project.h" but here you seem to have a common "mylcd.h"

David.

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

Ok. I try to place in "mylcd.h"

#ifndef F_CPU
   #define F_CPU 8000000UL     
#endif

.
Then I use the directive

#include 
#include "mylcd.h" 

in both "mylcd.c" and "main.c", but i have the same problem.

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

Try to think about the order in which the C pre processor operates. It's sequential. So if you include "mylcd.h" (in which F_CPU is defined) AFTER the use of then at that point F_CPU will not be defined.

To be honest you are better passing F_CPU as a -D from the Makefile anyway. Do you use a Makefile or do you use AVR Studio's project mechanism?

Cliff

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

Are you being deliberately obtuse?

Quote:
Then I use the directive

#include 
#include "mylcd.h"

knows nothing about F_CPU because "mylcd.h" has not been #include'd yet.

Personally I would always put the F_CPU in the makefile (or Studio config)

Using '#ifndef F_CPU' is a kludge in my opinion. You risk different values in different modules. I prefer to get the 'undefined' Warning. Then I correct my Studio configuration.

David.

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

Quote:

You risk different values in different modules.

One of the strong arguments for using -D instead and having a common value visible to all compilation units and defined BEFORE any #include.