Make your own AVR JTAG debugger

Posted: July 20, 2011 in Tutorials
Tags:

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.

The EAGLE schematic and board files can be downloaded here.

If you don’t have EAGLE, download the files in PDF format here.

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.

Here’s the hex file you need to upload.

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


Select JTAG ICE as platform and select the appropriate COM port.

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


This is configured for internal 1Mhz clock and enabling the JTAG.

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.


#include

void main()
{

DDRA=0xFF;

/* Infinite loop */
while(1)
{
PORTA=0xff;
PORTA=0×00;

}

}

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.

About these ads
Comments
  1. Hello,
    does this still work with AVRStudio 5.0? I’ve heard some people reporting that it does not work anymore (plus those cheap AVR JTAG ones on ebay for $14).

    Thanks for reply, OndraSter :)

  2. 賴建宏 says:

    I can’t let AVR studio 4 detect my device! I can download the HEX code by AVR mkII ISP successfully, and set the JP1 to VCC.

    But, my Vcc = 3.3V, is there anything I missed?

  3. Hello, What is the logic level to X1 connector in the schematic, Is it TTL or RS232…?

  4. Ruslan says:

    Why we are need in jamper 3, what he doing?

  5. Ruslan says:

    And why we dont use max232?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s