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

Addressing External Memory

Hello,

BASE_ADDR = 0x8000
i=1

A)may I know what is
#define COMMAND(i) (*((volatile char xdata *)(BASE_ADDR +i)))

B)Is is better to address the "command register" of an external chip as XDATA of 8051 chip(using ALE,Port0,Port2,etc) or should I just manually arrange some I/O pins (of 8051) to do that?

Regards,
KC

Parents
  • Hello,

    Thank You Erik!

    I really need help on this
    ---------------------------------------------------------------
    BASE_ADDR = 0x8000
    i=1

    #define COMMAND(i) (*((volatile char xdata *)(BASE_ADDR +i)))
    ---------------------------------------------------------------

    this code seems to use casting to define COMMAND(i) to point to a specific xdata location

    1) Is "COMMAND(i)" a "memory specific pointer"?
    2) Does it take up any memory space (eg 2bytes as you would expect of a memory specific pointer)?
    3) Is there a better way (or clearer code) to do this?
    4) Is the "volatile" really necessary? (or should i do some settings to prevent optimization from affecting code)

    Regards,
    KC

Reply
  • Hello,

    Thank You Erik!

    I really need help on this
    ---------------------------------------------------------------
    BASE_ADDR = 0x8000
    i=1

    #define COMMAND(i) (*((volatile char xdata *)(BASE_ADDR +i)))
    ---------------------------------------------------------------

    this code seems to use casting to define COMMAND(i) to point to a specific xdata location

    1) Is "COMMAND(i)" a "memory specific pointer"?
    2) Does it take up any memory space (eg 2bytes as you would expect of a memory specific pointer)?
    3) Is there a better way (or clearer code) to do this?
    4) Is the "volatile" really necessary? (or should i do some settings to prevent optimization from affecting code)

    Regards,
    KC

Children
  • 1) Is "COMMAND(i)" a "memory specific pointer"?
    yes
    2) Does it take up any memory space (eg 2bytes as you would expect of a memory specific pointer)?
    look in the generated assembly
    3) Is there a better way (or clearer code) to do this?
    many ways, I would name the memory mapped I/Os individually and use
    #define SOME_COMMAND_ADDR (BASE_ADDR +nnn)
    xdata volatile SOME_COMMAND _at_ SOME_COMMAND_ADDR
    or something like that
    4) Is the "volatile" really necessary? (or should i do some settings to prevent optimization from affecting code)
    Volatile works for this, why reinvent the wheel.

    Erik

  • 1) Is "COMMAND(i)" a "memory specific pointer"?

    No.
    A Pointer is a variable - you are defining an expression

    2) Does it take up any memory space (eg 2bytes as you would expect of a memory specific pointer)?

    No (apart from the code space necessary to evaluate the expression).
    COMMAND is a preprocessor definition - the macro is expanded by the preprocessor before the compiler ever sees it!

    3) Is there a better way (or clearer code) to do this?

    How about _at_, as Erik suggested?
    Or Keil's predefined XBYTE?

    4) Is the "volatile" really necessary?

    It's your system - only you can tell us that!

    ...should i do some settings to prevent optimization...

    No.
    This is precisely the situation for which volatile is provided - use it!