8051 MCU Data Description

Publisher:画意人生Latest update time:2021-04-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

One of the keys to learning C51 is to deeply understand and apply the extensions of C51 to standard ANSI C, because most of the extensions are directly targeted at the 8051 series CPU hardware.


The specific instructions are as follows (8031 is the default CPU).


1. Keil C51 extended keywords


C51 V4.0 version has the following extended keywords (19 in total):


_at_idata sfr16 alien interrupt small


bdata large _task_ Code bit pdata


using reentrant xdata to compact sbit data sfr


2. Memory Areas:


1. Pragram Area:


The code indicates that there can be up to 64kBytes of program memory


2. Internal Data Memory:


The internal data memory can be described using the following keywords:


data: Direct addressing area, the lower 128 bytes of the internal RAM 00H~7FH


idata: indirect addressing area, including the entire internal RAM area 00H ~ FFH


bdata: bit addressable area, 20H~2FH


3. External Data Memory


External RAM can be identified by the following keywords depending on its usage:


xdata: can specify up to 64KB of external direct addressing area, address range 0000H ~ 0FFFFH


pdata: Can access 1 page (25bBytes) of external RAM, mainly used in compact model.


4. Special Function Register Memory


8051 provides 128Bytes of SFR addressing area, which can be bit-addressed, byte-addressed or word-addressed to control timers, counters, serial ports, I/O and other components. It can be described by the following keywords:


sfr: byte addressing, for example, sfr P0=0x80; the address of the PO port is 80H, and the constant after “=” is between H and FFH.


sfr16: word addressing, such as sfr16 T2=0xcc; specify the Timer2 port address T2L=0xcc T2H=0xCD


sbit: bit addressing, such as sbit EA="0xAF"; specifies the 0xAF bit as EA, that is, interrupt enable


You can also define methods as follows:


sbit 0V=PSW^2; (0V is defined as the second bit of PSW)


sbit 0V=0XDO^2; (same as above)


Or bit 0V-=0xD2 (same as above).


3. Storage Mode


The storage mode determines the default storage area for variables, function parameters, etc. that do not explicitly specify a storage type. There are three types:


1. Small mode


All default variable parameters are loaded into the internal RAM. The advantage is fast access speed, but the disadvantage is limited space, which is only suitable for small programs.


2. Compact mode


All default variables are located in one page (256Bytes) of the external RAM area. The specific page can be specified by the P2 port and described in the STARTUP.A51 file. It can also be specified by pdata. The advantage is that the space is more spacious than Small and the speed is slower than Small, but faster than large. It is an intermediate state.


3. Large mode


All default variables can be placed in an external RAM area of ​​up to 64KB. The advantage is that the space is large and many variables can be stored. The disadvantage is that the speed is slow.


Tip: The storage mode is selected in the C51 compiler options.


4. Storage Type Declaration


The storage type of a variable or parameter can be specified by the default type specified by the storage mode, or it can be directly specified by a keyword declaration. Each type is described by: code, data, idata, xdata, pdata, for example:


data uar1


char code array[ ] = "hello!";


unsigned char xdata arr[10][4][4];


5. Variable or data type


C51 provides the following extended data types:


bit variable value is 0 or 1


sbit A bit variable defined from a byte, 0 or 1


sfr sfr byte address 0~255


sfr16 sfr word address 0~65535


Other data types such as char, enum, short, int, long, float, etc. are the same as ANSI C.


6. Bit variables and declarations


1. Bit type variable


Bit type variables can be used as variable types, function declarations, function return values, etc., and are stored in internal RAM 20H~2FH.


Notice:


(1) Functions declared with #pragma disable and functions specified with "usign" cannot return bit values.


(2) A bit variable cannot be declared as a pointer, such as bit *ptr; this is incorrect.


(3) There cannot be a bit array such as: bit arr[5]; error.


2. Description of bit addressable area 20H-2FH


It can be defined as follows:


int bdata i;


char bdata arr[3],


Then:


sbit bito=in0; sbit bit15=I^15;


sbit arr07 = arr[0]^7; sbit arr15 = arr[i]^7;


7. Keil C51 pointer


C51 supports Generic Pointer and Memory_Specific Pointer.


1. General pointers


The declaration and use of general pointers are the same as standard C, but the storage type of the pointer can also be specified, for example:


long * state; is a pointer to a long integer, and the state itself is stored according to the storage mode.


char * xdata ptr; ptr is a pointer to char data, and ptr itself is placed in the external RAM area. The data pointed to by the above long, char and other pointers can be stored in any memory.


Generally, the pointer itself is stored in 3 bytes, which are the memory type, high offset, and low offset.


2. Memory pointer


The storage type is specified when the pointer is declared based on the memory, for example:


char data * str; str points to the char data in the data area


int xdata * pow; pow points to an int integer in external RAM.


When storing this type of pointer, only one byte or two bytes are needed because only the offset needs to be stored.


3. Pointer conversion


That is, the pointer is converted between the above two types:


When a memory-based pointer is passed as an argument to a function that expects a general pointer, the pointer is automatically converted.


If you do not declare the prototype of an external function, the memory-based pointer will be automatically converted to a general pointer, causing an error. Therefore, please declare all function prototypes using "#include".


l The pointer type can be forcibly changed.


8. Keil C51 Function


C51 function declarations extend ANSI C, including:


