James Newton wrote: > One of THE most brilliant things I've ever seen. >=20 > #define _TRIS(pin) pin(_TRIS_F) > #define _TRIS_F(alpha,bit) (TRIS ## alpha ## bit) > #define _PORT(pin) pin(_PORT_F) > #define _PORT_F(alpha,bit) (R ## alpha ## bit) > #define _LAT(pin) pin(_LAT_F) > #define _LAT_F(alpha,bit) (LAT ## alpha ## bit) > #define _WPU(pin) pin(_WPU_F) > #define _WPU_F(alpha,bit) (WPU ## alpha ## bit) >=20 > #define pinSwitch(f) f(A,2) //Switch, INPUT , pin RA2 > #define pinLed(f) f(B,5) //Led, OUTPUT , pin RB5 >=20 > _TRIS(pinSwitch) =3D 1; //make pin an input > _WPU(pinSwitch) =3D 1; // turn on internal pull-up > _TRIS(pinLed) =3D 0; // make pin an output >=20 > while(1){ =20 > _PORT(pinLed) =3D ! _PORT(pinSwitch) > } >=20 > Why is it so brilliant? Because now, you can write a standard code librar= y, > without any pin assignment crap in it, and simply include a standard .h f= ile > (pins.h for example) which you make for each project. In that file, you d= o > the=20 >=20 > #define pinSwitch(f) f(A,2) //Switch, INPUT , pin RA2 I do this a bit different -- but also using macros :) When writing a generic module that uses any processor resources (I/O pins or other special registers), I use macros for these items in the module. The module header then has a section where I describe the required macros. Compiler is set up to fail if a macro is missing. The application using the module needs to provide the macros. This becomes then as simple as requiring e.g. macros SET_MODULE_OUT and GET_MODULE_IN, which in the hardware definition of a project could then be defined as #define SET_MODULE_OUT( arg ) LATB4 =3D !arg #define GET_MODULE_IN() RB3 (LATB4 and RB3 are Hi-Tech's names for the port pins. RB3 is port B3.) And the while loop would be =20 while( 1 ) { SET_MODULE_OUT( !GET_MODULE_IN() ); // becomes LATB4 =3D !RB3; } One advantage is that there are no (reasonable) limits to the complexity of the macros, so if on a given hardware you need to do something different, you can do that without messing with the module code. Like in this case change the polarity of the output. Gerhard --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .