c - 预处理器的 If 条件问题

标签 c c-preprocessor avr

我为 AVR XMega 微 Controller 编写了一个引导加载程序,并且该引导加载程序由配置文件配置:

Config_Bootloader.h

#ifndef CONFIG_BOOTLOADER_H_ 
#define CONFIG_BOOTLOADER_H_ 

 #include <avr/io.h>

 #define BOOTLOADER_INTERFACE                   &USARTE0
 #define BOOTLOADER_BAUD                        115200
 #define BOOTLOADER_TX                          3       

#endif /* CONFIG_BOOTLOADER_H_ */

此配置文件应该由另一个包含文件进行预处理,以获取一些寄存器值等。

Bootloader_Preprocessing.h

#ifndef BOOTLOADER_PREPROCESSING_H_
#define BOOTLOADER_PREPROCESSING_H_

 #include <avr/io.h>

 #ifdef USARTE0
     #if(BOOTLOADER_INTERFACE == &USARTE0)
        #define BOOTLOADER_PORT     &PORTE
     #else
        #error "Invalid bootloader interface!"
     #endif
 #endif

 #if(BOOTLOADER_BAUD == 9600)
     #define BOOTLOADER_BRREG_VALUE         12
     #define BOOTLOADER_SCALE_VALUE         0
 #elif(BOOTLOADER_BAUD == 19200)
     #define BOOTLOADER_BRREG_VALUE         11
     #define BOOTLOADER_SCALE_VALUE         -1
 #elif(BOOTLOADER_BAUD == 38400)
     #define BOOTLOADER_BRREG_VALUE         9
     #define BOOTLOADER_SCALE_VALUE         -2
 #elif(BOOTLOADER_BAUD == 57600)
     #define BOOTLOADER_BRREG_VALUE         75
     #define BOOTLOADER_SCALE_VALUE         -6
 #elif(BOOTLOADER_BAUD == 115200)
     #define BOOTLOADER_BRREG_VALUE         11
     #define BOOTLOADER_SCALE_VALUE         -7
 #else
     #error "Invalid baud rate for bootloader!"
 #endif

#endif /* BOOTLOADER_PREPROCESSING_H_ */

我将这两个文件都包含到我的 Bootloader.h

#ifndef BOOTLOADER_H_
#define BOOTLOADER_H_

 #include "Config_Bootloader.h"
 #include "Bootloader_Preprocessing.h"

#endif /* BOOTLOADER_H_ */

我收到以下错误和警告:

> #define BOOTLOADER_INTERFACE                  &USARTE0

operator '&' has no left operand

> #if(BOOTLOADER_INTERFACE == &USARTE0)

in expansion of macro 'BOOTLOADER_INTERFACE'
#error "Invalid bootloader interface!"

那么为什么地址比较不起作用?

最佳答案

预处理器中不存在“地址”之类的东西,因此无法在#if预处理器指令中对它们进行比较。

参见the GCC docs for #IF有关它可以做什么和不能做什么的详细信息。请参阅您正在使用的预处理器的文档,可能适用其他/不同的限制(您将其标记为 AVR)。

您的预处理器似乎得出结论,运算符 & 必须是按位运算符 &,它是二元运算符,因此需要左操作数。

关于c - 预处理器的 If 条件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56642242/

相关文章:

c - 如何在 GDB 中打印#defined 常量?

algorithm - 在没有PWM的AVR组件中生成方波

c - 变量被重置。不知道为什么

c - 如何计算 8 个不同浮点值的最大值/最小值

c - 如何计算多字节字符的数量?

c++ - 防止 C 预处理器执行特定的宏替换

c++ - 在 Clang 中使用 MSVC 预处理器 'charizing' 运算符

c - 为什么只有第一个中断起作用?

c - 即使声明为(已使用),PROGMEM 变量也会被丢弃

c - 在 switch case 语句中显式转换