Keil Logo

C51: USING MEMORY-MAPPED DEVICES


Information in this article applies to:

  • C51 Version 5 and Later

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...

  • An XDATA pointer that is initialized to the address of the device,
  • The abbsolute memory access macros XBYTE and XWORD,
  • A variable that is defined to reside at the address of the device.

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

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Friday, July 15, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.