One of the keys to learning C51 is to deeply understand and apply C51's extensions to standard ANSI C. This is because most of the extensions are directly targeted at the 8051 series CPU hardware. There are roughly 8 categories:
8051 memory types and memory areas
Storage Mode
Memory Type Declaration
Variable type declaration
Bit variables and bit addressing
Special Function Registers (SFRs)
C51 pointer
Function Properties
The specific instructions are as follows
Section 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 compact sbit data sfr
Section 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,
Counters, serial ports, I/O and other components 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; specify 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).
Section 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 port P2 in STARTUP.A51
The document states that it can also be specified using pdata. The advantage is that it has more space than Small, but is slower than Small and faster than Large.
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.
Section 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 declared by a keyword.
Use: code, data, idata, xdata, pdata to explain, for example:
data uar1
char code array[ ]="hello!";
unsigned char xdata arr[10][4][4];
Section 5 Variables or Data Types
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.
Section 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;
Section 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 pointers such as long and char 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 memory-based pointer is declared, 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 specify the external function prototype, the memory-based pointer will automatically be converted to a general pointer, causing an error.
Use "#include" to declare all function prototypes.
z can forcibly change the pointer type.
Section 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 */
10
2. Universal storage workspace
3. Select the general storage work area declared by using x, see the example above.
4. Specify the 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 as follows
To increase 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.
All internal variables except static variables are in the stack; 51 generally uses registers to pass parameters, and internal variables are generally in RAM.
When a function is reentered, the data of the last call will be destroyed. There are two ways to solve the problem of function reentry:
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.
number;
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" before the corresponding function to prohibit the compiler from using absolute
By addressing registers, code can be generated that is not dependent on register banks.
Previous article:Compile error message in C51
Next article:The real number storage format defined by float in C51
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- 【Aigtek Instrument Peripherals】Do you know how to use a dotting machine?
- Microwave Solid-State Circuit Design (Second Edition)
- Let's take a look at the electromagnetic waves in Korean
- How terrible is it that an electric motorcycle battery explodes?
- Is there any official routine for developing EFM8 in Simplicity Studio? Where can I download it?
- 【Silicon Labs BG22-EK4108A Bluetooth Development Evaluation】+Power-on and Example Program Test
- "Power amplifier experimental case" application of power amplifier in forward and reverse motion of ultrasonic motor
- Share: [Zhongke Blue News] AB32VG1 Review SDIO (File System)
- 【Chuanglong Technology Allwinner A40i Development Board】Performance Comprehensive Test
- The idea of single chip microcomputer time-sharing control