Unable to change variable values in cortex M4 MCU

Status
Not open for further replies.

Matt_Cuino

Newbie level 5
Joined
Mar 13, 2017
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
94
Hello, I'm having troubles with the programming of a cortex-m4 MCU , namely, I can't change the value of some of the variables.

I'm using keil uv5 to do the developing. Here is a more detailed description of the problem:

For example,

Code:
uint16_t var1 = 0xffff;
...

var1 = 0x1111;

Normally, after the last line, the value of var1 should be 0x1111, but it's not, instead, it's 0x0000.

The value of var1 before 0x1111 is 0xffff, indicating that it was properly initialized, assuming keil isn't faulty.

I tried to declare it static, it's the same regardless.

What is likely the explanation for this?
 

Better start doing some basic things with something less complex. Static variables will be initiated outside the heap. The initial value will be applicable only once. So, the correct sequence for static variable will be like that:
static uint16_t var1;
var1 = 0xffff;
...
var1 = 0x1111;
If it doesn't work, that means you do something wrong because it just will work do you wan't that or not)))
 

The two code lines can be expected to work as written. But how do you check if the assignment is taking place?

Your post is far from a "detailed description".

Consider that instructions may be optimized away if they have no effect. E.g. if var1 isn't read after the second assignment, the assignment is removed by the compiler with respective optimization level in effect.
 

@Easyrider83 - it does not matter. You just add some unnecessary code for the initial assignment (I assume the global scope of the variable). In your example the variable will be placed into the .bss (and zeroed of course) segment and then assigned somewhere in the code. If you you initialize it in the declaration it will be placed in the .data segment and initialized by the startup code (usually by simply copy from the FLASH memory).
Adding static does not change anything, and if the variable was not optimized out it has to be changed by the assignment. Show us more code.
 

If you initialize it in the declaration it will be placed in the .data segment and initialized by the startup code.
It depends. I read the code as local variable declaration inside a block. In this case, the initialization is just an assignment repeated each time the block is executed. The static attribute changes the behavior to single initialization during startup.

The difference however doesn't matter regarding the (up to now unclear) problem reported by Matt_Cuino.
 

Did you read my post? I assumed the global scope of the variable.
 

I assumed the global scope of the variable.
I know you did. But var1 is most likely a local variable in the OP's code.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…