printf in non-debug/programming mode

Hello all,

I was just curious if anyone knew what happens when you use a printf but don’t have the cable connected. Does it still run the statement? If so, is there any easy way to disable this so that I can basically comment out the code when in RUN mode but not in PROG mode?

Thanks!

even if the cable is unpluged it still will run the printf code.

//when you want to use your pintf’s uncomment this line of code
//#define DEBUG
//when you are done using printf’s comment out the above code.

// put this section of code where you want to use a printf.
#ifdef DEBUG
printf(“bla…bla…bla…”);
#endif

The bios detects if the programming/debug cable is attached at startup and runs in debug mode enabling functions like printf to communicate with Dynamic ‘C’. When the cable is not attached the bios runs in run mode and functions like printf do nothing. This behaviour can be altered by redirecting printf to a serial port.

Instead of using
#define DEBUG

it would be better to determine programmatically if running in run mode or debug mode. The bios stores this state in a variable called OPMODE which can be tested with

debugMode = (OPMODE & 8) == 8;

or you can directly read the status of the SMODE pins by reading the SPCR register.

debugMode = (RdPortI(SPCR) & 0x60) == 0x60;

If you want to minimize your final non-debug code size, you can automatically turn the printf’s on and off based on the Dynamic C setting for RST 28 instructions (under Options / Project Options / Compiler / Advanced / “Include RST 28 instructions”) like this:

#if (DEBUG_RST == 1)
/* warning to not forget to turn of RST 28 for production code */
#warnt DEBUG RST 28 INSTRUCTIONS ENABLED!
#define DEBUG_MSGS 1
#endif

And then encapsulate the printf’s like this:

#ifdef DEBUG_MSGS
printf( "Got to here
" );
#endif