We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi Guys
How am i able to assign values to a variable i have stored in flash so that it is included in the hex file?
For example: i have declared a variable volatile unsigned char code err_msgs[10][20] _at_ FLASHSTART;
but cannot assign an initial value to it and have to write it's value to flash when my program starts.
Xarion
Why are you using _at_?
If you don't use _at_, then you can assign initial values!
If you must use _at_, then read the Knowledgebase article linked at the end of the Manual page: http://www.keil.com/support/man/docs/c51/c51_le_absvarloc.htm
The _AT_ was to define where the flash memory resides, its not a ram variable, is there another way to declare variables in flash memory?
ah i just noticed now is that the "code" keyword defines that it is in flash mem. right?
Sort of!
The code keyword specifies that it is to be located in CODE space; if your flash is in your CODE space, then it will end up in your flash!
There is no need for _at_ simply to locate the strings into CODE space.
You only use _at_ when you need to define a specific address
Ah yes that makes more sense and it works :) thanx!
now have my array ...
volatile unsigned char code err_msgs[10][20];
how can i assign my 10 strings to this array? pragma?
Why on earth would you define that as volatile ??!!
not sure why volatile, was just following www.nxp.com/.../AN10342_1.pdf
not exactly sure what volatile means
"how can i assign my 10 strings to this array?"
The code keyword has no effect on this - you do it the same way you would for any standard ANSI 'C' array with any standard ANSI 'C' compiler. Therefore, see any 'C' textbook.
"not exactly sure what volatile means"
volatile is a standard ANSI 'C' keyword - it has no special meaning in Keil C51. Therefore, again, see any 'C' textbook.
See also: http://www.keil.com/forum/docs/thread11735.asp
yeah i can just strcpy into the array at runtime but thats going to use more code to write each character to flash as to write to flash its not just assigning to a variable, i'm running tight with space which is my real aim in doing this.
"The 'volatile' will indicate that the compiler will not optimize out the array if it isn't initialized..." www.nxp.com/.../AN10342_1.pdf
No, I think that's wrong!
volatile does not affect the allocation of variables; it just prevents optimisation of the access to variables!
"yeah i can just strcpy into the array at runtime"
No, you can not!
You not only need a 'C' textbook, but also an 8051 textbook!
As far as 'C' is concerned, you cannot write to a const object;
As far as the 8051 is concerned, you cannot write to CODE space.
You need to initialise the array in its definition - in the same way that you can initialise any other 'C' array in its definition...
yes sorry, had some brain failure there i cant just strcpy ok i'll take a look at some books
was just following www.nxp.com/.../AN10342_1.pdf
Why are you following that App Note?
That is about storing and updating data in flash using IAP functions - you don't need any of that just to have a set of fixed message strings in flash!
Perhaps if you described what you're actually trying to achieve...
i think i found what i needed to do, just needed to put = {"MSG1\0","MSG2\0","MSG3\0"};
thanx for you help
Don't you believe in the compiler being capable enough to zero-terminate your strings for you?
The only time the compiler will not zero-terminate a string, is if you assign to a fixed-size array, and the string has as many characters as the array - but then, no "\0" will help you since there are no room for that character...