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

Defining variables according to state of input

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



Joined: 08 Feb 2012
Posts: 13

View user's profile Send private message Visit poster's website

Defining variables according to state of input
PostPosted: Sat Feb 11, 2012 2:03 am     Reply with quote

I need to define a variable depending on the state of an input. This has to be done with preprocessor commands though because I will use the value of this variable to include certain header files.

Is there anyway to do this? I cannot find anything, but honestly I don't know exactly how to word it for searching haha.

PIC: 18F4550

-zack-
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Sat Feb 11, 2012 2:50 am     Reply with quote

What you are asking, does not make any sense.
An 'input' doesn't have a 'state', till code is running. This is long after compilation, and long after the code has left the PC, and any header files far behind...
If you want a variable that changes type in a running program, then the way to do this is to use a pointer, and (if necessary because the size changes), malloc to allocate the space it needs.
You can have #define that enables different headers to load (look at almost any of the examples, where settings are changed according to the compiler type), but this has to be done at compile time, not depending on an input.

This is not a CCS limitation, but fundamental to the basis of compiled code rather than interpreted code. On an interpreted program the whole program is present in the chip running it. On an compiled program, only the resultant machine code is present.

Best Wishes
zlittell



Joined: 08 Feb 2012
Posts: 13

View user's profile Send private message Visit poster's website

PostPosted: Sat Feb 11, 2012 3:31 am     Reply with quote

Wow as soon as I started reading your reply I felt extremely stupid haha. Its been a long day of coding and I can't believe I made a mistake like this. I have been looking at the code without thinking about the PIC for far too long with this new project. Thank you for pointing out the painfully obvious mistake I have made haha.
zlittell



Joined: 08 Feb 2012
Posts: 13

View user's profile Send private message Visit poster's website

PostPosted: Sat Feb 11, 2012 3:41 am     Reply with quote

I guess I was forgetting that it compiled the includes and then loads it onto the chip. In the perfect world inside my head I was thinking i could get it to load each include into the pic and use the variable assignments inside the file depending on the state of an input. Ahhhh sometimes its so easy to forget that everything doesn't work the best way for your specific project. I guess I am going to have to brain storm this one a bit.
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Sat Feb 11, 2012 4:53 am     Reply with quote

We have all had days like that... (sometimes weeks).
Anyone who has programmed for a time, and has _not_ had occasions where you 'head off' in an invalid direction, is either lucky, or kidding themselves. Smile

One suggestion might be if (for instance) you wanted to load a suite of definitions to allow language switching, consider storing these on an EEPROM, and having the code only the ones required.

Best Wishes
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