While Statement Not Working Correctly.

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

I'm using STK500 and STK501 with ATMega128L.

I'm getting strange behaviors with my code. I'm removed all but the simplest of code from my app and it still acts strange.

The code enters the BlinkLED routine just fine but then gets stuck there. I set a break point inside the while loop and watch i change from 0, 1, 2, 3, 4 but then the loop executes again and i goes 0, 1, 2, 3, 4 again. I just repeats this over and over and never jumps past the while loop.

It's driving me nuts. Any ideas?

/* ATMEGA128L */
/* GNU GCC */

void BlinkLED(void)
{
	uint8_t i;

	i=0;
	
	while(i<4)
	{
		i = i + 1;
		LED_SET_ON;
		_delay_ms(200);
		LED_SET_OFF;
		_delay_ms(200);
	}
}

Edit: Removed irrelevant code.

Last Edited: Wed. Feb 11, 2009 - 04:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

M103C by any chance?

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

WOW! Thanks clawson. That was it. I unchecked M103C fuse and now it works. I'll have to read up on what that fuse does.

This is my first project with an ATMega128. It would have taken me a very long time to figure this out, if ever. I saw the M103C in the fuses but nothing stuck out that I should dissable it.

Thanks again. You saves a few more hairs from being pulled out of my head! :D

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

As they come from the factory mega128's are really behaving like mega103's for compatibility with a very old product. It didn't have as many h/w features and so it's SFR and RAM layout were different. If you build code for a 128 but put it into what is effectively a 103 then the stack is not in the right place. So as soon as the code makes a CALL when it comes to RET it won't get back the address it came from.

The solutions are:

1) change the M103C fuse so it really becomes a 128

2) change the build settings so the code is built for the (more limited) 103

(1) is preferable - glad it worked out OK for you!

[sometimes I think the 128 datasheet should open with "WARNING: even if you don't read anything else you MUST read this - the chip behaves as a mega103 - if you want to use mega128 then disable the M103C fuse" - it sure would have saved a LOT of questions on this message board - one from almost everyone who starts out with a mega128]

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

I totally agree!

Quote:
[sometimes I think the 128 datasheet should open with "WARNING: even if you don't read anything else you MUST read this - the chip behaves as a mega103 - if you want to use mega128 then disable the M103C fuse" - it sure would have saved a LOT of questions on this message board - one from almost everyone who starts out with a mega128]

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

clawson wrote:
M103C by any chance?

Dammm you are good :-)

I propose that ATMEL changes your user to "Sir Clawson" , when you hit the 20K mark :wink: :wink:

Maybe "Lord Clawson" is better ... Lord of the AVRs.
Then they the publish your posts , it would prob. be more than than 3 volumes , and beat Tolkien.

You could then write "The Hobbit of AVRs" as the beginners intro.

/Bingo

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

Aww shucks! :oops:

(if anyone else wants to look smart, the next time anyone posts ANY fault identified as being on a mega128 just say "M103C" and sit back and bathe in the resulting adulation - 9 times out of 10 you will be right. On the other occasions just say "oh well, worth a try" - works for me)