How to access external RAM in C51

Publisher:zhihuaLatest update time:2015-09-08 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The P0 and P2 ports of C51 are used for external expansion, where XBYTE [0x0002], P2 port corresponds to the high address, and P0 port corresponds to the low address. Generally, P2 port is used for control signals, and P0 port is used as a data channel.

XBYTE defines the external address so that it can communicate with the device connected to your IO port.
In general programs that read and write external RAM, you often see sentences like this:

    XBYTE[address]=data    write data
    data=XBYTE[address]    read data 1
    The external bus consists of 3 groups of buses, data address control, we often call it the external bus, since there are 3 groups of different signals, how do they coordinate work? Generally speaking, the CPU has special external data

The access instruction is like the 51's MOVX instruction you mentioned here (it will be compiled into this instruction in C language). When executing this instruction, the three groups of lines work in coordination.
mov dptr,#1000h
mov a,#55h
movx @dptr,a
The above three call statements can be expressed in C language as follows
#define   W_DATA   XBYTE[0x1000]
W_DATA=0X55;
When using the external bus, the data address and control signal are directly output high and low levels according to the specified timing, so you don't have to worry about it. Of course, you must meet the timing work. How to understand #define XBYTE ((unsigned char

volatile xdata * ))
8051 unique memory type
code     program memory read with MOVC @A+DPTR
data     internal data memory that can be directly accessed
idata     internal data memory accessed with Mov @Rn
bdata     bit addressable internal memory
xdata     external data memory accessed with MOVX @DPTR
pdata     external data memory accessed with MOVX @Rn
Special data type
bit     general bit variable
sbit     absolute addressing bit variable
Syntax
sbit     my_flag     =     location;     (location range from 0x00 ~ 0x7F)
Example
sbit     EA =     0xAF;
or bit variable declared with bdata
char     bdata         my_flags;
sbit     flag0 =       my_flags ^ 0;
(Note that static cannot be added before sbit)
sfr     Special Function Register
Syntax
sfr     my_sfr     =     location;     (location range from 0x80 ~ 0xFF)
Example
sfr     P0     =     0x80;
Specify the absolute address of the variable
In a single module, you can use the following syntax to declare
[memory_space]     type     variable_name     _at_     location
Example
pdata         char     my_pdata     _at_     0x80;
If the variable must be used by multiple modules (Global Variable),
it is more convenient to define it in the header file as an abstract pointer.
#define     variable_name     *((data_type *)         location)
Example
#define     my_pdata     *((char pdata *)     0x80)
(Note the order of char and pdata)
ABSACC.H provides the following convenient macro definitions.
#define CBYTE ((unsigned char volatile code *) 0)
#define DBYTE ((unsigned char volatile data *) 0)
#define PBYTE ((unsigned char volatile pdata *) 0)
#define XBYTE ((unsigned char volatile xdata *) 0)
#define CWORD ((unsigned int volatile code *) 0)
#define DWORD ((unsigned int volatile data *) 0)
#define PWORD ((unsigned int volatile pdata *) 0)
#define XWORD ((unsigned int volatile xdata *) 0)
Hidden initialization program
The first program module executed by 80C51 after power reset (Power On Reset) is not the user's main program
main(), but a program module called startup.a51 hidden in the KEIL-C51 standard link library.
The main task of startup.a51 is to clear the memory blocks including idata, xdata, and pdata to 0, and
initialize the recursive pointer. Then startup.a51 is still executed by a
program module called init.a51 hidden in the KEIL-C51 standard link library. The main task of init.a51 is to initialize
variables with non-zero initial values.
After completing the above initialization program, the control of 80C51 will be handed over to main() to start executing the user's program.
#define XBYTE ((unsigned char volatile xdata *) 0)
defines     XBYTE as a pointer to the unsigned char data type in the xdata address space, and the pointer value is 0
In this way, you can directly use XBYTE[0xnnnn] or *(XBYTE+0xnnnn) to access external RAM.

Among them, the initial address is 0x0000;

 

Baidu Knows:

#define XBYTE((char*)0x20000L)
XBYTE[0x8000]=0x41;
What does this procedure mean?

