一:C语言中的宏定义函数是一种预处理指令,用于在编译之前对代码进行替换和展开。宏定义函数具有以下几个优势:
代码重用:通过宏定义函数,可以将一段代码片段定义为一个宏,然后在程序中多次调用该宏,从而实现代码的重用。这样可以减少代码的冗余,提高代码的可维护性和可读性。
编译时展开:宏定义函数在编译时会被直接展开,而不需要函数调用的开销。这样可以减少函数调用带来的额外开销,提高程序的执行效率。
参数灵活性:宏定义函数可以接受不同数量和类型的参数,而不像普通函数需要事先声明参数的类型和数量。这样可以在一定程度上提高代码的灵活性和通用性。
条件编译:宏定义函数可以与条件编译结合使用,根据不同的条件选择性地展开或忽略宏定义函数的代码。这样可以根据不同的编译选项生成不同的代码,实现更灵活的程序控制。
二:在学习参考了前两个宏定义函数以后,充分体会了宏定义函数的妙用,个人总结了后六个常用宏定义函数提供大家参考,在个人嵌入式MCU编程中发挥了十分巧妙的作用。
#define return_if_fail(p) \
if (!(p)) { \
return; \
}
#define return_value_if_fail(p, value) \
if (!(p)) { \
return (value); \
}
#define return_if_in_invalid_time(clock, time) \
static unsigned int TimerNode = 0; \
if (clock - TimerNode < time) \
return; \
else \
TimerNode = clock; \
#define return_value_if_in_invalid_time(clock, time, value) \
static unsigned int TimerNode = 0; \
if (clock - TimerNode < time) \
return (value); \
else \
TimerNode = clock; \
#define execute_once(p) \
static char executed = 0; \
if (!executed) { \
p; \
executed = 1; \
}
#define execute_invalid_time(clock, time, p) \
do \
{ \
static unsigned int TimerNode = 0; \
if (clock - TimerNode > time) { \
p; \
TimerNode = clock; \
} \
}while(0)
#define do_if_flag_set(flag, p) \
if (flag!=0) { \
p; \
flag=0; \
}
#define timed_shutdown(flag, clock, time, p) \
do \
{ \
static char pre_flag = 0; \
static unsigned int TimerNode = 0; \
if(flag != 0) \
{ \
if (flag != pre_flag) \
TimerNode = clock; \
if (clock - TimerNode > time) \
{ \
p; \
flag = 0; \
} \
} \
pre_flag = flag; \
}while(0)
#define delay_ms(clock, time) \
do \
{ \
unsigned int TimerNode = clock; \
while(clock - TimerNode < time); \
}while(0)
/*超时看门狗, 定时喂狗,否则执行超时操作*/
#define overtime_watchdog(flag, clock, time, p) \
do \
{ \
static unsigned int TimerNode = 0; \
if(flag != 0) \
{ \
TimerNode = clock; \
} \
else if(clock - TimerNode > time) \
{ \
p; \
} \
}while(0)
/*标志位使能作用时间*/
#define flag_enable_time(flag, clock, time, p) \
do \
{ \
static unsigned int TimerNode = 0; \
static char lock = 0; \
if(flag != 0 && lock == 0) \
{ \
TimerNode = clock; \
lock = 1; \
} \
else if(clock - TimerNode > time) \
{ \
p; \
lock = 0; \
} \
}while(0)
/*捕获*/
#define edge_capture_init_0(object, p) \
do \
{ \
static char pre_object = 0; \
if(object != pre_object) \
{ \
p; \
} \
pre_object = object; \
}while(0)
#define edge_capture_init_1(object, p) \
do \
{ \
static char pre_object = 1; \
if(object != pre_object) \
{ \
p; \
} \
pre_object = object; \
}while(0)
#define up_edge_capture(object, p) \
do \
{ \
static char pre_object = 1; \
if(object != 0) \
{ \
if(pre_object == 0) \
{ \
p; \
} \
} \
pre_object = object; \
}while(0)
#define down_edge_capture(object, p) \
do \
{ \
static char pre_object = 0; \
if(object == 0) \
{ \
if(pre_object != 0) \
{ \
p; \
} \
} \
pre_object = object; \
}while(0)
#define execute_invalid_time_debug(s, clock) \
do \
{ \
static unsigned int TimerNode = 0; \
printf("%s:%d\r\n", s, clock-TimerNode); \
TimerNode = clock; \
}while(0)
#define execute_times_fail_do(condition, times, p) \
do \
{ \
int times_num; \
for(times_num=0; times_num<times && !(condition); times_num++); \
if(times_num>=times) p; \
}while(0)
#define execute_times_waiting_for_seccess(condition, times, p, event) \
if(!condition){ \
static unsigned int times_num = 0; \
if(times_num<times){ \
p; \
times_num++; \
} \
else{ \
static char executed = 0; \
if (!executed) { \
event; \
executed = 1; \
} \
} \
}
————————————————
版权声明:本文为CSDN博主「嵌入式小宁」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_53592457/article/details/131438241
|