Weird macro Requirement

Status
Not open for further replies.

PICMICRO

New Member
I often use macro to define pins, like
#define output RA5
So that, in the code I could use, like
output = 1;
I do this so that If at any time I need to change the pin, I need to only correct the #define part.
Thats good and everyone seems to do that.
But thats not all of the story.What I am concerned is, you need to set TRIS register as well. So, often I find myself doing
...
TRISA5 = 0;
output = 1;
...
which completely ruins the purpose of defining macro.

What I am searching is something like this
...
#define output A5
...
...
TRISoutput = 0;
Routput = 1;
WPUoutput = 1;
...
... //etc

Of course, it won't work that way, so I am searching for workaround?
 
Why not use 2 defines? like:
Code:
#define OUTPUT RA5
#define OUTPUT_TRIS TRISA5
...
OUTPUT_TRIS = 0; //SET PIN TO OUTPUT
OUTPUT = 1;         //SET PIN HIGH
 
Why not use 2 defines? like:
Code:
#define OUTPUT RA5
#define OUTPUT_TRIS TRISA5
...
OUTPUT_TRIS = 0; //SET PIN TO OUTPUT
OUTPUT = 1;         //SET PIN HIGH
Because there are sometimes too many.
#define INPUT1 RA5
#define INPUT1_TRIS TRISA5
#define WPU_INPUT1 WPUA5 // weak pull up
#define IOC_INPUT1 IOCA5 // interrput on change
#define INPUT2 RA4
... // so on

But for the moment, thats my only way.
 
That's not a macro, that's a definition.

A macro would look like this:
Code:
Set_Output      macro
       movlw    b'00000001'
       movwf    TRISA, acc    
       bsf      PORTA, 5, acc
endm
"Set_Output" is the name of the macro, "endm" is the end of it.

Then when you want to execute the macro in your code, you write
Set_Output
And the compiler just pastes that block of code there.

You can give a macro variables, like this:
Code:
setb    macro      set_reg, bit
        bsf        set_reg, bit, acc
        endm
You would use this one like this:
setb PORTA, 5
and it gives the variables to the instructions during the compile.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…