有没有办法将多个值作为 C 中定义的单个宏值传递给宏函数?
Is there a way to pass multiple values to macro function as single defined macro value in C?
我想在全局 header 中将引脚定义声明为简单的一行,例如:
#define STATUS_LED B,7
然后我想将这个引脚定义传递给上面的函数:
CMBset_out(STATUS_LED);
我不知道如何解决这个问题 - MY_PIN 格式正确,可以在预编译阶段替换。
#define CMBsbi(port, pin) (PORT##port) |= (1<<pin)
#define CMBset_out(port,pin) (DDR##port) |= (1<<pin)
// define pins
#define STATUS_LED B,7
然后,我想将此引脚定义传递给上面的函数(hw_init_states()
在从主 C 文件调用的同一个 header 文件中声明):
// runtime initialization
void hw_init_states(){
#ifdef STATUS_LED
CMBset_out(STATUS_LED);
#endif
}
但是我得到一个编译错误:
Error 1 macro "CMBset_out" requires 2 arguments, but only 1 given GENET_HW_DEF.h 68 23 Compass IO_proto
是可能的,但你需要另一层宏来扩展参数:
#define CMBset_out_X(port,pin) (DDR##port) |= (1<<pin)
#define CMBset_out(x) CMBset_out_X(x)
当然,这意味着您不能使用带有两个显式参数的 CMBset_out
宏。
对上一个答案的改进,它还允许您使用两个显式参数调用宏。
它应该适用于任何 c99(或更好)编译器:
#define CMBset_out_X(port,pin) (DDR##port) |= (1<<pin)
#define CMBset_out(...) CMBset_out_X(__VA_ARGS__)
#define STATUS_LED B,7
CMBset_out(STATUS_LED)
CMBset_out(B, 7)
我想在全局 header 中将引脚定义声明为简单的一行,例如:
#define STATUS_LED B,7
然后我想将这个引脚定义传递给上面的函数:
CMBset_out(STATUS_LED);
我不知道如何解决这个问题 - MY_PIN 格式正确,可以在预编译阶段替换。
#define CMBsbi(port, pin) (PORT##port) |= (1<<pin)
#define CMBset_out(port,pin) (DDR##port) |= (1<<pin)
// define pins
#define STATUS_LED B,7
然后,我想将此引脚定义传递给上面的函数(hw_init_states()
在从主 C 文件调用的同一个 header 文件中声明):
// runtime initialization
void hw_init_states(){
#ifdef STATUS_LED
CMBset_out(STATUS_LED);
#endif
}
但是我得到一个编译错误:
Error 1 macro "CMBset_out" requires 2 arguments, but only 1 given GENET_HW_DEF.h 68 23 Compass IO_proto
是可能的,但你需要另一层宏来扩展参数:
#define CMBset_out_X(port,pin) (DDR##port) |= (1<<pin)
#define CMBset_out(x) CMBset_out_X(x)
当然,这意味着您不能使用带有两个显式参数的 CMBset_out
宏。
对上一个答案的改进,它还允许您使用两个显式参数调用宏。
它应该适用于任何 c99(或更好)编译器:
#define CMBset_out_X(port,pin) (DDR##port) |= (1<<pin)
#define CMBset_out(...) CMBset_out_X(__VA_ARGS__)
#define STATUS_LED B,7
CMBset_out(STATUS_LED)
CMBset_out(B, 7)