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.
const char anarray[5]={1,3,5,7,9}; The above works well. But occupies a lot of mem. const char code anarray[5]={1,3,5,7,9}; Compliled and linked without an error prompt. But works bad.
What do you mean, "works bad?" There are many clever people on this forum, but (mostly) we're not psychic! How about:
code const char anarray[5]={1,3,5,7,9};
actually I did use unsigned. Is the speed accessing const char[] in data seg the same as in code seg
You still haven't explained "works bad" "Is the speed accessing const char[] in data seg the same as in code seg" The 'Code' space is external to the CPU "Core" and will therefore always be slower than 'Data' (as distinct from XDATA)
Works bad: seems only access the first element of the array.
unnecessary -------v const char anarray[5]={1,3,5,7,9}; ^--- Tells compiler that anarray[] is read-only and places it in the default memory space, for small this is in the fast but preciously small DATA memory. const char code anarray[5]={1,3,5,7,9}; ^--- Tells Keil C51 to apply a non-standard C extension and place this object in CODE space. Code reads usually go throught the DPTR which is slower than DATA access but also more appropriate usually. Again, you don't need to dimension an array if you tell it what will be in the array as is required for const objects. My suggestion? const unsigned char idata arr[] = { 1, 2, 3, 4, 5 }; for speed and const unsigned char code arr[] = { 1, 2, 3, 4, 5 }; for storage efficiency and protection from errant pointer overwriting.
Thanks Mark! placing a storage class on all vars, yes all. Better control and fewer surprises when changing models. I do not understand what is storage class and what is changing models. could you please give some example?
Storage Class is a Keil 'C' language extension: specifies where an object is to be located; viz, code, data, idata, pdata, or xdata. You can specify a Storage Class independently for each identifier in your program. Model (or, more precisely, "Memory Model") is a Keil Compiler option. It defines a default Storage Class (and a few other things) for your program; ie, if you don't explicitly state the Storage Class in a definition, the compiler will automatically use the default specified by the Memory Model, viz: Small - data; Medium - pdata; Large - xdata. It's all in the C51 User' Guide (C51.pdf), accessible from the 'Books' tab in the uVision Project window.
By storage control I meant telling the compiler which of the many 8051 memory spaces. E.g. char idata var1; char data var2; char xdata var3; char pdata var4; char code var5; I've never actually used pdata (paged data where Port 2 acts as a page register of 256 bytes). I don't remember if there are other storage spaces. As for models, you had better read up on those. The default memory model for C51 is SMALL, which is all I have ever used (well, once I used LARGE) despite my application size. SMALL places all variables in data/idata by default so I tell C51 to place loop counters in data, arrays and structs in idata or xdata, and const vars. in code. Small also has some other efficiencies that benefit code running on the 8051. Read up the differences between the models. - Mark