|Debugging Your Program|
Online Library Reference
NOTICE: THIS IS AN ARCHIVAL COPY OF THIS COURSE. For the latest version, please visit our interactive classroom.
There are two ways to debug your code using software simulation on the PC. If you are running Windows, you can use AVR Studio 4 to debug. The advantage is that the simulator exposes all of the ATmega 8 internal hardware.
To prepare for this type of debugging, use "make extcoff" which will generate a .cof file for your project. Then launch AVR Studio. Use the Open button on the welcome dialog to select the .cof file (tutor.cof, in this case). Don't select the hex file since it has no debugging information.
You'll see this screen:
Leave AVR Simulator selected in the left box and pick ATmega 8 in the right box. Then click Finish.
You'll wind up with a great debugging session:
If you prefer you can use the graphical version of gdb to debug your program (or you can use gdb if you prefer). However, the gdb simulator is not quite as complete as AVR Studio's. Assuming you can run an X program, enter "make debug" to start insight:
This actually launches two programs. One is the simulator and it listens on a TCP port (usually port 1212, but you can verify this by reading the start up message in the simulator window). The other window is insight itself (seen above).
To start the debugging session, use the Run | Connect to Target. A dialog will appear the first time you do this:
The target setting, hostname, and port entries should all match the picture above (assuming your simulator is listening on port 1212). Press OK. Then use the Run | Download menu to send your program to the simulator. Finally, use Control | Continue to start executing the program. Don't use the Run command since the simulator is already running your program.
In either case, debugging this program won't be very much fun. Since the UART is not simulated, the program will hang while trying to perform serial output. You can manually move the program counter (or, if you are using AVR Studio, manually set the UART bits) to avoid these traps. However, it is easy enough to conditionally put debugging statements into your program:
#if DEBUG // don't do anything #else printf(....); #endif
As usual, debugging won't cure everything, but it can be very helpful.
Next, we'll make a few changes and explore some optimizations.