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

How can I save Constants

Hallo everybody,

can anybody help me. I want to save Constants on the XRAM. How does this works?

  • Refer STRING directive in C51 user manual.

  • I want to save Constants on the XRAM.

    What exactly do you want to accomplish ?

    Putting constants in XRAM does not make much sense except in fairly exotic circumstances (for example: All code memory is used and you want to store a table in XDATA that is generated by an algorithm at bootup).

    XDATA, on most devices, is volatile and will lose its contents during a power cycle.

    That aside, there is nothing that stops you from declaring a constant in XDATA

    const char xdata myconst = 0xA5;
    

    However, this is a way to waste both CODE and XDATA memory since the value of myconst must be stored in CODE memory and copied to the location in XRAM at bootup (which requires several assembly instructions, i.e. more CODE memory).

    The usual place to store constants is CODE memory. Here, they don't need to be initialized (since CODE memory is usually nonvolatile) and are difficult to modify accidentially. The following code snipped declares a constant in code memory. The const specifier is actually unnecessary and only used for clarification, since code memory is considered intrinsically constant on the '51 architecture, as there is no assembly instruction to move data to code memory.

    const char code myconst = 0xA5;
    

  • the directive XCROM can also helps you

  • How can I know, at with Adress I can storage my constant?

  • How can I know, at with Adress I can storage my constant?

    In most cases, the linker does this for you and you do not need to worry about it.

    Where the linker put what can be found in the linker listing file (*.m51).

    If you need to place the constant at a particular address, refer to the linker documentation on how to do it.

  • U by ur self also can specify it by using the absolute address refer to keil user guide volatile and _at_ will help u

  • "Putting constants in XRAM does not make much sense except in fairly exotic circumstances (for example: All code memory is used and you want to store a table in XDATA that is generated by an algorithm at bootup)."

    Another example would be where you have non-volatile memory in XDATA space.

    If part of your ROM is mapped into XDATA space, that's where the XCROM directive comes in...

    It'd help if you said what you're actually trying to achieve - then people could make informed suggestions!