[D-Flash] How to set initial value

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

[D-Flash] How to set initial value

跳至解决方案
1,079 次查看
Pogo
Contributor III

Hi, 

I want to set a const variable with initial value in D-Flash segment and use this variable in some funtion. But if I set this variable with initial value and use it in other function. When I change this variable using writing D-Flash with user's input, this variable will not change. I checked the complier code, all code which using this variable will replace by the initial value. The following example code will express my issue.

#pragma DATA_SEG __EPAGE MYDFlashconst unsinged int uVariable = 10;#pragma DATA_SEG DEFAULTvoid main(void){   if( uVariable == 10 )  //<-- this line complier will not use uVariable, it will use 10      printf("OK");   WriterToDFlash();   if( uVariable == 5 )  // same above      printf("ok");   else      printf("write to D-flash failed");}void WriteToDFlash(){   //Write 5 to uVarialbe's address}

The run result will be

OK

write to D-flash failed.

My expection is OK and OK.

My question is how can I tell to complier don't using const value to replace variable and just use the value in varible's address. Please help me! Thanks a lot.

 

Pogo

标签 (1)
0 项奖励
回复
1 解答
866 次查看
kef
Specialist I

If I understand your request

 

1) use volatile keyword, like volatile const int var;

2) Go to compiler options and check "Disable CONST variable by constant replacement" optimization.

3) move variable definition to different source file. When compiling different source files, where variable is just declared, compiler won't see variable initializer and won't replace variable access with constant.

在原帖中查看解决方案

0 项奖励
回复
2 回复数
867 次查看
kef
Specialist I

If I understand your request

 

1) use volatile keyword, like volatile const int var;

2) Go to compiler options and check "Disable CONST variable by constant replacement" optimization.

3) move variable definition to different source file. When compiling different source files, where variable is just declared, compiler won't see variable initializer and won't replace variable access with constant.

0 项奖励
回复
866 次查看
Pogo
Contributor III

Hi kef,

Thanks a lot.  

 

 

Pogo

0 项奖励
回复