ARM Technical Support Knowledge Articles

USING MEMORY-MAPPED DEVICES

Applies to: C51 C Compiler

Answer


Information in this article applies to:


QUESTION

How can I access memory-mapped devices in C using the Keil 8051 C compiler? I want to access a memory-mapped UART using the /RD and /WR pins of the 8051.

ANSWER

External hardware you connect to the 8051 using the address lines and the /RD and /WR pins appears in the XDATA memory space. This is a 64K memory space that may be RAM, external memory mapped devices, or any combination thereof.

There are several ways you can access memory-mapped devices. Since they are mapped into XDATA, they can be accessed using...

Each of these methods is described below.

Using an XDATA Pointer

If your memory-mapped device is located at 0x8000, you can access it using the following C pointer:

void main (void)
{
volatile unsigned char xdata *p = (char xdata *) 0x8000;

p[0] = 0xAA;
p[1] = 0x55;
}

This code writes the value 0AAh to address 8000h and the value 55h to 8001h. The following assembler code is generated for this c program.

;---- Variable 'p' assigned to Register 'R6/R7' ----
0000 7F00          MOV     R7,#00H
0002 7E80          MOV     R6,#080H
                                           ; SOURCE LINE # 5
0004 8F82          MOV     DPL,R7
0006 8E83          MOV     DPH,R6
0008 74AA          MOV     A,#0AAH
000A F0            MOVX    @DPTR,A
                                           ; SOURCE LINE # 6
000B A3            INC     DPTR
000C 7455          MOV     A,#055H
000E F0            MOVX    @DPTR,A

Using the XBYTE Macro

If your memory-mapped device is located at 0x8000, you can access it using the following C library routine:

#include <absacc.h>

void main (void)
{
XBYTE[0x8000] = 0xAA;
XBYTE[0x8001] = 0x55;
}

This code writes the value 0AAh to address 8000h and the value 55h to 8001h. The following assembler code is generated for this c program.

0000 908000        MOV     DPTR,#08000H
0003 74AA          MOV     A,#0AAH
0005 F0            MOVX    @DPTR,A
                                           ; SOURCE LINE # 7
0006 A3            INC     DPTR
0007 7455          MOV     A,#055H
0009 F0            MOVX    @DPTR,A

Using an Absolutely Located Variable

If your memory-mapped device is located at 0x8000, you can access it using the following variable declaration(s):

volatile unsigned char xdata mm_dev_reg0 _at_ 0x8000;
volatile unsigned char xdata mm_dev_reg1 _at_ 0x8001;

void main (void)
{
mm_dev_reg0 = 0xAA;
mm_dev_reg1 = 0x55;
}

This code writes the value 0AAh to address 8000h and the value 55h to 8001h. The following assembler code is generated for this c program.

0000 908000        MOV     DPTR,#mm_dev_reg0
0003 74AA          MOV     A,#0AAH
0005 F0            MOVX    @DPTR,A
                                           ; SOURCE LINE # 7
0006 A3            INC     DPTR
0007 7455          MOV     A,#055H
0009 F0            MOVX    @DPTR,A

MORE INFORMATION

SEE ALSO

Article last edited on: 2005-07-15 14:18:41

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential