View previous topic :: View next topic |
Author |
Message |
zeeshan ahmed
Joined: 02 Dec 2010 Posts: 22
|
Help needed regarding #define function/directive |
Posted: Mon Sep 05, 2011 5:17 am |
|
|
Greetings,
Hi all, I am working on a project, here i got a problem in #define function.
Compiler is giving warning message i.e: "Duplicate defines".
Because of this i think my code is not working as it is supposed. It allways uses the last #define expression. Although it reads the input correctly. The LED's connected at ports (C1 & C2) glows correctly. Please tell me the right way to do this.
Thanks Alot.
Note: Compiler Version= PCWHD 4.124, controller=18f252
Code: | VOID STRB_SEL(void)
{
if(input(pin_c6))
{
output_low(pin_c2);
#DEFINE STB_LOW output_LOW(pin_a1) // ARL compatible
#DEFINE STB_HIGH OUTPUT_HIGH(pin_a1)
}
else
{
output_low(pin_c1);
#DEFINE STB_LOW output_high(pin_a1) // AEI compatible
#DEFINE STB_HIGH OUTPUT_low(pin_a1)
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Mon Sep 05, 2011 8:05 am |
|
|
I think you are misunderstanding 'what' #define is.
#define, is a _preprocessor_ macro directive. Not a code instruction. Your 'if' statement is a code 'if', so has no effect on the define, and the compiler attempts to process both, hence the 'duplicate #define' error.
There is a preprocessor 'if' (look at #if), _but_ this can't use a input from a pin. The #definea are all processed at _compile_ time, before the code ever gets into the processor....
You appear to be wanting to reverse the direction of two strobe commands at run time. If so, something like:
Code: |
int8 bit_to_output=1; //default
#define STB_LOW output_bit(pin_a1,bit_to_output)
#define STB_HIGH output_bit(pin_a1,bit_to_output^1)
VOID STRB_SEL(void) {
if(input(pin_c6)) {
output_low(pin_c2);
bit_to_output = 0;
}
else {
output_low(pin_c1);
bit_to_output = 1;
}
}
|
Best Wishes |
|
|
zeeshan ahmed
Joined: 02 Dec 2010 Posts: 22
|
|
Posted: Tue Sep 06, 2011 5:59 am |
|
|
Many thanks for your suggestions, and enlightening me about #define directive, now the code is working correctly
Again thanks for your time. |
|
|
|