RAM variables re-declared at power ON

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

RAM variables re-declared at power ON

1,331 次查看
LordMark
Contributor IV

Hi. I need to keep the value of some RAM variables after a power loss. I obviously have to use a back-up battery. But I have a doubt. Please consider the following example code:

 

int a; <-- global variable

 

main()

{

... 

a = 10;

...

 

Suppose that power goes down and a = 10. What happens after the new power-on with the declaratione int a? Is the variable "a" redeclared? Would its value be lost?

I'm using the MCF52259DEMOKIT; VSTBY pin is connected to 3.3V but after a reset my variable goes to "0" after it is redeclared. 

标签 (1)
标记 (1)
0 项奖励
回复
1 回复

684 次查看
kef
Specialist I

You meant reinitialized, not redeclared, right?

 

C compiler has to initialize all static variables at startup. Static variables without initializer are initialized to zero. All variables in .bss sections are reset to zero at startup. All variables in .data (variables with initializers) are initialized with data from ROM .idata section or something like that. I'm not sure about all sections, but you could move some variables to non default section and startup routine will be unable to initialize those variables.

 

In CW for MCUs V6.3 this seems to be working: 

 

1) Create some custom memory section. Let us call it .noinit

 

Add to lcf file, somewhere between .bss {} block and .custom {} block something like this:

 

  .noinit :
  {
      *(.noinit)
  } >> userram

 

2) In your "C" file 

 

   2.1) make compiler see .noinit section:

 

#pragma define_section noinit ".noinit" far_absolute

   2.2) move varible to .noinit section:

 

__declspec(noinit) int a;

0 项奖励
回复