CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Help needed regarding #define function/directive

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
zeeshan ahmed



Joined: 02 Dec 2010
Posts: 22

View user's profile Send private message

Help needed regarding #define function/directive
PostPosted: Mon Sep 05, 2011 5:17 am     Reply with quote

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: 19506

View user's profile Send private message

PostPosted: Mon Sep 05, 2011 8:05 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 06, 2011 5:59 am     Reply with quote

Many thanks for your suggestions, and enlightening me about #define directive, now the code is working correctly Smile

Again thanks for your time.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group