NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Define debug comments

May 22, 2013
by dvdsnyd
dvdsnyd's Avatar

I am not quite sure how to word this question, so please bear with me...

Essentially, I am wondering if it is possible to use a define statement, or some variant of that would allow certain comments to be printed either to the LCD or to a terminal window to check a calculation or variable... Some pseudo-code for reference...

#define debug 0
#define debug 1

if debug == 1;
    //print x to LCD
    //print y to LCD
    //print z to LCD
endif

It would be ideal if one could set a debug flag in when calling for the program to be compiled and written to the microcontroller... If anyone could shed some light on to this it would be great. Let me know if you have any questions. I can try to form my thoughts a little more clear.

Thanks a lot Dave

May 22, 2013
by Noter
Noter's Avatar

You can do it like your example and the debug code will always be compiled and included in the hex file but will only execute if your debug variable is a 1. An alternative is to use the preprocessor to only include and compile the debug statements into the hex file when a debug value is defined. To turn off debugging and exclude those statements from the compile and hex, just remove or comment out the define. If you've ever heard the term "conditional assembly", this is what it's about.

#define DEBUG

#ifdef DEBUG
  print ...
  print ...
#end if

The C preprocessor is very cool and worth learning.

May 22, 2013
by Noter
Noter's Avatar

If you look back at the sample code I posted for the DHT22 you will see a couple examples of using the preprocessor to control which source lines are included in the compilation. It's also used for the pin io macro include file to ensure that code is included only once regardless of how many times it appears in an #include statement. Sometimes an include file is needed by other include files but can only actually be included once in the compile or redefinition errors will occur.

The C Preprocessor

May 22, 2013
by pcbolt
pcbolt's Avatar

Noter -

I traced that link back from your response to my last question and your right...it's very cool (man programmers are lazy :-).

David -

If you want to change the value of DEBUG from a makefile you can add the -D flag inside the "avr-gcc" section of your makefile. There are several way to do this but the simplest is to add a variable to the makefile and have two definitions for it (one commented out).

If you take the NK tempsensor makefile as an example, here is it's original content...

GCCFLAGS=-g -Os -Wall -mmcu=atmega168 
LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt -lm
AVRDUDEFLAGS=-c avr109 -p m168 -b 115200 -P /dev/ttyUSB0
LINKOBJECTS=../libnerdkits/delay.o ../libnerdkits/lcd.o ../libnerdkits/uart.o

all:    tempsensor-upload

tempsensor.hex: tempsensor.c
    make -C ../libnerdkits
    avr-gcc ${GCCFLAGS} ${LINKFLAGS} -o tempsensor.o tempsensor.c ${LINKOBJECTS}
    avr-objcopy -j .text -O ihex tempsensor.o tempsensor.hex

tempsensor.ass: tempsensor.hex
    avr-objdump -S -d tempsensor.o > tempsensor.ass

tempsensor-upload:  tempsensor.hex
    avrdude ${AVRDUDEFLAGS} -U flash:w:tempsensor.hex:a

Change this to read (note changes on lines 5 and 10)...

GCCFLAGS=-g -Os -Wall -mmcu=atmega168 
LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt -lm
AVRDUDEFLAGS=-c avr109 -p m168 -b 115200 -P /dev/ttyUSB0
LINKOBJECTS=../libnerdkits/delay.o ../libnerdkits/lcd.o ../libnerdkits/uart.o
DBUG=-DDEBUG=1
#DBUG=-DDEBUG=0

all:    tempsensor-upload

tempsensor.hex: tempsensor.c
    make -C ../libnerdkits
    avr-gcc ${GCCFLAGS} ${LINKFLAGS} -o tempsensor.o tempsensor.c ${LINKOBJECTS} $(DBUG)
    avr-objcopy -j .text -O ihex tempsensor.o tempsensor.hex

tempsensor.ass: tempsensor.hex
    avr-objdump -S -d tempsensor.o > tempsensor.ass

tempsensor-upload:  tempsensor.hex
    avrdude ${AVRDUDEFLAGS} -U flash:w:tempsensor.hex:a

Now you should have a value defined automatically in your source code, so you can go back to your original code (you won't need lines 1 and 2, and you should change "debug" to "DEBUG" just so you know it's not like other variables). Now you can comment out "DBUG=-DDEBUG=1" and un-comment "DBUG=-DDEBUG=0" to go back to your normal code. OR, you can override the variable "DBUG" from your "make" command line, like this...

make DBUG=-DDEBUG=0

Now the question I have for Noter is... if you take your code from 2 posts ago, eliminate line 1, what's the easiest way to alter the makefile so all you have to do is type "make -DDEBUG" from the command line?

May 23, 2013
by Noter
Noter's Avatar

The easiest way is to have a different recipe for debug. With the following you type 'make debug' whenever you want the debug variable defined.

GCCFLAGS=-g -Os -Wall -mmcu=atmega168 
LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt -lm
AVRDUDEFLAGS=-c avr109 -p m168 -b 115200 -P /dev/ttyUSB0
LINKOBJECTS=../libnerdkits/delay.o ../libnerdkits/lcd.o ../libnerdkits/uart.o

all:    tempsensor-upload

debug: DEBUG = -DDEBUG
debug: tempsensor-upload

tempsensor.hex: tempsensor.c
    make -C ../libnerdkits
    avr-gcc ${GCCFLAGS} ${LINKFLAGS} -o tempsensor.o tempsensor.c ${LINKOBJECTS} $(DEBUG)
    avr-objcopy -j .text -O ihex tempsensor.o tempsensor.hex

tempsensor.ass: tempsensor.hex
    avr-objdump -S -d tempsensor.o > tempsensor.ass

tempsensor-upload:  tempsensor.hex
    avrdude ${AVRDUDEFLAGS} -U flash:w:tempsensor.hex:a

Then in the program, test if DEBUG is defined ...

#ifdef DEBUG
  print ...
  print ...
#end if
May 23, 2013
by Noter
Noter's Avatar

BTW, there should be no space in #endif.

May 23, 2013
by Noter
Noter's Avatar

And, it's not a different recipe but a different rule. I think I'm awake now. :-)

May 23, 2013
by dvdsnyd
dvdsnyd's Avatar

This is exactly what I was looking for. You guys are awesome! Thanks a lot! I will take a look at this more when I get home this afternoon.

Thanks again!

David

May 23, 2013
by dvdsnyd
dvdsnyd's Avatar

I was able to get this to work a little bit ago. It will be nice to be able to go back and forth between seeing a bunch of debug prints to not with a simple recompile and chip write! Thanks for your help again! Dave

May 23, 2013
by Noter
Noter's Avatar

Glad it's working for you. It's amazing what you can do with the make utility. I keep the reference handy because I change make files so seldom I usually have to look up how to do things. This time I had to look up how to assign a value to a variable in a rule. GNU Make Manual

Post a Reply

Please log in to post a reply.

Did you know that you can build a giant scrolling LED array with our microcontroller kit? Learn more...