Help with macros C

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Help with macros C

跳至解决方案
1,728 次查看
Florijan
Contributor III

Hi,

 

I have the following code:

 

#define SET_BIT(reg,bit_no)      reg |= (1<<bit_no)

#define TRANSMITTER_ENABLE_BIT   SCI2C2, 7

 

 

 

void main(void){

 

     SET_BIT(TRANSMITTER_ENABLE_BIT);

 

}

 

 

With this code I want to put two arguments SCI2C2 and 7 into SET_BIT() macro. But the compiler says that there is only one argument detected instead of two in the line SET_BIT(TRANSMITTER_ENABLE_BIT);.

 

Can someone please tell me how to pass two arguments to macro   SET_BIT() ?

 

Best regards,

Florijan

标签 (1)
0 项奖励
回复
1 解答
1,517 次查看
kubiznak_petr
Contributor V

Hi Florijan,

I don't think you can do so since function-like macros are evaluated from outside in, as explained here: Macro Call as Macro Argument . Hence SET_BIT is evaluated first, the preprocessor sees it requires two arguments, but only one is found. It ends up with error without evaluating the inner macro (TRANSMITTER_ENABLE_BIT).

I see only two solutions:

1:

#define SET_BIT(reg,bit_no)        reg |= (1<<bit_no)

#define TRANSMITTER_ENABLE_REG     SCI2C2

#define TRANSMITTER_ENABLE_BIT     7

 

void main(void){

      SET_BIT(TRANSMITTER_ENABLE_REG, TRANSMITTER_ENABLE_BIT);

}

2:

#define SET_BIT(reg,bit_no)      reg |= (1<<bit_no)

#define TRANSMITTER_ENABLE       SET_BIT(SCI2C2,7)

 

void main(void){

      TRANSMITTER_ENABLE();

}

在原帖中查看解决方案

0 项奖励
回复
4 回复数
1,518 次查看
kubiznak_petr
Contributor V

Hi Florijan,

I don't think you can do so since function-like macros are evaluated from outside in, as explained here: Macro Call as Macro Argument . Hence SET_BIT is evaluated first, the preprocessor sees it requires two arguments, but only one is found. It ends up with error without evaluating the inner macro (TRANSMITTER_ENABLE_BIT).

I see only two solutions:

1:

#define SET_BIT(reg,bit_no)        reg |= (1<<bit_no)

#define TRANSMITTER_ENABLE_REG     SCI2C2

#define TRANSMITTER_ENABLE_BIT     7

 

void main(void){

      SET_BIT(TRANSMITTER_ENABLE_REG, TRANSMITTER_ENABLE_BIT);

}

2:

#define SET_BIT(reg,bit_no)      reg |= (1<<bit_no)

#define TRANSMITTER_ENABLE       SET_BIT(SCI2C2,7)

 

void main(void){

      TRANSMITTER_ENABLE();

}

0 项奖励
回复
1,517 次查看
Florijan
Contributor III

Hi,

thank you all for your help and time.

Erich thanks for info.

Petr, I will change the code like you suggested. Carlh, the brackets didn't help but thanks anyway for your time :smileyhappy:.

Best regards,

Florijan

0 项奖励
回复
1,517 次查看
BlackNight
NXP Employee
NXP Employee

While with C preprocessing things are 'textually' replaced, this does not apply to the macro argument expansion. You cannot pass an argument which then is expanded in to a list of argument (what you have in your code).

You need to supply the right number of arguments. What you could do is pass an 'empty' argument, as

SET_BIT(TRANSMITTER_ENABLE_BIT,);

(note the comma, for an 'empty' argument.

I think you need to rewrite your code as such that it is using two arguments.

Unless someone else has a different solution?

1,517 次查看
carlh
Contributor I

Wouldn't brackets help here?

#define TRANSMITTER_ENABLE_BIT   (SCI2C2, 7)

0 项奖励
回复