Tired of putting LEDs every time you want to check some value in the microcontroller? Well, its time to build yourself a debugger.
A debugger is a device which helps you run through your code in the microcontroller step by step and also gives you the ability to read or write the registers directly using the PC which means you can check the value of any register or any variable at any time without the need for any external components like LEDs or seven segment displays. A debugger will help you immensely when you’re in the prototyping phase of any project. Since I almost entirely use AVR, I’ll be showing you how to build an AVR debugger. AVR has many debuggers like the AVR Dragon, AVR One, AVR JTAGICE mkII etc. The problem is they cost hundreds of dollars and are hard to get in India. The AVR JTAGICE(not mkII) however, has been made open source by Atmel and therefore anyone can build it. The only drawback of this debugger is that it supports only a limited number of microcontrollers but hey its open source and dirt cheap to build, so what did you expect?
Compatible devices with AVR JTAGICE:
ATmega16(L), ATmega162(L), ATmega169(L or V), ATmega32(L), ATmega323(L), ATmega64(L), ATmega128(L).
I borrowed the schematic from http://aquaticus.info/jtag
Since I don’t own an ancient PC, I was forced to use a USB to serial converter. If the converter is TTL compatible, which it will be in most cases, you won’t need the level shifter IC (MAX232). I also wanted the debugger to act as a programmer for non-JTAG capable devices, so I had to create everything from scratch on EAGLE and make a board layout.
Etch the PCB using the layout provided(If you don’t know how to etch a PCB, see this tutorial). You can also try the circuit on a breadboard but if you’ll be using it all the time it’s better to make it on a PCB since it’ll be much more compact and robust.
Here are a few pics
And here’s the schematic if you just want to build it on a breadboard
It’s important that you use a 7.3728Mhz crystal and not 8Mhz as it will affect the baud rate and the debugger won’t work.
After you’re done building the circuit, its time to upload the firmware to the microcontroller(ATMEGA16). You can use any programmer you currently have like USBasp or USBtiny etc.
And the fuse settings should be set to
HFuse – D8
Lfuse – FF
Now put the microntroller in the JTAG circuit and connect the USB to serial cable to the board.
Since the board doesn’t have its own power supply, you’ll have to provide it from an external source.
To test if the board is working you’ll have to give 5V to the board by connecting a 10pin cable to one of the headers(JTAG or AVR ISP). Make sure you use a regulated 5V source or you might damage your USB port.
Now go to AVR studio(No need to create a new project) > Tools > Program AVR > Connect
You should see the following window
Now press connect. If everything is correct, you should see the following message.
If not, check for the following
1. Check whether the jumper is set in the correct position. If it doesn’t work, try changing the position.
2. Check if you’ve selected the correct COM port.
3. Check all connections and make sure there are no shorts or breaks.
If it still doesn’t work you might have programmed the ATMEGA16 incorrectly or entered the wrong fuse settings. So try to reprogram it again and make sure everything is correct.
If everything is working, take another ATMEGA16, the one which you want to debug and configure the fuse settings as follows
HFuse – 19
Lfuse – E1
Now just connect the jtag cable to the microcontroller with the following wiring
Create a new project in AVR studio, select avrgcc
Now select JTAGICE in the debug platform and ATMEGA16 as the device
You can write whatever code you want to test, here’s a simple example of blinking an LED.
/* Infinite loop */
Build the program. Go to tools>start debugging, if everything goes well, you’ll see a yellow pointer towards the starting of the code. You now have a working debugger. To step through the program press F11 or go to tools and select one of the many available options to step through the code.
Here’s a short video showing the JTAG debugger in action.
If you have any comments or questions leave them below.