学了点宏魔法想用一下,编译运行无误,但是VSC 代码分析的宏展开结果是错的,报错代码如下:
//参数连接工具宏
#define CAT(a, b) a ## b
#define CAT3(a, b, c) a ## b ## c
//查表工具宏
#define FIRST(a, ...) a
#define EVAL_FIRST(...) FIRST(__VA_ARGS__)
#define SECOND(a, b, ...) b
#define EVAL_SECOND(...) SECOND(__VA_ARGS__)
#define THIRD(a, b, c, ...) c
#define EVAL_THIRD(...) THIRD(__VA_ARGS__)
#define FOURTH(a, b, c, d, ...) d
#define EVAL_FOURTH(...) FOURTH(__VA_ARGS__)
//映射表,用来把引脚编号映射到寄存器,或者反向映射
#define PIN_INFO_1 P3, 0
#define PX_INFO_P30 1
#define PIN_INFO_2 P3, 1
#define PX_INFO_P31 2
#define PIN_INFO_3 P3, 2
#define PX_INFO_P32 3
//执行映射和反向映射
#define PIN_INFO(num) CAT(PIN_INFO_, num)
#define PX(port, pin) CAT3(PX_INFO_, port, pin)
#define PIN_PORT(num) EVAL_FIRST(PIN_INFO(num))
#define PIN_PIN(num) EVAL_SECOND(PIN_INFO(num))
#define SET_PIN(port, pin) do{ CAT3(port, _, pin) = 1; } while(0)
#define CLR_PIN(port, pin) do{ CAT3(port, _, pin) = 0; } while(0)
#define GET_PIN(port, pin) (CAT(port, pin))
#define setpin(num) SET_PIN(PIN_PORT(num), PIN_PIN(num))
#define clrpin(num) CLR_PIN(PIN_PORT(num), PIN_PIN(num))
#define getpin(num) GET_PIN(PIN_PORT(num), PIN_PIN(num))
#define LED PX(P3, 2) //先反向映射生成引脚编号
int main(void) {
clrpin(LED); //引脚编号再映射回去操作寄存器,只是为了测试。
while(1) {
if(getpin(LED))
clrpin(LED);
else
setpin(LED);
delay();
}
}
VSC 的报错如图:
正确的宏展开结果应该是
do{ P3_2 = 1; } while(0)
没找到可以解决这个报错问题的设置,报错全关掉又不方便。
visual studio code的解析器不支持宏的嵌套展开。