‘##’的用法
执行完A ## B后变为AB(连接A和B并去掉空格)。
最近帮老师做的一个项目,叫云柜控制,里面有十二把锁要控制,并且需要读他的状态,如果有一个一个去写函数控制太麻烦,可以用##批量定义、声明变量或者函数
include “lock.h”
include “delay.h”
define Unlock_Dlytime 100
define lock_read_Dlytime 100
lock_t lock_array[12];
/上锁操作宏函数声明/
define UNLOCK_DECLAR(locknum) void >unlock##locknum(void) \
{ \
unsigned char i = 0;\
if( lock_array[locknum].lock_isvaild == Lock_Invaild ) \
return ; \
if( lock_array[locknum].lock_fault == Lock_Fault ) \
return ; \
while(1){ \
i++; \
lock_con##locknum = 1; \
delay_ms(Unlock_Dlytime); \
lock_con##locknum = 0; \
if(lock_array[locknum].lock_sta == UNLocked) \
break; \
if(i >= 3){ \
lock_array[locknum].lock_fault = Lock_Fault; \
break; \
} \
} \
}/************读锁状态操作宏函数声明***************/
/*
*禁用的锁一直处于关闭状态
*/define LOCK_READ_STA_DECLAR(locknum) void lock_read_sta##locknum(void) \
{ \
if(lock_array[locknum].lock_isvaild == Lock_Invaild) \
lock_array[locknum].lock_sta = Locked; \
if( lock_array[locknum].lock_fault == Lock_Fault ) \
return ; \
lock_array[locknum].lock_sta = lock_state##locknum; \
\
}/*
*锁初始化操作宏函数声明
*/define LOCK_ARRAY_INIT_DELCAR(locknum) void lock_array_init##locknum(void) \
{ \
lock_array[locknum].lock_isvaild = Lock_vaild; \
lock_array[locknum].unlock = unlock##locknum; \
lock_array[locknum].unlock = Lock_Normal; \
lock_array[locknum].read_sta = lock_read_sta##locknum; \
lock_array[locknum].lock_sta = Locked; \
}
/函数声明/
LOCK_READ_STA_DECLAR(0)
LOCK_READ_STA_DECLAR(1)
LOCK_READ_STA_DECLAR(2)
LOCK_READ_STA_DECLAR(3)
LOCK_READ_STA_DECLAR(4)
LOCK_READ_STA_DECLAR(5)
LOCK_READ_STA_DECLAR(6)
LOCK_READ_STA_DECLAR(7)
LOCK_READ_STA_DECLAR(8)
LOCK_READ_STA_DECLAR(9)
LOCK_READ_STA_DECLAR(10)
LOCK_READ_STA_DECLAR(11)UNLOCK_DECLAR(0)
UNLOCK_DECLAR(1)
UNLOCK_DECLAR(2)
UNLOCK_DECLAR(3)
UNLOCK_DECLAR(4)
UNLOCK_DECLAR(5)
UNLOCK_DECLAR(6)
UNLOCK_DECLAR(7)
UNLOCK_DECLAR(8)
UNLOCK_DECLAR(9)
UNLOCK_DECLAR(10)
UNLOCK_DECLAR(11)LOCK_ARRAY_INIT_DELCAR(0)
LOCK_ARRAY_INIT_DELCAR(1)
LOCK_ARRAY_INIT_DELCAR(2)
LOCK_ARRAY_INIT_DELCAR(3)
LOCK_ARRAY_INIT_DELCAR(4)
LOCK_ARRAY_INIT_DELCAR(5)
LOCK_ARRAY_INIT_DELCAR(6)
LOCK_ARRAY_INIT_DELCAR(7)
LOCK_ARRAY_INIT_DELCAR(8)
LOCK_ARRAY_INIT_DELCAR(9)
LOCK_ARRAY_INIT_DELCAR(10)
LOCK_ARRAY_INIT_DELCAR(11)/函数初始化/
void lock_init(void){
lock_array_init0();
lock_array_init1();
lock_array_init2();
lock_array_init3();
lock_array_init4();
lock_array_init5();
lock_array_init6();
lock_array_init7();
lock_array_init8();
lock_array_init9();
lock_array_init10();
lock_array_init11();
}
分别用LOCK_READ_STA_DECLAR、UNLOCK_DECLAR、LOCK_ARRAY_INIT_DELCAR 声明了n个函数,其实可以用循环去写,或者更好的是用位操作去做,没有用位操作主要是基于我后来程序代码的考虑
‘#’的用法
使用#把宏参数变为一个字符串
/#123 –>”123”
/#define str(x) #s
可以用str()宏将数字转化位字符串
a=15 printf("a = %s",str(a));
结果为15