1. Interrupt function declaration:


The interrupt declaration method is as follows:


void serial_ISR () interrupt 4 [using 1]


{


/* ISR */


}


To improve the fault tolerance of the code, an iret statement is generated at the unused interrupt entry to define the unused interrupt.


/* define not used interrupt, so generate "IRET" in their entrance */


void extern0_ISR() interrupt 0{} /* not used */


void timer0_ISR () interrupt 1{} /* not used */


void extern1_ISR() interrupt 2{} /* not used */


void timer1_ISR () interrupt 3{} /* not used */


void serial_ISR () interrupt 4{} /* not used */


2. Universal storage workspace


3. Select the general storage work area declared by using x, see the example above.


4. Specify storage mode


Described by small, compact and large, for example:


void fun1(void) small { }


Tip: All internal variables of the function described by small use internal RAM. The key and frequently time-consuming parts can be declared in this way to improve the running speed.


5. #pragma disable


Declare before a function and it is valid for only one function. The function cannot be interrupted during its call.


6. Recursive or reentrant function specification


Functions that can be called in both the main program and the interrupt are prone to problems. This is because 51 is different from PC. PC uses the stack to pass parameters, and all internal variables except static variables are in the stack; while 51 generally uses registers to pass parameters, and internal variables are generally in RAM. When the function is re-entered, the data of the last call will be destroyed. The following two methods can be used to solve the problem of function re-entry:


a. Use the aforementioned "#pragma disable" statement before the corresponding function, that is, only the main program or interrupt is allowed to call the function;


b. Describe the function as reentrant. As follows:


void func(param...) reentrant;


After compilation, KeilC51 will generate a reentrant variable stack, and then you can simulate the method of passing variables through the stack.


Since reentrant functions are generally called by the main program and interrupts, interrupts usually use a different R register set from the main program.


In addition, for reentrant functions, add the switch "#pragma noaregs" in front of the corresponding function to prohibit the compiler from using absolute register addressing, so that code that does not depend on the register group can be generated.


7. Specifying PL/M-51 Functions


Specified by alien.


Reference address:8051 MCU Data Description

Previous article:Circuit module design of intelligent transformer temperature monitoring instrument
Next article:Application of C8051F020 in LED display control system

Recommended ReadingLatest update time:2024-11-23 06:06

Software IAP Technology of C8051F MCU
Introduction The C8051Fxxx series of high-speed SoC microcontrollers are fully integrated mixed-signal system-level chips developed by Cygnal Corporation of the United States, with a microprocessor core compatible with the 8051. The C8051Fxxx microcontrollers have a large capacity Flash memory for storing program co
[Microcontroller]
Software IAP Technology of C8051F MCU
Single chip microcomputer C8051F020 and its application in instruments and meters
1 Introduction At present, with the continuous improvement of science and technology and industrial and agricultural production levels, higher and higher requirements are placed on corresponding instruments and meters. Therefore, instruments and meters need to expand a large number of peripheral functional comp
[Industrial Control]
Single chip microcomputer C8051F020 and its application in instruments and meters
Port configuration issues when C8051F340 is connected to FM24C04
I originally wanted to use the SMBus port of the microcontroller, but the official example used several timers, and my small project also needs a lot of timers. The timer resources are not enough. So I just use the IO port to simulate the I2C timing to operate the FM24C04. The project is not strict on time. carve. 
[Microcontroller]
Port configuration issues when C8051F340 is connected to FM24C04
Serial interface structure and working mode setting register of 8051 single chip microcomputer
Serial interface structure of 8051 microcontroller  The serial interface of 8051 microcontroller is a programmable full-duplex serial communication interface. It can be used as an asynchronous communication mode (UART) to connect to external devices that transmit information serially, or for full-duplex 8051 multi-ma
[Microcontroller]
Serial interface structure and working mode setting register of 8051 single chip microcomputer
A clock based on 8051 that can start and stop/display or hide time/set current time
Functions implemented by the program: 1. After the experimental box is powered on, the current time is not displayed. 2. After sending "Show.E" to 8051, the current time is displayed. 3. After sending "Hide.E" to 8051, hide the current time. 4. After sending "Start.E" to 8051, the clock starts timing. 5. Aft
[Microcontroller]
A clock based on 8051 that can start and stop/display or hide time/set current time
Single chip microcomputer design: simple bicycle odometer
This is a simple bicycle odometer, the core part of which is an 8051 microcontroller. Circuit Diagram Program Flow PCB thermal transfer image, can be used directly
[Microcontroller]
Single chip microcomputer design: simple bicycle odometer
CCD photoelectric telemetry vertical line instrument based on C8051F340 drive and acquisition
0 Introduction     The vertical line is a simple and effective means to observe the horizontal displacement and deflection of the dam. With the advancement of technology, the telemetering vertical line coordinate instrument has developed from contact type to non-contact type.     Charge Coupled Device (CCD) is a sens
[Microcontroller]
CCD photoelectric telemetry vertical line instrument based on C8051F340 drive and acquisition
How to design the digital sensor system hardware of C8051F060 microcontroller
With the development of science and technology, intelligent control technology has begun to be widely used in the field of electronic testing. In modern industrial measurement and control systems, people often connect various sensors to the field bus to form a sensor network system. Various sensor devices serve as one
[Microcontroller]
How to design the digital sensor system hardware of C8051F060 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号