This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

About constants



Can anybody tell me how constants

are stored in a 8051 application.

If it is prom- able to write it to the

PROM and how it will be accessed during

program execution? thanks.


regards,
san

  • The following constant table:

    const unsigned char code tens [] =
      { 1, 10, 100, 1000, 10000, 100000 };
    

    will be stored in CODE memory. This will be stored in your PROM. When you access the tens array, the code generated will read from CODE memory (the PROM) using the MOVC instruction.

    Keil Support

  • Note that excluding the 'code' keyword will put it in RAM:

    // Goes in RAM, not ROM.
    const unsigned char tens[] = { 1, 10, 100, 1000, 10000, 100000 };
    
    // Goes in ROM
    const unsigned char code tens[] = { 1, 10, 100, 1000, 10000, 100000 };
    
    Const is an ISO C keyword and C knows nothing of ROM or RAM. Thus const does not imply PROM, just that it is read-only from the compiler's perspective.

    - Mark

  • Just to pick a nit however, note that in all of the previous examples, "char" should be changed to "long" to achieve the results implied by the array's initializers.

    --Dan Henry

  • Hehehehehe

    You are right! :-)

  • Hi Mark,

    thank you so much.

    in this case,

    // Goes in RAM, not ROM.
    const unsigned char tens[] = { 1, 10,
    100, 1000, 10000, 100000 };


    where tens will be stored? Whether

    in Heap? or in Stack? or some where

    globally?


    regards,
    san

  • // Goes in RAM, not ROM. 
    const unsigned char tens[] = { 1, 10, 100, 1000, 10000, 100000 };
    Will be stored in the heap, never on the stack because it is outside the scope of a function (I assume). It will exist in DATA if you use the small model and XDATA if you use the large model. Personally, I'd explicitly specify the memory space as CODE or XDATA like this:
    const unsigned char code tens[] = { ... };
    It is global if you do not add the static keyword as in:
    static const unsigned char code tens[] = { ... };
    Static will make it global to the file but not the project. Leaving static off makes it global to the project. Defining 'tens' inside a function makes private to that function. Defining 'tens' inside a { } sequence inside a function makes it visible only inside the { } sequnce. Etc. I suggest you hang out in comp.lang.c as these are all just ISO C questions.

    Regards.

    - Mark

  • Good grief, no wonder he's having problems! I didn't even notice this obvious bug.

    - Mark