[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] Preprocessor question
From: |
Preston Wilson |
Subject: |
Re: [avr-gcc-list] Preprocessor question |
Date: |
Fri, 02 May 2008 23:12:40 -0400 |
User-agent: |
Microsoft-Entourage/11.4.0.080122 |
"Thomas D. Dean" wrote:
> I have a question about preprocessor syntax.
>
> I want to declare som macros that ease controlling port assignments.
>
> in the include file, I have
>
> #define GLUE(a, b) (a##b)
> #define PORT(x) (GLUE(PORT, x))
> #define PIN(x) (_BV((x)))
> #define DDR(x) (GLUE(DDR, x))
> #define SET_INPUT(x) ( GLUE(x, _DDR) |= ~GLUE(x, _PIN ) )
> #define SET_OUTPUT(x) ( GLUE(x, _DDR) |= GLUE(x, _PIN ) )
> #define SET_BIT(p,b) (p |= b)
> #define CLR_BIT(p,b) (p &= ~b)
>
> #define LEFT_MOTOR_DIR_PORT PORT(C)
> #define LEFT_MOTOR_DIR_PIN PIN(3)
> #define LEFT_MOTOR_DIR_DDR DDR(C)
>
>
> In the .c file, I have
>
> #include <avr/io.h>
> #include <macro.h>
> int main() {
> SET_INPUT(LEFT_MOTOR_DIR);
> return 0;
> }
>
> # avr-gcc -mmcu=atmega16 -Wall -Wmissing-prototypes -Os -I.
> -I../../../asus-avr-1.0/include -E -o xx macro.c
>
> and, in xx
>
> ...
> int main() {
> ( ((GLUE(DDR, C))) |= ~(((1 << ((3))))) );
> return 0;
> }
>
>
> The macro is fully expanded on the RHS, but, not the LHS
>
> What did I do wrong?
If a macro contains its own name (directly or via other macro expansion) it
will not be expanded again.
In the case of LEFT_MOTOR_DIR_PIN there is not another GLUE() "call"
In the case of LEFT_MOTOR_DIR_DDR there is another GLUE() "call", so that
second call is not expanded.
-Preston