View previous topic :: View next topic |
Author |
Message |
zlittell
Joined: 08 Feb 2012 Posts: 13
|
Defining variables according to state of input |
Posted: Sat Feb 11, 2012 2:03 am |
|
|
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: 19510
|
|
Posted: Sat Feb 11, 2012 2:50 am |
|
|
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
|
|
Posted: Sat Feb 11, 2012 3:31 am |
|
|
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
|
|
Posted: Sat Feb 11, 2012 3:41 am |
|
|
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: 19510
|
|
Posted: Sat Feb 11, 2012 4:53 am |
|
|
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.
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 |
|
|
|