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 (8031 is the default CPU).
Section 1 Keil C51 Extended Keywords
C51 V4.0 version has the following extended keywords (a total of 19):
_at_ idata sfr16 alien interrupt small
bdata large _task_ Code bit pdata
using reentrant xdata compact sbit data sfr
Section 2 Memory Areas:
1. Program 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 by the following keywords:
data: directly addressable area, which is the lower 128 bytes of the internal RAM 00H~7FH
idata: indirectly addressable area, including the entire internal RAM area 00H~FFH
bdata: bit addressable area, 20H~2FH
3. External Data Memory
The external RAM can be identified by the following keywords depending on 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. Speciac Function Register Memory
8051 provides 128Bytes of SFR addressing area, which can be bit-addressable, byte-addressable or word-addressable to control timers, counters, serial ports, I/O and other components. It can be described by the following keywords:
sfr: byte addressing, such as sfr P0=0x80; the PO port address is 80H, and the constant between H and FFH after "=".
sfr16: word addressing, such as sfr16 T2=0xcc; specifies 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
There may also be the following definition methods:
sbit 0V=PSW^2; (defining 0V as the second bit of PSW)
sbit 0V=0XDO^2; (same as above)
or bit 0V-=0xD2 (same as above).
Section 3 Storage Modes
The storage mode determines the default storage area for variables, function parameters, etc. that do not have an explicitly specified storage type. There are three types:
-
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. -
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, described in the STARTUP.A51 file, or specified by pdata. The advantage is that the space is more spacious than Small, the speed is slower than Small, and faster than Large. It is an intermediate state. -
Large mode
All default variables can be placed in up to 64KB of external RAM. 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 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];
Section 5 Variables or Data Types
C51 provides the following extended data types:
bit: bit variable value is 0 or 1
sbit: bit variable defined from byte 0 or 1
sfr: byte address 0 to 255
sfr16: word address 0 to 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 Variables
Bit variables can be used as variable types, function declarations, function return values, etc., and are stored in internal RAM 20H to 2FH.
Note:
(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; it is wrong
(3) There cannot be a bit array, such as: bit arr[5]; it is wrong.
2. The bit addressable area 20H-2FH
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 those in 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 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.
The general pointer itself is stored in 3 bytes, which are the memory type, high offset, and low offset.
2. Memory pointer
When describing a memory-based pointer, the storage type is specified, for example:
char data * str; str points to the char data in the data area
int xdata * pow; pow points to the int integer in the 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".
- The pointer type can be forcibly changed.
Section 8 Keil C51 Function
C51 function declaration extends 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. General storage work area
3. Select the general storage work area and declare it by using x, as shown in the above example.
4. Specify the storage mode
Describe it 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 places can be declared in this way to improve the running speed.
5. #pragma dISAble
is declared before the function and is only valid for one function. The function cannot be interrupted during the call.
6. Recursive or reentrant function specification
Functions that can be called in both the main program and the interrupt are prone to problems. 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 reentered, the data of the last call will be destroyed. You can use the following two methods to solve this problem: a
. Use the aforementioned "#pragma dISAble" declaration before the corresponding function, that is, only one of the main program or interrupt is allowed to call the function; b. Describe the function as reentrant. As follows:
void func(param...) reentrant;
After KeilC51 is compiled, a reentrant variable stack will be generated, and then the method of passing variables through the stack can be simulated.
Since reentrant functions are generally called by the main program and interrupts, interrupts usually use a different R register group 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. Specify PL/M-51 functions
Specified by alien.
Previous article:Design of signal processing circuit for RFID reader based on C8051F131
Next article:51 MCU RTL8019AS network card driver
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Overview of TI's 28335 chip
- 【STM32WB55 Review】+First Impressions
- The Relationship between DSP and FPGA in Embedded Development
- DSP Learning (4) Bus Structure
- Vehicle LIN communication error frame problem
- [Technical Tips] It turns out that the ARM+Linux audio solution is so simple!
- Selling some development boards, both new and used.
- FAQ Bluetooth transmission questions and answers
- How to enable TI 15.4-Stack to support 470M frequency band
- What is millimeter wave?