Hi,
there's one pin depending on his level (high or low), the programm should / or should not transmit all the rs232 debug messages (printf()) to the hyperterminal.
How is it possible to use some #ifdefs | or is it much better to use a global variable?
#ifdef DEBUG_MODE //debug mode - transmit all msg to the hyperterminal #else //no debug messages #endif int main(void) { if(AT91C_BASE_PIOA->PIO_PDSR & 0x10) { /* -> transmit rs232 debug messages */ } else { /* do not transmit any debug messages */ } printf("only tansmit the msg if DEBUG_INIT is true"); while(1); }
best regards Tim
Thank you for your time & information.
The canonical method is
#ifdef ENABLE_DEBUG_PRINTOUTS # define DEBUG_PRINTF(args) printf args #else # define DEBUG_PRINTF(args) /*nothing*/ #endif /*...*/ DEBUG_PRINTF(("outch, that %s hurt!\n", symptom));
Note: the double parentheses are important.
You don't say! Seriously, though: it's pointless to worry about time or space efficiency after you've started using printf().
That's the point. So maybe it's much better to decide at compiler time if I want to see the debug messages or not (without of the use of the pin state). But how is it possible to do that with #ifdef?
how it would be possible to get the same behaviour using #ifdefs
It isn't, because it can't be the same behaviour. It's reasonably easy to have a compiler switch that turns debug output on or off, but impossible to have one react to run-time events.
But then I have to use for every possible rs232 message an if clause determining the pin's state (or the global variable)?
You want to write a function debug_printf() that does the check, then calls vfprintf() as needed.
That will cost time and increase the code size...
Thank you for your answer. Could you tell me how it would be possible to get the same behaviour using #ifdefs (without the information of the pin's state) at compile time?
You already have a global variable: that pin's state.
That's right. But then I have to use for every possible rs232 message an if clause determining the pin's state (or the global variable)?
if(debug_global_var == 1) printf("next debug information");
How is it possible to use some #ifdefs
It isn't. Not for what you're trying to do. #ifdef is evaluated at compile time. They have no relation whatsoever to run time influences like port pin states.
or is it much better to use a global variable?
View all questions in Keil forum