3. Section 3 Storage mode
The storage mode determines the default storage area of variables, function parameters such as storage type, and the like without explicitly specify storage types.
1. SMALL mode
All default variable parameters are loaded into internal RAM, the advantage is that the access speed is fast, the disadvantage is limited, only for the applet.
2. 2. COMPACT mode
All default variables are located on a page (256bytes) of the external RAM area. Which page can be specified by the P2 port, which is described in the startup.a51 file, and the PDATA can also be specified, the advantage is that the space is slower than the small, Compared with Large, it is a middle state.
3. 3. Large mode
All default variables can be placed in an external RAM area of up to 64KB, and the advantages are large, and there are many variables, and the disadvantage is slower.
Tip: The storage mode is selected in the C51 compiler option.
4. Section 4 Storage Type Declaration
The storage type of the variable or parameter can specify the default type by the storage mode or by the keyword direct declaration. Each type is: Code, Data, Idata, XDATA, PDATA, Example:
Data UAR1
Char code array [] = "Hello!";
Unsigned char XData Arr [10] [4] [4];
5. Section 5 variables or data types
The C51 provides the following extension data types:
Bit bit variable value is 0 or 1
SBIT from bit variables defined in bytes 0 or 1
SFR SFR byte address 0 ~ 255
SFR16 SFR word address 0 ~ 65535
The remaining data is as follows: CHAR, ENUM, SHORT, INT, LONG, FLOAT, etc. are the same as ANSI C.
6. Sixth section variables and statements
1. 1. Bit type variable
Bit type variable can be used, function declaration, function return value, etc., stored inside RAM20H ~ 2FH.
note:
(1) Use the #pragma disable description function and the function specified by "USIGN" cannot return BIT values.
(2) A BIT variable cannot be declared as a pointer, such as bit * ptr; it is wrong
(3) Do not have BIT arrays such as: Bit Arr [5]; error.
2. 2. Dependent placement area Description 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;
7. Seventh section Keil C51 pointer
The C51 supports general pointers and memory pointers (Memory_Specific Pointer).
1.1 general pointer
The declaration and use of the general pointer is the same as the standard C, but it can also explain the storage type of the pointer, for example:
LONG * State; is a pointer to the LONG type integer, and State itself is stored according to the storage mode.
Char * xData PTR; PTR is a pointer to the char data, and the PTR itself is placed in an external RAM area, and the data indicated 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 memory type, high offset, and low offset.
2. 2. Memory pointer
The storage type is specified when the memory-based pointer description is specified, for example:
Char Data * Str; STR points to the CHAR type data INT xData * POW; POW points to the external RAM.
This pointer is stored, just one byte or 2 bytes is enough because only the offset is stored.
3. 3. Pointer conversion
That is, the pointer is transformed between the two types:
l When the memory-based pointer is passed to a function that requires a general pointer, the pointer is automatically converted.
l If the external function is not explained, the memory-based pointer is automatically converted to a general pointer, resulting in an error, and thus describes the original shape with "#include".
l You can change the pointer type.
8. Section 8 KEIL C51 functions
The C51 function declaration is extended to ANSI C, including:
1. 1. Interrupt function declaration:
The interrupt declaration method is as follows:
void serial_isr () Interrupt 4 [using 1]
{
/ * ISR * /
}
In order to improve the fault tolerance capabilities of the code, generate the IRET statement at the unused interrupt entry is generated, and the unused interrupt is defined.
/ * Define Not buy Interrupt, So Generate "IRet" in their entrance * /
Void extern0_isr () interrupt 0 {} / * not available * /
Void Timer0_isr () Interrupt 1 {} / * not available * /
Void extern1_isr () interrupt 2 {} / * not buy * /
Void Timer1_ISR () Interrupt 3 {} / * not available * /
Void Serial_ISR () Interrupt 4 {} / * not available * /
2. 2. General Storage Workspace
3. 3. Select the storage workspace by USING X declaration, see above.
4. 4. Specify storage mode
Description by Small Compact and Large, for example:
Void fun1 (void) small {}
Tip: The internal variable inside the small description uses internal RAM. Key often time time consumption can be declared to improve operational speed.
5. #pragma disable
State before the function, only valid for one function. This function call will not be interrupted during the call.
6. 6. Recursive or reusable function
The function that can be called in the main program and interrupts is prone to problems. Because 51 and PCs, PC uses stack transmission parameters, and internal variables other than static variables are in the stack; and 51 generally use registers to transfer parameters, the internal variables are typically in the RAM, and the function will undermine the data last call. . The function can be solved with the following two ways:
A. Use the aforementioned "#pragma disable" declaration before the corresponding function, that is, only one of the main programs or interrupts is allowed to call the function;
b, explain the function as reusable. as follows:
Void Func (param ...).
The Keilc51 is compiled and will generate a reusable variable stack, and then simulate the method of passing the variable through the stack.
Since the typical reproducible function is called by the main program and interrupt, the R register group different from the main program is usually interrupted.
In addition, the re-enter function is added to the corresponding function, and the switch "#pragma noaregs" is added to prohibit the compiler from using an absolute register addressing, which can generate the code that does not depend on the register group.
7. 7. Specify a PL / M-51 function
Specified by Alien.
4. Chapter 4 Keil C51 Advanced Programming
This chapter discusses the following:
l absolute address access
L C and assembly interface
l Universal file in the C51 package
l Segment name conversion and program optimization 1. Section 1 absolute address access
C51 provides three ways to access absolute addresses:
1. 1. Absolute macro:
In the program, use "#include
Cbyte, Xbyte, PWORD, DBYTE, CWORD, XWORD, PBYTE, DWORD
Take a look at Absacc.h
E.g:
Rval = cbyte [0x0002]; pointing to the 0002H address of the program memory
Rval = xword [0x0002]; pointing to the 0004H address of the outside RAM
2. 2. _AT_ Keywords
Directly after the data definition plus _at_ const, but pay attention:
(1) Absolute variables cannot be initiated;
(2) Bit type function and variable cannot be used as _AT_ specified.
E.g:
IDATA STRUCT LINK LIST _AT_ 0x40; Specifies the LIST structure starting from 40h.
XData char text [25b] _AT_0XE000; specifying the TEXT array starting from 0E000H
Tip: If the external absolute variable is the I / O port, you can change the data, you need to use the Volatile keyword, please refer to AbsAcc.h.
3. 3. Connect positioning control
This method is to use the connection control command Code XData PDATA / DATA BDATA to "Segment" address, if you want to specify a specific variable address, there is limitable, not a detailed discussion.
2. The second section of the KEIL C51 and the assembly interface
1. 1. Interface in the module
The method is to use the #pragma statement to the specific structure:
#pragma ASM
Compile
#pragma endasm
This method is essentially by ASM and NDASM to tell the C51 compiler intermediate line without compiling as a compile line, so there is SRC in the compilation control command to control rows that do not compile.
2. 2. Module Interior
The interface of the C module and the assembly module is relatively simple, and the source file is compiled with the A51 separately, and then connect the OBJ file with the L51, the key problem is that the parameter transmission problem between the C function and the assembly function, two in the C51 Parameter transmission method.
(1) Transfer function parameters by register
Only 3 parameters can pass through register, the laws are as follows:
Number of parameters CHAR INT Long, Float General Pointer
123 R7R5R3 R6 & R7R4 & R5R2 & R3 R4 ~ R7R4 ~ R7 R1 ~ R3R1 ~ R3R1 ~ R3
(2) Transfer (Fixed Memory) by fixed storage area
This method passes the Bit type parameters to a memory:
? FUNCTION_NAME? BIT
Bring all other types of parameters to the following paragraph:? Function_name? BYTE, and stored in a pre-selected order.
As for where the fixed storage area itself is, the storage mode is default.
(3) Return value of the function
Function return value is placed in the register, there is the following rules:
RETURN TYPE Registev Description
Bit flag is returned by the specific flag
Char / unsigned char 1_byte pointer R7 single-byte by R7 returns
INT / UNSIGNED INT 2_BYTE pointer R6 & R7 double-byte by R6 and R7, MSB in R6
Long & Unsigned long r4 ~ r7 msb in R4, LSB in R7
Float R4 ~ R7 32bit IEEE format
General Pointers R1 ~ R3 Storage Types in R3 High R2 Low R1
(4) SRC control
This control command compiles the C file to generate assembly file (.src), after the assembly file can be renamed, generate assembly .asm files, and then compile with A51. 3. Universal files in the third section of the Keil C51 package
There are several C source files in the C51 / LIB directory, which has a very important role in this C source file, which can be modified to them, and can be used in their own private system.
1. 1. Dynamic Memory Allocation
INIT_MEM.C: This file is a program source code that initializes the dynamic memory area. It can specify the position and size of the dynamic memory, only INIT_MEM () can be used to adjust other functions, such as Malloc Calloc, Realloc, and the like.
Calloc.c: This file is the source code for array allocating memory, which can specify the unit data type and the number of the unit.
Malloc.c: This file is the source code of Malloc and assigns a fixed size memory.
Realloc.c: This file is a realloc.c source code, which is to adjust the size of the current allocated dynamic memory.
2. 2. C51 launched files Startup.a51
Start file startup.a51 contains the target board startup code, you can add this file in each Project, as long as you reset, the file is immediately executed, and its features include:
l Define internal RAM size, external RAM size, retrnate the stack position
l Clear an external memory of the interior, external or with this page.
l Press the storage mode to the initiatory rendering stack and stack pointer
l Initialize 8051 hardware stack pointer
l to the main () function
Developers can modify the following data to initialize system
Constant name
Idatalen to be clear internal RAM length
XData Start Specifies the starting address of the outside RAM to be cleared
XDATALEN to be cleared outside RAM length
IBPSTACK is a small mode to reinstall the stack pointer to initialize the flag, 1 is required. Default 0
IBPSTacktop Specifies the small mode with the top address of the stack
XbpStack is not a big mode reinstall pointer to initialize the flag, default is 0
XbpStacktop Specifies the top address of the big mode to retrieve the stack
Pbpstack is compact with the stack pointer, the initialization mark is required, the default is 0
PbpStackTop Specifies the address of the COMPACT mode to reinstall the top address
PPAGEENABLE P2 Initialization Allow Switch
PPAGE specifies P2 value
PDATASTART is prepared outside the outside RAM page
PDATALEN to be cleared outside the RAM page length
Tip: If you want to initialize P2 as a high-end address, you must: ppageEnagle = 1, PPAGE is a P2 value, such as specifying a page 1000h-10ffh, then PPAGE = 10h, and the connection must be as follows:
L51 PDATA (1080H), wherein 1080H is any of 1000 hours in 10FFH.
The following is the Startup.a51 code snippet, and red is often required to modify:
; ------------------------------------------------- -----------------------------
THIS FILE IS Part of The C51 Compiler Package
CopyRight Keil Elektronik GmbH 1990
; ------------------------------------------------- -----------------------------
Startup.a51: This Code is Executed After Processor Reset.
;
To translate this file use a51 with the folowing invocation :;
A51 Startup.a51
;
To link the modified startup.obj file to your application use the folowing
L51 INVOCATION:
;
; L51
;
; ------------------------------------------------- -----------------------------
;
User-defined Power-on Initialization of Memory
;
; With the following equ statements the initialization of memory. INTINIZATION OF MEMORY
At Processor Reset Can Be Defined:
;
; The absolute start-address of iData memory is always 0
Idatalen EQU 80h; The Length of Idata Memory in bytes.
;
XDataStart Equ 0h; The Absolute Start-Address of XData Memory
XDATALEN EQU 0H; The Length of XData Memory in bytes.
;
PDATASTART EQU 0H; The Absolute Start-Address of PData Memory
PDATALEN EQU 0H; The Length of Pdata Memory in Bytes.
;
Notes: The Idata Space Overlaps Physically The Data and Bit Areas of The Data And Bit Areas of Thae
; 8051 CPU. At minimum the memory space Occupied from the c51
Run-Time Routines Must Be set to zero.
; ------------------------------------------------- -----------------------------
;
Reentrant Stack Initilization
;
; The following Equ Statements Define The Stack Pointer for Reentrant
Functions and INITIALIZED IT:
;
Stack Space for Reentrant Functions in The Small Model.
IBPSTACK EQU 0; Set to 1 if Small Reentrant Is Used.
IBPSTACKTOP EQU 0FFH 1; Set Top of Stack To Highest Location 1.
;
Stack Space for Reentrant Functions in The Large Model.
Xbpstack EQU 0; Set to 1 if Large Reentrant is buy.
XbpStacktop EQU 0fffh 1; Set Top of Stack To Highest Location 1.
;
Stack Space for Reentrant Functions in The Compact Model.
Pbpstack EQU 0; Set to 1 if Compact Reentrant is buy.pbpstacktop EQU 0FFFH 1; Set Top of Stack To Highest Location 1.
;
; ------------------------------------------------- -----------------------------
;
Page Definition for Using The Compact Model with 64 Kbyte XData Ram
;
; The following equ statements define the xdata Page Used for PDATA
Variables. The Equ Ppage Must Conform with the PPage Control Used
In The Linker Invocation.
;
PPAGEENABLE EQU 0; Set to 1 if PData Object Are Used.
PPAGE EQU 0; Define Ppage Number.
;
; ------------------------------------------------- -----------------------------
3. 3. Standard input and output files
PUTCHAR.C
Putchar.c is a low-level character output subscrotor, and developers can be modified to apply to their hardware systems, such as to CLD or LEN output characters.
By default: putchar.c is output to the serial port XON | XOFF is a flow control flag, and the wrapper "/ * n" is automatically converted to the Enter / Rail "/ r / n".
GetKey.c
The GetKey function is a low-level character input substener that can be used to enter the character in the matrix keyboard input by default.
4. 4. Other files
It also includes an init.a51 function with a unique function of Watch-Dog and a function of the 8 × C751 applicable to the source code.
4. Fourth Section Agreement and Program Optimization
1. Segment name agreement (Segment Naming Conventions)
The target file generated by the C51 compiler is stored in many segments, which are some units of code space or data space, and one segment can be relocated, or it can be an absolute segment, each of which has one heavy position. Types and names, C51 paragraphs have the following provisions:
Each segment name includes two parts of the prefix and module name, the prefix indicates the storage type, the module name is the name of the compiled module, for example:
? CO? Main1: Represents part of the code segment in the MAIN1 module
? PR? FUNCTION1? The executable segment of the function function1 in the Module table module, specific specified reference manual.
2. 2. Program Optimization
The C51 compiler is a compiler with an optimized function, which provides a total of six level optimization functions. Ensure the highest efficiency of the target code (the least code is the fastest). The specific six-level optimized content can be referred to.
Provide the following compilation control command control code optimization in C51:
Optimize (sjxe): Try to use subroutines to reduce the program code.
NOAREGS: Do not use absolute register access, program code and register segment independent.
NOREGPARMS: Parameter passes is always implemented in local data segment, program code compatible with low version C51.
Optimize (SIZE) AK Optimize (Speed) provides 6-level optimization feature, which is default: Optimize (6, speed).
5. Chapter 5 Keil C51 Library Function Reference C51 Powerful Features and Its High Efficiency Important Reflections Licensing Database Functions, Multi-Library Functions make program code simple, clear structure, easy to debug, and maintenance, The library function system of the C51 is described below.
1. Section 1 This library function (Intrinsic routines) and non-book library functions
The intrinsic function provided by the C51 refers to the compile time to insert the fixed code into the current row, rather than using the ACALL and LCALL statements, which greatly provides the efficiency of functional access, not the intrinsic function, must be by ACALL and LCALL. transfer.
The C51's intrinsic library function is only 9, and the number is small, but it is very useful, listed as follows:
_crol _, _ cror_: Return the CHAR type variable loop to the left (right)
_IROR _, _ IROL_: Return the INT type variable loop to the left (right)
_lrol _, _ lror_: Return the LONG type variable loop to the left (right)
_NOP_: Equivalent to insert NOP
_TestBit_: It is equivalent to the JBC Bitvar to test the bit variable and jump at the same time.
_CHKFLOAT_: Test and return the source point status.
When using, #inclucle If not, the library function described below refers to a non-intrusive library function.