i know, there are many posts on it, but i still do not get it. I want to simplify port handling an a microcontroller with #define. Usually you do like this
DDRA |= (1 << PORTA0) // set pin 0 at port A as OUTPUT
PORTA |= (1 << PORTA0) // set pin 0 at port A
"PORTA", "DDRA" and "PORTA0" are macros of the avr/io.h which expand to numbers.
Currently i do like this:
// pinControl.h
#define setPin(port, pin) (port |= (1 << pin))
#define setOutput(ddr, pin) (ddr |= (1 << pin))
// some other file.h
#define myPinPort PORTA
#define myPinDDR DDRA
#define myPin PORTA0
setOutput(myPinDDR, myPin)
setPin(myPinPort, myPin)
now, if the port changes and i have lots of pins, i need always to change every DDRA and PORTA to e.g. to DDRB and PORTB. I'd like to simplify it like this:
// pinControl.h
#define stringify1(x) #x
#define stringify(x) stringify1(x)
#define setPin(port, pin) (stringify(PORT)##port |= (1 << stringify(PORT)##port##pin))
#define setOutput(port, pin) (stringify(DDR)##port |= (1 << stringify(PORT)##port##pin))
// some other file.h
#define myPinPort A
#define myPin 0
setOutput(myPinPort, myPin); // shall expand to "DDRA |= (1 << PORTA0)";
setPin(myPinPort, myPin); // shall expand to "PORTA |= (1 << PORTA0);"
I am really confused with the syntax to get this result out :(:(