Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to tell through MPLAB whether a constant is defined at run time?

Status
Not open for further replies.

deweyusa

Member
I am trying to figure out if various Microchip constants that were the result of a #define earlier in program execution were defined or not at run time while I'm stepping through code with a debugger. Is there an easy way to do that? Can I somehow add the constants names to the watch window or something? MPLAB doesn't seem to want to let me do that, or I'm doing it wrong.

Thanks for any help!
-Josh
 
Last edited:
I don't really see the point?, why would you want to check a constant? - it's 'constant' that's the idea of it.

Not a very helpful answer Nigel. There have been plenty of times when I wanted to check the value of a constant that's derived from a formula in the define statement.

Hi Josh. You can probably use an instruction that stores the constant value in a variable to check the value. Then delete that instruction afterwards.

Cheerful regards, Mike
 
I don't really see the point?, why would you want to check a constant? - it's 'constant' that's the idea of it.

You've obviously never looked at some of the Microchip code. You can spend a long time and have to look through several files to find out if a constant is defined or not. There USB code is about the worst example I've seen for this.

Mike.
 
Thanks for the help folks. Yeah, tell me about it when it comes to Microchip and their love of constants. Mike (K8LH), you're saying to transfer the constant's value to a variable, and then track the value of the variable in the watch window? I never thought of that...brilliant!
 
if you want to know if it was DEFINED or not, you can do that at compile time. I do that a good bit when I'm not 100% certain that certain defines are valid inside of header files. For instance, some code might say something like:

Code:
#if USE_SHIFT_METHOD
   variable >>= 3;
#else
   variable /= 8;
#endif

What you asked was how to you tell if the define was defined at that point...

Code:
#if USE_SHIFT_METHOD
   #warning using SHIFT method
   variable >>= 3;
#else
   #warning using stupid divide :(
   variable /= 8;
#endif

Or something else you can do is

Code:
#ifdef USE_SHIFT_METHOD
   #warning using shift method
#else
   #error stupid divides! // the error directive will cause the compile to fail
#endif

I don't know if this is what you actually want to do or not, but #define's don't always have a value. For instance, if you compile in debug mode, __DEBUG is defined, but it isn't defined as anything. I don't know if you can transfer that to a variable as in variable=__DEBUG, but I do use it to do things like

Code:
General_Exception_Handler( stuff that goes here )
{
#ifdef __DEBUG
   software breakpoint assembly instruction
#endif
}
 
Last edited:
Another option is to use a real constant instead of a #define

Code:
const int ThisConstThing =  any valid compile time expression

I think I checked in C18 and it generates the same code size if not the identical code as the #define.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top