#define XBYTE ((char*)0x20000L) defines XBYTE as a character pointer (or array) variable pointing to address 0x20000L

XBYTE[0x8000]=0x41; Assign a value of 0x41 to the variable 0x8000 of the array XBYTE. That is, assign a value of 0x41 to the memory unit at address 0x28000L.

This means that you are using a 51 chip with an external data bus. The external memory is most likely RAM, and the capacity of RAM is at least 0x8000 (32k) bytes. The capacity of RAM may be 64k bytes?

If you still have questions, please send a message to contact

Supplement 1:
L stands for long, which means 0x20000L is a long integer. Generally speaking, in 8-bit microcontrollers such as 51, integers are represented by 16 bits of binary and long integers are represented by 32 bits of binary.
 
 
There is also this use:
Write a header file like this

How to access external RAM in C51
Such a format:
Type storage space definition alias keyword _at_ register address
The keyword _at_ is an absolute definition of the address
Keywords:C51 Reference address:How to access external RAM in C51

Previous article:How to create multiple C files and precautions for 51 single chip microcomputer KEIL
Next article:Implementation of I2C bus protocol for 51 single chip microcomputer

Recommended ReadingLatest update time:2024-11-15 07:34

External interrupt C51 program
When there is no interrupt, it is displayed as a running light. External interrupt 0 makes the left and right 4 LEDs flash alternately, and external interrupt 1 makes the LEDs flash. #include reg51.h unsigned char code design ={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x00}; void Delay(unsigned int i){        uns
[Microcontroller]
C51---11 Buzzer
Buzzer Introduction Drive circuit ULN2003 Notes and frequencies Buzzer plays reminder tone
[Microcontroller]
C51---11 Buzzer
c51: 16-bit keyboard program 1
1. Program Function 1. Realize the 16-bit keyboard input function of 0~f, and output the numbers represented by the keys through port P1. 2. Code #include"reg52.h" #define uchar unsigned char   //16-bit keyboard row flip method corresponding key value. //The corresponding key value is 0~f flying code kco
[Microcontroller]
C51 MCU serial communication points
Today, when I was using the C51 microcontroller to send data to the PC, I encountered a small problem: if I use a button to trigger the microcontroller to send 5 data to the PC, when the microcontroller is reset, the first data sent is always only one data, not 5, and it is normal again after the second time. The reaso
[Microcontroller]
C51 Programming 25-Application (MCU and computer realize WiFi communication)
This article implements data communication between the microcontroller and the computer using the ESP-01S wifi module.  Set the baud rate of the WiFi module  Since the default baud rate of the ESP-01S wifi module is 115200, and the baud rate of the 51 microcontroller is usually set at 9600, it is necessary to set
[Microcontroller]
C51 Programming 25-Application (MCU and computer realize WiFi communication)
Keil C51 compilation error summary
1. The first error message ***WARNING L15:  MULTIPLE CALL TO SEGMENT SEGMENT:   ?PR?_WRITE_GMVLX1_REG?D_GMVLX1 CALLER1:   ?PR?VSYNC_INTERRUPT?MAIN CALLER2:   ?C_C51STARTUP ***WARNING L15:  MULTIPLE CALL TO SEGMENT SEGMENT:   ?PR?_SPI_SEND_WORD?D_SPI CALLER1:   ?PR?VSYNC_INTERRUPT?MAIN CALLER2:   ?C_C51STARTU
[Microcontroller]
Program structure of single chip microcomputer C language C51
(1) The library functions defined in C51 are different from those defined in the standard C language. The library functions defined in the standard C language are defined according to general microcomputers, while the library functions in C51 are defined according to the corresponding situations of 51 single-chip micr
[Microcontroller]
Direct software architecture for 8-bit MCU systems (C51 series)
1. Preparation of startup documents $ NOMOD51 ; Ax51 macro assembler control command: disable predefined 8051 ; Customize the storage area that needs to be initialized after power-on ; The starting address of the indirect addressing area IDATA is fixed to 0 IDATALEN EQU 8OH; specifies the length
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号