[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [avr-gcc-list] Question about gcc preprocessing and port/pin assignm
From: |
Dave Hylands |
Subject: |
RE: [avr-gcc-list] Question about gcc preprocessing and port/pin assignments |
Date: |
Mon, 6 Dec 2004 16:03:33 -0800 |
Hi James,
Thanks for pointing out (off list) that this doesn't actually compile
using gcc - Doh - That'll teach me to post without testing :)
OK. So here's a way that does work under gcc and a non-gcc compiler that
failed the original way. There are a few sublte changes, like requiring
parentheses around the data pin definitions and removal of parenthesis
around the PORT and PIN_MASK usage in say SETPIN.
Adding the extra set of parenthesis
SETPIN(DATA1) becomes SETPIN((D,7)) which causes only a single
argument of (D,7) to be passed in.
PORT pindef become PORT (D,7) - which is where the
parenthesis come back into play.
//Example defines
#define DATA1 (D, 7)
#define DATA2 (C, 3)
#define DATA3 (B, 3)
//Code to manipulate PORTx, PINx, and DDRx using definitions like
//those above
#define PORT(port,pin) (PORT##port)
#define DDR(port,pin) (DDR##port)
#define PIN(port,pin) (PIN##port)
#define PIN_MASK(port,pin) (1<<(pin))
//Set a pin
#define SETPIN(pindef) PORT pindef |= PIN_MASK pindef
//Clear a pin
#define CLRPIN(pindef) PORT pindef &= ~PIN_MASK pindef
//Set a IO to output
#define SET_DDR_OUT(pindef) DDR pindef |= PIN_MASK pindef
//Set a IO to input
#define SET_DDR_IN(pindef) DDR pindef &= ~PIN_MASK pindef
//Read the value of a pin
#define GETPIN(pindef) PIN pindef & PIN_MASK pindef
SETPIN( DATA1 )
So, this should now be completely portable. If you really object to the
parenthesis, then the original
#define DATA1 D,7
#define SETPIN(pindef) PORT(pindef) |= PIN_MASK(pindef)
Will work in gcc, but may fail in other compilers. You can make your own
trade off.
--
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/
RE: [avr-gcc-list] Question about gcc preprocessing and port/pin assignments, Dave Hylands, 2004/12/08
RE: [avr-gcc-list] Question about gcc preprocessing and port/pin assignments, Dave Hylands, 2004/12/08
RE: [avr-gcc-list] Question about gcc preprocessing and port/pin assignments, Dave Hylands, 2004/12/08