0

I'm writing a simple library for my AVR, and I wanted to try to use a macro function to define my baud-rate. A lot of the functions in the AVR's library use the macro F_CPU as well as the one I want to write.

Here's what I have for the macro definition and my supposed implementation:

#define BAUD_SELECT(baud) ((F_CPU)/(2*baud)-1)

myubrr = BAUD_SELECT(38400);

I have tried using #define F_CPU 8000000UL, and also in the make file as -D"F_CPU 8000000UL" but I always get the same error at the implementation line.

expected ')' before numeric constant

I'm sure it has something to do with my abuse of #define, and that the macro definition is in a header file, the implementation in the appropriate .c file, and the F_CPU definition either in the makefile or another main.c file.

EDIT I made the parenthesis change as suggested and ran the preprocessor and found the output file (atleast I think)

 unsigned int myubrr = ((8000000UL 1)/(2*(baud))-1);

It places an extra 1 where F_CPU should be, I'm not experienced with the preprocessor so I'm not sure how to make it not do that, but perhaps that is the problem?

3
  • try running the preprocessor on it and see what it expands to Commented Oct 3, 2011 at 16:32
  • 1
    This seems to be correct. You are most certainly missing a parenthesis somewhere else. On a side note, in the expression of the macro, always put the variables taken as argument in parentheses. Imagine your macro called like BAUD_SELECT(some_baud+100) which would expand to ((F_CPU)/(2*some_baud+100)-1) which is not what you want. So, you would have to write the expression like this: ((F_CPU)/(2*(baud))-1) Commented Oct 3, 2011 at 16:36
  • Take a closer look at the defintion of F_CPU ... perhaps there is some garbage at the end of the line outside your editor's window. Commented Oct 3, 2011 at 17:02

2 Answers 2

1

Try wrapping it in parentheses:

#define BAUD_SELECT(baud) ((F_CPU)/(2*(baud))-1)
Sign up to request clarification or add additional context in comments.

Comments

0

This works fine for me:

#define BAUD_SELECT(baud) ((F_CPU)/(2*(baud))-1)

unsigned int myubrr = BAUD_SELECT(38400);

when I compile with

$ cc -c -DF_CPU=8000000UL t.c

The extra parens don't really matter in this specific case, thought they're a good idea in general. So there's something else going on. Perhaps there's another definition of F_CPU in some other header file that is overriding your definition of F_CPU

2 Comments

Strangely enough going from -D"F_CPU 8000000UL" to -D"F_CPU=8000000UL" allowed it to compile, I never noticed any distinction between the two until now, but still not sure what the preprocessor does differently.
Indeed, this is somehow weird ... ;-) - In the first run -D"F_CPU 8000000UL" defines one macro with a blank in it's identifier, namely the macro 'F_CPU 8000000UL', as no '=' sign is used it gets the value of 1. So we have 'F_CPU 8000000UL 1'. Then when parsing your code the preprocesser does take this phrase as it is and replaces 'F_CPU' in your code with '8000000UL 1'. One could consider this as a cpp bug? ;->

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.