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
Solved! Go to Solution.
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.
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.
Hi kef,
Thanks a lot.
Pogo