Strange variable evaluation problem with mega64 in avrstudio

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

Hi,
I have a strange problem: I can not change any value of my variables! When i define a variable, it would be initialized to a value, and i can not change that. This value is dependent to the type of variable. For example, in the following code:

#include
#include
#include
#include
#include
#include

int
main(void)
{
char a=1;
DDRA = 0xFF;

a=0;
PORTA=0;
PORTA=1;
return 0;
}

the value of variable 'a' is always 213(as watch window says).

I'm using JTAGICE, and mega64L. I should note that when i choose Simulator as platform, there is no problem. But when i start with JTAGICE, and i debug it, the problem appears.
I'm looking for your help.
Tnx.

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

The search term you are looking for is "optimisation".

But just take a step back and look at what happens to 'a' in that program. You set it to 1 (but never use that value) then you set it to 0 (but never use that value). So why would the compiler generate any code to ever make use of 'a' at all? (it's pretty smart at spotting what is completely pointless).

Also, even if 'a' was used (assigned to/from a volatile source) there's a pretty strong chance the compiler would (because it's just local to main) decide to just hold the value in a register and not bother to commit it to SRAM - as such the debugger has no hope of "watching" it anyway because all the "watch window" does is give you a localised view on named SRAM locations.

By the way an embedded program should never return from main(). The code should operate within an infinite loop.