1

trying to solve this problem, I reduced my code to a minimum to find errors and check the results of expressions F_CPU is defined in the main file.

#ifndef USART_H_
#define USART_H_

#include <avr/io.h>



#define USART_BAUDRATE          4800
#define USART_BSCALE            -3

#if USART_BSCALE < 0
    #define USART_BSEL          ((F_CPU / 16) * (1<<(-1*USART_BSCALE)) / USART_BAUDRATE - (1<<(-1*USART_BSCALE)))
    #if (USART_BSEL < 0)
        #pragma message "BSEL < 0"
    #elif (USART_BSEL == 0)
        #pragma message "BSEL = 0"
    #elif (USART_BSEL > 0)
        #pragma message "BSEL > 0"
    #endif
#endif

#endif

The result of USART_BSEL - calculating with integers only - is 3325. The output is this:

#pragma message: BSEL > 0
#pragma message: BSEL < 0

How can I get more than one message from this code? And how can they be different?

The output is always two lines, but depending on my use of integer suffixes for BAUDRATE and F_CPU (I used L, UL and ULL), I get different pragma messages. Why is the preprocessor behaving that way?

Community
  • 1
  • 1
Peder
  • 345
  • 1
  • 4
  • 14
  • 2
    what is the #define for F_CPU? – Rishikesh Raje Jun 15 '16 at 07:43
  • Is it included twice? – dbrank0 Jun 15 '16 at 07:44
  • To produce exactly these two pragma lines, it is `#define F_CPU 32000000UL` – Peder Jun 15 '16 at 07:45
  • #define without parentheses is highly dangerous. You're aiming a gun right at your foot. – gnasher729 Jun 15 '16 at 07:45
  • @dbrank0 It is included in the main file and in the USART.c file. But as I understand it, this needs to be done. – Peder Jun 15 '16 at 07:47
  • 2
    And I only get BSEL > 0 when turning your code to .c and adding F_CPU define. – dbrank0 Jun 15 '16 at 07:47
  • 3
    Peder: And for each include you get one printout. And something (F_CPU?) changes between inclusions. – dbrank0 Jun 15 '16 at 07:48
  • I am also only getting BSEL>0, after removing the avr/io.h include and adding the F_CPU #define. This is on gcc 4.9.3. – Rishikesh Raje Jun 15 '16 at 07:52
  • I'm trying to update the compiler. Meanwhile, when I do _not_ include this header file (USART.h) in USART.c, only one and the correct message appear. This raises two problems: 1) As far as I know I _have_ to include the header file it in the c-file and 2) why are the results of the two messages different? (The c-file is empty except for the header inclusion.) – Peder Jun 15 '16 at 08:12
  • With given values, it results in `(16000000 / 4800 - 8)`. The magnitude of the 1st summand and the 2nd summand in your macro is very different. It seems `BSEL <= 0` will never occur? – Holger Jun 15 '16 at 11:33
  • Since you didn't include your whole project, we have to guess. My guess: You include usart.h in multiple C files. In one of those C files, F_CPU is defined, in another, it is undefined or 0. There *should* be a warning about F_CPU being used when undefined, but you *could* be compiling with this (or all) warnings turned off. – Steve Kolokowsky Jun 15 '16 at 16:19
  • @SteveKolokowsky: in an `#if` preprocessing directive, using a name which has not been `#define`d is not an error; the name is simply replaced with 0. I don't believe gcc will warn about this with any commonly-used `-W` setting – rici Jun 15 '16 at 16:23
  • @Peder: FYI: `-1*x` is parsed as though it were written `-(1*x)`, which is obviously the same as `-x`. So you could just write `-USART_BSCALE` instead of `(-1*USART_BSCALE)` – rici Jun 15 '16 at 16:25

0 Answers0