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?

Parents
  • 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;
    

Reply
  • 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;
    

Children