ATmega16 ADC / JTAG Ice MkII issue

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello again, everyone!

 

Working on a small project and have several different parts of it working correctly but my ADC doesn't appear to be acting in the way I expect it to (ie, it's not working correctly).  Unfortunately, I'm working at home away from most of my development tools so I have with me an STK500, an ATmega16, a JTAG Ice MkII, and Atmel Studio 7 (oh, and a scope and multimeter). 

 

Unfortunately, my ADC isn't working correctly and it looks like my JTAG Ice MkII is not wanting to play nice so I can't really see where the issue is when debugging.  This leaves me with 3 options:

 

1)  Get help with the code itself

2)  Get help getting my debugger working so I can look closer at the code

3)  Wait until Monday when I have access to more tools/debuggers

 

For all intents and purposes, I'll place my code here.  I have it running on AS7 and have the fuse set to run on the internal 1 MHz clock.

#define F_CPU 1000000L

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
	// Set PDB0 as output
	DDRB = 0x01;
	
	//ADC must be between 50 and 200 kHz.  Select ADC divider as 8 for 125 kHz
	ADCSRA |= (1 << ADPS1) | (1 << ADPS0);
	
	ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
	ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading

	// ADC0 Selected - ADMUX -> MUX[4:0] = 0

	// Set ADC to Free-Running Mode - SFIOR -> ADTS[2:0] = 0

	ADCSRA |= (1 << ADEN);  // Enable ADC
	ADCSRA |= (1 << ADSC);  // Start A2D Conversions

	for(;;)  // Loop Forever
	{
		if(ADCH < 128)
		{
			PORTB = 1;
		}
		else
		{
			PORTB = 0;
		}
	}
}

What I'm expecting to happen is I connect Port A Pin 0 to gnd, the ADC will read low (<128) and have the LED I have connected to Port B pin 0 to be turned off.  If I connect it to power, ADC will read high (>=128) and the LED will enable (because it's active low, if I remember correctly). 

 

Unfortunately, when I run my code, regardless of whether I connect it to GND or "VTG" (5V), the LED remains on.  So I'm clearly doing something wrong, unfortunately (or I have an overflow condition with one of those on the ADC).  The code is simple enough that I wasn't expecting any big problems at this point. 

 

When trying to get the debugger, I get the following problem:

 

1)  When connecting the JTAG debugger to the 10 pin ISP on the board (still have the 6 pin ISP connected to the red connector on the STK500 to program the ATmega16), I actually get everything powering down on the board and the red power light blinking on and off

2)  When I connected it backwards, the two red and one green leds on the JTAG Ice come on (which, with my experience in the past usually means it's connected correctly, which is odd), LED5 and 6 light up (connected to PB5 and PB6) but when I try and connect to it via the device programming menu, I get the following error:

 

Unable to connect to tool JTAGICE mkII (00B000002DEB)

At this point, I'm kind of worried that I might damage the JTAG Ice (if it wasn't damaged already) so I'm hesitant to move forward with this without understanding what the problem is (maybe I have a connector that needs to be removed first, maybe the JTAG Ice is just not working correctly, which is entirely possible).

 

Anyway, any insight someone could provide me to get one step closer to solving this would be great.  If I left out a crucial bit of information, let me know.  Thanks again!

Last Edited: Sat. Mar 24, 2018 - 08:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1.  read the fuses with ISP.   Make sure that JTAGEN is enabled.

2.  connect your JTAGICE-mkII to the correct header SCKT3100A3 on the STK500 JTAG ADAPTER

 

You should be able to connect via JTAG without problem either for programming or Debug session.

 

Now you can address your program "problem".

3.  you have not enabled Free Running Mode.   So the ADC will read only once.

 

• Bit 5 – ADATE: ADC Auto Trigger Enable
When this bit is written to one, Auto Triggering of the ADC is enabled. The ADC will start
a conversion on a positive edge of the selected trigger signal. The trigger source is
selected by setting the ADC Trigger Select bits, ADTS in SFIOR.

 

4.  Seriously.   Please use = when initialising.   Only use |= when appropriate.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

cradleinflames wrote:
my ADC doesn't appear to be acting in the way I expect it to (ie, it's not working correctly). 

Well, that's not necessarily the case.

 

If your expectations aren't (quite) right, then it won't work (quite) as you expected - even thought it is working perfectly correctly!

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Once you get your adc running, keep in mind the leds on the stk500 are inverted (1=dark, 0=lit)...to add an extra layer of confusion factor when debugging.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Start with something simple and try to add more debug code.

What is your ADC connected to? (A potentiometer 0V to 5V is always nice to start with).

If it does not work (as expected) I'ts usually best to get down to the most basic configuration you can think of.

Start with setting the "start conversion" bit inside your for( ;; ) loop, so you have more certainty it is doing at least something.

Then start with a simple delay before you read the ADC register.

If that works, then read a flag for the end of conversion.

If that works, then put your ADC in "continous" mode.

 

A sigle led does not give much feedback.

As a quick and easy intermittant debug step it is a good idea to simply write 8 bits of the adc result to a port (Is there a port with 8 leds on your board?)

 

Ghe general idea is to first make "something" work, then extend slowly in small steps.

