1

I tried the following syntax to define a macro:

#define SETBIT(ADDRESS,BIT,NEG)  #if NEG                    \
                                   (ADDRESS &= ~(1<<BIT))   \
                                 #else                      \
                                   (ADDRESS |= (1<<BIT))    \
                                 #endif

Using:

#define LED1 PORTA,1,1   \\ LED with anode to Vcc
#define LED2 PORTA,1,0   \\ LED with anode to GND
SETBIT(LED1)             \\ resulting in bit clear
SETBIT(LED2)             \\ resulting in bit set

I hope that the main point is clear: I'd like to specify the port polarity only in one place and make the code more readable and scalable.

However GCC compiler complains for it:

ipt.h:1: error: '#' is not followed by a macro parameter (column 30)

I found out that #if is not allowed within a macro definition.

How am I able to declare the macro with indicated function?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Roman Matveev
  • 563
  • 1
  • 6
  • 22
  • 1
    `do{ if(NEG) (ADDRESS &= ~(1<< – BLUEPIXY Apr 11 '14 at 18:34
  • @BLUEPIXY Could you make clear why you're using do-while loop here? – Roman Matveev Apr 11 '14 at 18:37
  • Roman, you seem to have misunderstood preprocessor directives. Those things that start off with a `#` are issued, before even your programme is compiled, so definitely before a variable like `NEG` means anything. While using `#if`, you either have to use an integer constant like `1` or `0`, for true and false, respectively; or a macro definition like `#define ASD 1`, and use the `ASD` in place of `1`, and so on... – Utkan Gezer Apr 11 '14 at 18:47
  • E.g if(condition)SETBIT(PORTA,1,1);else otherthing; is prevented so as not to cause this error. – BLUEPIXY Apr 11 '14 at 18:51
  • 1
    What @BLUEPIXY means is that macros are inserted into existing code as replacement text without regard for meaning. The example he gives will get messed up by creating two ifs followed by two elses. The `do {} while(0)` trick makes sure that your macro is inside braces, but also allows it to be inserted as a statement. It can go anywhere without messing up existing code. The trick works because the "loop" will be executed exactly once. – UncleO Apr 11 '14 at 19:28
  • @UncleO But I thought the curle brackets will be enough. Isn't it? – Roman Matveev Apr 11 '14 at 19:40
  • See http://stackoverflow.com/questions/154136/do-while-and-if-else-statements-in-c-c-macros – UncleO Apr 11 '14 at 21:40

1 Answers1

2

Try something like this:

#define SETBIT(ADDRESS,BIT,NEG) \
  if (NEG)                      \
    (ADDRESS &= ~(1<<BIT));     \
  else                          \
    (ADDRESS |= (1<<BIT));
murrekatt
  • 5,961
  • 5
  • 39
  • 63