[SOLVED] #ifndef not working?

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

Hello, I have short question regarding the use of #ifndef. I want to move some functions to a library and for that I want to define F_CPU and other constants in the main.c:

 

In my main.c there is:

 

#define F_CPU   48000000

#include <avr32/io.h>
#include "my_usart.h"

int main (void)
{
    while(1)
    {
        //some crazy stuff
    }
}

in my my_usart.h:

 

#ifndef MY_USART_H_
#define MY_USART_H_

#include <compiler.h>

#ifndef F_CPU
	#warning F_CPU defined by my_usart
	#define F_CPU 60000000
#endif

void usart_init(uint16_t baudRate);

void usart_putc(char data);

void usart_puts(char *strPtr);

#endif /* MY_USART_H_ */

and in my my_usart.c:

 

#include <avr32/io.h>
#include <compiler.h>
#include "my_usart.h"

#ifndef USART1_TXD
	#define USART1_TXD	AVR32_PIN_PA23
#endif

#ifndef USART1_RXD
	#define USART1_RXD	AVR32_PIN_PA24
#endif

void usart_init(uint16_t baudRate)
{
    //
}

and it's still taking the F_CPU from the my_usart.h file as I can see through my #warnings. I made the files according to the pattern I found in the util_delay.h file which I know from the 8 bit atmegas. For use of this library F_CPU must be defined before including the library. I wanted to do the same, but it's not. Can someone see what is going wrong?

Last Edited: Mon. Oct 30, 2017 - 09:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The way to investigate preprocessor issues is to view the preprocessor output

 

IIRC, -save-temps is the GCC option for this.

 

https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html

 

Many options there to help you - including:

 

GCC manual wrote:
-dM

 

Instead of the normal output, generate a list of ‘#define’ directives for all the macros defined during the execution of the preprocessor, including predefined macros.

This gives you a way of finding out what is predefined in your version of the preprocessor.

 

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

Jacky_88 wrote:
and it's still taking the F_CPU from the my_usart.h file as I can see through my #warnings.
Sure! A #define in a c-file is not global. When my_usart.c is compiled the compiler knows nothing about the #define F_CPU in main.c, so the  #ifndef F_CPU triggers.

Stefan Ernst

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

That is why I define F_CPU in a header file (f_cpu.h) and #include that file where needed. It can also be defined in the IDE and it is global from there.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

I prefer to define it in the project properties/toolchain settings:

 

Greg Muth

Portland, OR, US

Atmel Studio 7.0 on Windows 10

Xplained/Pro/Mini Boards mostly

 

 

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

Ok, I will deal with the GCC preprocessor tommorow. But how is the avr/util/delay.h library doing the trick?

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

Jacky_88 wrote:
But how is the avr/util/delay.h library doing the trick?

What trick?

It is the exact same thing there. If you do a #include <util/delay.>, then you either have to do a #define F_CPU beforehand (directly in the same c-file, or indirectly by including a header file containing it, as suggested by Jim), or you "inject" the F_CPU definition from "outside" (as suggested by Greg). Having the #include <util/delay.> in one c-file and the #define F_CPU in some other c-file (like your code above) does not work.

Stefan Ernst

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

If you use <util/delay.h> without providing a F_CPU definition, the delay routines will default to assuming 1MHz for F_CPU.

 

Edit: clarify statement

David (aka frog_jr)

Last Edited: Mon. Oct 30, 2017 - 09:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

omg, I got it. I got confused with .h files and .c files . Thank you smiley

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

Rather than edit the thread title, here is the proper way to mark a thread as solved: http://www.avrfreaks.net/comment...