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.
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
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Principles and Applications of Single Chip Microcomputers 3rd Edition (Zhang Yigang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- STC32G Series MCU Technical Reference Manual
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Do you know about incomplete types in C language?
- LM014LC1S02
- Technical characteristics and wide application of embedded systems
- Student dormitory apartment monitoring system based on Internet of Things
- 【DIY】Make a 5V input car light controller
- Linux self-study notes (II) Linux disk partition
- High-voltage amplifier driving piezoelectric tubes
- EEWORLD University Hall ---- Intelligent Information Processing Peking University Tan Ying
- 【Gravity:AS7341 Review】+Received Color Sensor
- Xunwei IMX6Q development board AndroidStudio-calendar test