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?
do{ if(NEG) (ADDRESS &= ~(1<<<BIT)); else (ADDRESS |= (1<<BIT));}while(0)- BLUEPIXY#are issued, before even your programme is compiled, so definitely before a variable likeNEGmeans anything. While using#if, you either have to use an integer constant like1or0, for true and false, respectively; or a macro definition like#define ASD 1, and use theASDin place of1, and so on... - Utkan Gezerdo {} 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