在宏函数中使用数值常量
我正在为我的 AVR 编写一个简单的库,我想尝试使用宏函数来定义我的波特率。 AVR库中的很多函数以及我想写的宏都使用了F_CPU宏。
这是我对宏定义和我假设的实现的内容:
#define BAUD_SELECT(baud) ((F_CPU)/(2*baud)-1)
myubrr = BAUD_SELECT(38400);
我尝试使用 #define F_CPU 8000000UL,也在 make 文件中使用 -D"F_CPU 8000000UL" 但我总是在实现行遇到相同的错误。
expected ')' before numeric constant
我确信这与我滥用 #define 有关,宏定义位于头文件中,实现位于相应的 .c 文件中,F_CPU 定义位于 makefile 或另一个 main.c 文件中。
编辑 我按照建议更改了括号并运行了预处理器并找到了输出文件(至少我认为)
unsigned int myubrr = ((8000000UL 1)/(2*(baud))-1);
它在 F_CPU 应该在的位置放置了一个额外的 1,我对预处理器没有经验,所以我不知道如何使它不这样做,但也许这就是问题所在?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将其括在括号中:
Try wrapping it in parentheses:
这对我来说效果很好:
当我使用
额外的括号进行编译时,在这种特定情况下并不重要,一般认为它们是一个好主意。所以还有其他事情发生。也许在其他头文件中还有另一个
F_CPU
定义,它会覆盖您对F_CPU
的定义This works fine for me:
when I compile with
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 ofF_CPU