If it stops working (as expected) you know you should look for the difference in the last change you made.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the suggestions, everyone!  It does look like it's working correctly now.  I thought I had the free running part already selected but, clearly looked past the ADATE bit (which apparently, lets me select what mode to place it in, including the free run mode). 

 

As far as the JTAG connector, still no luck with this.  I did verify that the JTAG fuse is enabled, so that's not the issue. 

 

david.prentice wrote:

1.  read the fuses with ISP.   Make sure that JTAGEN is enabled.

2.  connect your JTAGICE-mkII to the correct header SCKT3100A3 on the STK500 JTAG ADAPTER

 

You should be able to connect via JTAG without problem either for programming or Debug session.

 

 

Can you expand on this, maybe I'm missing something really obvious.  I currently have the JTAG connector going through the 10 pin ISP header and the 6 pins ISP jumpered to SPROG3 (which goes to the SCKT3100A3).  Is there a separate header besides this that I'm looking past?  I'm still getting the same results as I mentioned in my first post (which makes sense as I haven't really changed anything). 

 

I have my ADC hooked up correctly but I'd still like to get my JTAG up and working so I can debug (my UART is having some issues so I'd like to debug that.  It's getting data, it's just not getting the right data, which I assume is related to choosing the wrong baud rate on my end).  I'd like to troubleshoot it but not having the ability to debug makes things that much more difficult. 

 

awneil wrote:

cradleinflames wrote:
my ADC doesn't appear to be acting in the way I expect it to (ie, it's not working correctly). 

Well, that's not necessarily the case.

 

If your expectations aren't (quite) right, then it won't work (quite) as you expected - even thought it is working perfectly correctly!

 

Heh, this is definitely true.  Didn't mean to imply that because it's not working as I expect it to, it means IT'S acting incorrectly.  Clearly, I was doing something wrong!

 

Looks like everything else is working correctly (which is a welcome change!) so that's a good step forward.  If anyone else has any suggestions regarding what to do with the JTAG to get debugging to work, let me know.  If it helps, I can upload a picture of the board or my settings, if that helps.  Let me know if you need any additional information on this!  Thanks again for your help, everyone!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The JTAG 10-pin does not go on the ISP10 header.
There is a special adapter that fits on the EXPAND0 header as I explained in (2).
.
If you do not have the adapter, you need io make your own or use a squid cable.
I suggest an adapter is better than a squid. Make adapter with plated-through protoboard and male and female header strips.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If your baud rate is wrong, first just set up a loop to spit out a character in an endless cycle to check all of your settings on both ends..will be easier to test.  Once that works 100%, you should be able to likewise send chars to your chip,  no debugger needed.

 

I find some of my cheapo rsr232-usb converters are somewhat flaky, some in only 1 direction.  When I have any issues, I go to these hardware manager settings & lower them...can make a big difference.

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
The JTAG 10-pin does not go on the ISP10 header. There is a special adapter that fits on the EXPAND0 header as I explained in (2).
As they say "a picture is worth 1,000 words". So here are 2,000 words on the subject:

 

 

Those images taken from this page in the user manual:

 

https://www.microchip.com/webdoc...

 

I have one of those expanders I use but from time to time I have been known to use the squid cable that came with the JTAGICE mkII to make the connection:

 

Image result for stk500 jtagicemkii squid cable

(I just googled that image so it might not actually be wired right for JTAG - but it shows what the squid cable looks like!)

Last Edited: Mon. Mar 26, 2018 - 11:34 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry to resurrect a (week) old thread but I got tied up all week and couldn't get back to this before now.  I actually got my UART communicating correctly, which is good news.  It's now sending and receiving the correct data, which is good. 

All the same, I still don't have a way to connect the JTAG programmer as I have an old JTAG Ice MKII but none of the expansion or squid cables that came with it.  I started trying to find the pinout for the expand0 board (so I can figure out how to connect my JTAG device with jumpers) but I'm having trouble finding it. 

 

I ended up trying to match up the wiring on the ATmega16 on the STK500 and trying to determine which pins go where on Expand0 (I kinda cheated and looked at a schematic of an STK501 to verify and make sure it all makes sense).  I got out a multimeter to verify and overall, everything checked out. 

 

The problem I eventually ended up is not knowing the difference between nSRST and nTRST.  Not sure if those should be tied together to nReset on the ATmega16, or if only one should be tied and the other left floating?  Below is the wiring I have thus far:

 

JTAG Pin # JTAG Signal Name Atmega16 Port # Atmega16 Pin # Expand0 Pin #
1 TCK PC2 24 28
2 GND GND 11/31 1/2/19/20/39/40
3 TDO PC4 26 26
4 Vref AREF 32 14
5 TMS PC3 25 27
6 nSRST RESETn 9 15
7 Vsup VCC 10/30 21/22
8 nTRST RESETn 9 15
9 TDI PC5 27 25
10 GND GND 11/31 1/2/19/20/39/40

 

Overall, I think I'm pretty close to having enough information to jumper my JTAG to this (once I verify I have the connection correct, I'll make my own adapter board so I don't ever need to do this again . . . heh) but I'm missing a few bits of information. 

 

Any help you can give me would be greatly appreciated!

 

If you think this belongs in a separate thread, let me know and I can create a new one.  Thanks!