avr-gcc-list
[Top][All Lists]
Advanced

[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 15:19:18 -0800

Hi James,

Just a word of caution.

Using 

    #define     SETPIN(pindef)  PINDEF_PORT(pindef) |=
PINDEF_PIN(pindef)

and passing in DATA1 which is defined as a comma separated list, isn't a
portable construct. Gcc happens to compile it (I tried 3.3.3 and 3.4.1
which both worked), however other compilers will probably complain. It
would be better to rework them slightly to look like this (I also
renamed a couple of things):

//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(port,pin)        PORT(port,pin) |= PIN_MASK(port,pin)

//Clear a pin
#define CLRPIN(port,pin)        PORT(port,pin) &= ~PIN_MASK(port,pin)

//Set a IO to output
#define SET_DDR_OUT(port,pin)   DDR(port,pin) |= PIN_MASK(port,pin)

//Set a IO to input
#define SET_DDR_IN(port,pin)    DDR(port,pin) &= ~PIN_MASK(port,pin)

//Read the value of a pin
#define GETPIN(port,pin)  PIN(port,pin) & PIN_MASK(port,pin)

- With these redefinitions PORT, DDR, and PIN reflect the PORTx, DDRx,
and PINx registers.
- PIN_MASK is more intuitive to me, and allows PIN to be used as the
register.

DDR_OUT and DDR_IN don't really have a verb, so adding the word SET
makes them more like SETPIN/CLRPIN. You might also want to consider:

    SETDIR_IN, SETDIR_OUT

--
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/ 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]