[D-Flash] How to set initial value

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

[D-Flash] How to set initial value

ソリューションへジャンプ
577件の閲覧回数
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 解決策
364件の閲覧回数
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 返答(返信)
365件の閲覧回数
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 件の賞賛
364件の閲覧回数
Pogo
Contributor III

Hi kef,

Thanks a lot.  

 

 

Pogo

0 件の賞賛