0

Sorry for my lack in knowledge about preprocess code and macro definition, but i'm trying to work on #define macros in order to make more efficient my avr code. I want to develop a driver routine to manage a 1-wire digital thermometer. My purpose is to obtain a preliminar definition of port and pin registers to be reused in my avr library and i would like to make all these preliminar definitions through macros. I'll explain better:

I'd like to have a definition function like that:

#define (port, pin)

and to use port and pin parameters to define other basic macros like:

#define DRIVER_PORT PORT##port
#define DRIVER_PIN PIN##port##pin
#define DRIVER_DDR DDR##port

So, from here on out i'd be able to use the value DRIVER_PORT to manage input/output/high-z port pins, DRIVER_PIN to read port value and so on...

Googling over, I've produced this non-working code:

[...]
#define define_pin(port,pin) { \
   #define DRIVER_PORT PORT##port \
   #define DRIVER_PIN PIN##port##pin \
   #define DRIVER_PINR PIN##port \
   #define DRIVER_DDR DDR##port \
}
[...]

1° question: Is it possible to obtain what i'm pursuing? 2° question: If so, what's the right and best method to obtain that?

Thank's in advance for the help!

Buzz
  • 1,102
  • 1
  • 9
  • 24
  • 1
    No, you can't have a macro define other macros. And you want to avoid the use of macros wherever possible, which I am sure is the case here. –  Mar 10 '18 at 18:33

1 Answers1

1

Hi you can not do so in C++ or in C as well. In C++ to avoid this kind of macro definition and its expansion during compile time, it introduced inline method which is much easier to use. Better use inline method instead of macros in C++.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17