2. Several types of important library functions in the second quarter
1. 1. Special Register Include file
For example, 8031, 8051 is REG51.H which includes all 8051 SFR and its bit definition, and the general system must include this file.
2. 2. Absolute address incline file Absacc.h
Several macros are actually defined in this file to determine the absolute address of each storage space.
3. 3. Dynamic memory allocation function, located in stdlib.h
4. 4. The buffer processing function is located in "string.h"
These include functions such as copy comparison movements such as:
Memccpy Memchr Memcmp Memcpy Memmove MemSet
This makes it easy to process the buffer.
5. 5. Enter the output stream function, located in "stdio.h"
The serial port or user-defined I / O port data of the stream function pass 8051 is defined by 8051 serial port. For example, if you want to modify, if you change to the LCD display, you can modify the getKey.c and putchar.c source files in the lib directory. Then replace them in the library.
3. Group 3 KEIL C51 library function prototype list
1. CType.h
Bit Isalnum (Char C);
Bit Isalpha (Char C);
Bit Iscntrl (Char C);
Bit isdigit (Char C);
Bit isgraph (char C);
Bit Islower (CHAR C);
Bit isprint (char C);
Bit ISPUNCT (CHAR C);
Bit Isspace (Char C);
Bit isupper (Char C);
Bit isxdigit (char C);
Bit toascii (Char C);
Bit tool (char C);
Char TOLOWER (CHAR C);
Char __tolower (char C);
Char Toupper (CHAR C);
Char __toupper (char C);
2. 2. Intrins.h
Unsigned char _crol_ (unsigned char c, unsigned char b);
Unsigned char _cror_ (unsigned char c);
Unsigned char _chkfloat_ (float ual);
Unsigned int _Irol_ (unsigned int i, unsigned char b);
Unsigned int _iror_ (unsigned int i, unsigned char b);
Unsigned long _irol_ (unsigned long l, unsigned char b);
Unsigned long _iror_ (unsigned long l, unsigned char b);
Void _nop_ (void);
Bit _TestBit_ (bit b);
3. 3. STDIO.H
Char getchar (void);
Char _getKey (void);
Char * Gets (Char * String, Int Len);
INT Printf (const char * fmtstr [, argument] ...);
Char Putchar (CHAR C);
INT PUTS (Const Char * String);
INT scanf (const char * fmtstr. [, argument] ...); int sprintf (char * buffer, const char * fmtstr [; argument]);
INT SSCANF (Char * Buffer, Const Char * fmtstr [, argument]);
CHAR UNGETCHAR (CHAR C);
Void vprintf (const char * fmtstr, char * argptr);
Void vSprintf (Char * Buffer, Const Char * FMTSTR, Char * Argptr);
4. 4. STDLIB.H
FLOAT ATOF (VOID * STRING);
int Atoi (VOID * STRING);
Long atol; Void (VOID * STRING);
Void * Calloc (unsigned int Num);
Void Free (void xdata * p);
Void init_mempool (void * data * p, unsigned int size);
Void * malloc (unsigned int size);
Int rand (void);
Void * Realloc (Void xData * p, unsigned int size);
Void Srand (int seed);
5. 5. String.h
Void * Memccpy (Void * Dest, Void * SRC, CHAR C, INT LEN);
Void * Memchr (Void * BUF, CHAR C, INT LEN);
CHAR MEMCMP (Void * BUF1, VOID * BUF2, INT LEN);
Void * Memcopy (Void * DEST, VOID * SRC, INT LEN);
Void * Memmove (Void * DEST, VOID * SRC, INT LEN);
Void * MEMSET (Void * BUF, CHAR C, INT LEN);
Char * STRCAT (Char * DEST, CHAR * SRC);
Char * strChr (const char * string, char c);
CHAR STRCMP (Char * string1, char * string2);
Char * STRCPY (Char * DEST, CHAR * SRC);
Int strcspn (char * src, char * set);
int Strlen (Char * SRC);
Char * Strncat (Char 8Dest, Char * SRC, INT LEN)
Char strncmp (char * string1, char * String2, int LEN);
Char Strncpy (Char * Dest, Char * SRC, INT LEN)
Char * StrPbrk (Char * String, Char * SET);
Int strpos (const char * string, char c);
Char * Strrchr (Const Char * String, CHAR C);
Char * StrPbrk (Char * String, Char * SET);
Int Strpos (Const Char * String, Char C);
INT strspn (char * string, char * set);
6. Chapter 6 Keil C51 example: Hello.c
Hello is located / c51 / eXcmples / hello / directory, which is functional to the serial port output "Hello, World" The entire program is as follows:
#Pragma DB OE CD
#indule
Void main (void)
{
SCON = 0x50;
TMOD = 0x20
TH1 = 0xF3;
Tri = 1;
Ti = 1;
Printf ("Hello, WORLD / N");
While (1) {}
}
1. The first section of the UVISION for Windows
(1) File_new Create a new Hello.c file, enter the above or direct directory file file.
(2) File_save or toolbar to save the file.
(3) Project_New Project Creates a Project named Hello and add Hello.c.
At this time, the Project is already open, or opens an existing Project with Open Project.
(4) Option_c51 Compiler is selected at least two DB OEs.
(5) OPTION_DSCope Debugger selection Hello / DS51.ini
View ds51.ini to see if it is:
"LOAD ... / ... / BIN / 8051.DLL
Map 0, 0xffff "
Otherwise modify it.
(6) Select the order of Make file in Option_make.
(7) Project Select Build Project to see if there is a syntax error, if you do not generate a HEX file, if you have a modified source file, you repeat the above steps.
(8) Run_Dscope debugger After entering the DSCope51, the Hello can be installed with Go. You can use the Go directly to see whether the Serial window is output, and each system is running once. The Serial window has a "Hello, World" indicates that the run is correct.
2. Section 2 ISHELL for DOS use steps
(1) Enter iShell with the setup Editer selection editor.
Then click Edit or edit the Hello.c source file with the Edit command, the store, or you can select Hello.c directly in the Files window.
(2) Change the Project directory to the Hello directory with the CD.
(3) Select 8051 at the setup_target project.
(4) Output DB OE in Setup_C51.
(5) Enter Project name Hello in Setup_Project.
(6) Save the iShell.cfg file in setup_save.
(7) Edit a link file Hello.lin has "Hell.obj" a line.
(8) On Hello.c in the Files menu, click "Translate", if there is no syntax, then "link", then the hex file is generated.
(9) Click Simulate, such as simulation, in 8051.cdf, simulation is dSCOPE, enter DSCope debugging direct "Go", see the Serial window output to "Hello.World".
(10) If the program is incorrect to modify the source code, it will not be needed to transfer or link, as long as one step is Amake.
If the Project includes more than one file, you cannot compile with Translate in the ISHELL of DOS, and the BAT file should be established and compiled directly, then the link is connected.
If you need to use Translate, you can only compile, then you can connect.
7. Chapter 7 Code Efficiency of Keil C51
The C51 program compiles the efficiency of assembly code, which is determined by many factors. For Keil C51, it is mainly affected by the following two factors:
1. Influence of the first storage mode
The storage mode determines the storage space of the default variable, and the degree of intendment of assembly code accesses each spatial variable determines the level of code rate.
For example: a plastic variable I, such as the memory 18h, 19h space, then i's operation compiles to four statements: inc 0x19
Mov A, 0x19
JNZ 0x272D
INC 0x18
0x272d:
And if you put in the deposit space 0000h, 0001H The operation of i is compiled into nine statements:
Mov DPTR, 0001
Movx a, @ DPTR
INC A
Movx @ DPTR, A
JNZ # 5
Mov optri, # 0000
Movx a, @ DPTR
INC A
Movx @ DPTR, A
For the compilation of the statement, the operation of the external memory is much lower than the internal memory operation code rate, the generated statement is twice the memory, and there is a large number of this operation in the program, and the memory mode is visible to the code rate. The ring.
Therefore, the principle of programming is
1. Storage mode selection from Small-Compact-Large, which is really variable, and selects the LARGE mode.
2. Even if the Large mode is selected, some common local or can be placed in memory, it is best to be placed in memory to minimize the code rate of the program.
2. Influence of the second section program structure
The structural unit of the program includes modules, functions, and the like. The same function, the more complex, the more operations, variables, function module functions, the more structural, and the simple program is much lower than that of the code.
In addition, the operation control statement of the program is also a key factor affecting the code rate. For example: Switch -case statement, many compilers translate them very complicated, Keil C51 is no exception, relatively simple Switch-Case statement, compile Jump instruction form, the code rate is high, but to the more complex switch-case, you want to call a system library function? C? ICase for processing, very complicated.
Rethinking, such as if (), while (), and other statements are also relatively low statements, but after compilation is much higher than Switch-Case.
Therefore, it is recommended that the designer should use the Switch-Case statement to control the program structure to increase the code rate.
In addition to the above two points, other factors will also affect the code rate, for example:
Whether to pass the parameters in the register, the noaregs option is
Does it include debugging information: The debug option
Does it include extended debugging information: BJECTEXTEND
8. Chapter 8 DSCope for Windows
1. Section 1 Overview
1. 1. Main window (Mainframe Window)
Other various debug windows can be set, set breakpoints, observe points, modify address space, load file, etc.
2. 2. Debug Window
Support for various display methods for user programs, run continuously, run the user program in a single step, and assemble online;
3. 3. Command window (Command Window)
Support for the input of the command line;
4. 4. Watch Window
The variables, expressions, etc. to be observed;
5. 5. Register window (RegisTe Window)
Display the content of the internal register, the number of programs, etc.
6. 6. Serial window (SERICAL Windows)
Display the data received and transmitted by the serial port;
7. 7. Performance Analysis Window
Display the space of each block to be observed to occupy the space of the CPU; 8. 8. Memory Window
Displays data in the selected memory;
9. 9. Symbol Browse Window (Symbol Browser Window)
Display Various symbol names, including proprietary symbols, user-defined symbols (function names, variables, labels), etc .;
10. 10. Call-stack window (Call-Stack WINDOW)
Dynamically displays the function call relationship of the currently executed program segment;
11. 11. Code coverage window
Provides the ratio of the executed code in each block in the current module;
12. 12. Peripheral Window (Peripherals)
Peripherals such as I / O port, timer, interrupt, serial port can be displayed;
2. Section 2 DSCope for Windows Basic Operation
1. 1. Specify the initialization file
In the Uvision's Option Menu DScope Debugger, specify the initialization file of the DScope, starting the DSCope with Uvision's Run will automatically load this initialization file, automatically execute the command;
Here is an example that you can see the process of transferring a debug code. DS51.INI:
LOAD 8051.DLL
Load test
Slog >> Test.log
Xtal = 11.0592
Define Button "Go to Main", "G, Main"
WS Revcounter
WS RM.R
g, main
Pa RESET
PA Serial
PA Timer0
2. 2. Observe the variable
Method 1: Command line
WS Expression [, Numberbase] [line]
Where NumberBase is a display number, 10 correspondence, 16 correspondence, default is 16. LINE is displayed in a single line, default is multi-line display.
Method 2: Setup-> WatchPoints, enter variables in the dialog
3. 3. Display the value of RAM
D i (x, d): start address, termination address
d Variable name
4. 4. Observe the stack
View-> Call-Stack-> Show Invocation, you can track the calling process;
5. 5. Interrupt handler debugging
After loading 8051.dll, PERIPHERIAL will be added in the main menu of DSCope, and there are 4 words menu:
I / 0 port: PI port status
Interrupt: Interrupt settings
Timer: Timer Interrupt Status
Serial: Serial Interrupt Status
Set the corresponding interrupt request flag to generate an interrupt.
6. 6. Performance Analyzer: PA)
PA is used to analyze a percentage of a code execution to occupy the CPU. definition:
Command line PA FUNC_NAME
3. Section 3 DSCope for Windows Command File
In addition to debugging with the command line, DSCOPE can collect various debug commands in a debug file, then call the file to reach the purpose of automatically testing the source code. The DSCope command file supports the format of C / PL / M, so preparing a debug command file is something similar to the compilation C language program.
1. 1. Address space and address space type
1. (1) address space segmentation
The maximum available space provided by DSCOPE is 16m, in fact, we only use the following three paragraphs:
1 internal data space segment (0x00 segment or D)
0x00: 0x0000 ~ 0x00: 0xfff (0x00FF for MSC51)
2 external data space segment (0x01 segment or X)
0x01: 0x0000 ~ 0x01 ~ 0xffff
3 program space segment (0xFF segment or C)
0xFF: 0x0000 ~ 0xFF: 0xffff
2. (2) Address space type
C: Code Space
D: internal direct addressing space
I: Internal indirect addressing space
X: External data space
B: Bit Addressing Space
P: I / O port
EB: Extended bit addressing space (MCS251 proprietary)
ED: Extended data space (MCS251 is available)
CO: constant space (MCS251 is available)
HC: Normal Space (MCS251)
2. 2. Constant
DSCope supports hexadecimal, octal, decimal, binary constant, which is H, Q (O), T (or no), Y;
DSCOPE does not distinguishes large, lowercase.
1. (1) Integer constant
Divided into integer (int), no symbolic (UINT, 00RD), long integer (long), unsigned long (WLONG, WORD).
2. (2) Floating point constant
The same as ANSI C.
3. (3) String Region
The same as ANSI C
4. (4) Character constant
Divided into characters (char) and unsigned characters (UCHAR).
5. (5) Line Number Constant
Refers to the line number in the user program, actually an address
6. (6) Bit Constant (Bit):
0 and 1
7. (7) Address constant
There are many types of address constants, and the address constant is different from the line number constant. The line number constant is an address, and the number of addresses is referenced, and the data in the address is actually taken.
C: Code address constant, such as C: 0x0012 or 0xFF: 0x0012
D: Internal direct addressing address constant, such as D: 0x0068 or 0x00: 0x0068
I: Interior, press addressing address constant, such as i: 0x0010 or 0x00: 0x0010
X: External data space address constant, such as x: 0x0028 or 0x01: 0x0028
B: Bit address constant, such as B: 0x20 or B: 0x24.0
EB: Extended bit address constant (MCS251 is available),
ED: Extended data space address constant (MCS251 is available)
CO: constant space address constant (MCS251)
HC: Normal Number Space Address Constant (MCS251)
8. (8) Identifier constant
That is, the label, function name, etc. in the user source program, actually represents a certain address.
9. (9) Constants defined in the user source program
3. 3. Variables
The variable name or identifier supported by DSCope can consist up from 31 characters. The first letter is A ~ Z, A ~ Z, the underscore or question mark, and subsequent characters can be letters, numbers, underscores and question marks. In addition to CPU variables and system variables, DSCope does not support global variables, but the variables defined by the "define" command are global variables. The variables supported by DSCope are divided into the following (variable names are not distinguished, lowercase), support type conversion:
1. (1) Integer variable
Divided into integer variables (int), unsigned integer variables (UINT / Word), long integer (long), unsigned long integer (ULONG / DWORD).
2. (2) Floating point variable (FLOAT)
The same as ANSI C.
3. (3) Characterized variable L
Divided into characters (char) variables and unsigned characters (UCHAR)
4. (4) Bit Variables (Bit)
5. (5) System variable
DSCope defines a series of internal variables that can read or read / write these variables, which can be referenced by the user's custom number.
a. cycles (read only)
32-bit variable (ULONG) indicates that the current program executes the cost of instructions (CYCLE).
b. ramsize (r / w)
16-bit variable (UINT) indicating the size of the data space inside directly addressed.
c. RADIX (R / N)
8-bit variable (UCHAR), decided to output the output
RADIX = 0x0a (10), radix = 0x10 (16)
d. -IIP- (r / w)
8-bit variable (UCHAR) indicating the current interrupt nested number.
e. $ (r / w)
32-bit variable (ULONG) indicates the PC value, and can change the process executed by writing to it.
f. itrace (r / w)
8-bit variable (UCHAR) decides whether to record the program operation
ITrace = 1, enable record operation
ITrace = 0, fundamentally recorded
g. __break __ (r / w)
8-bit variable (UCHAR) __BREAK __ = 1, the operation of the suspension program
h. __mode__ and __frame size__ is the proprietary variable of the MCS 251.
6. (6) CPU variable
That is, R0 ~ R7, A, C (bit variable), B, DPTR, and special function register variables can be read and written on these variables.
7. (7) Variables, arrays, structures, etc. defined in the user source program
4. 4. Operator
DSCope supports ANSI C operators, including arithmetic operators, logical operators, and relational operators.
5. 5. Expression
With the operator, the constants, variables, functions, functions such as DSCope, constitute DSCOPE expressions.
6. 6. A number of groups
DSCope does not support arrays in command files, but can reference arrays in the user program, reference mode as C.
7. 7. Structure and Union
DSCope does not support the definition structure and joint in the command file, but can reference the structure and joint in the user program, the reference mode is like c, but if you want to output the entire structure or the result, you must use the command "OBJ". 8. 8. Pointer:
You cannot customize the pointer, but support the pointer variables in the user source program.
9. 9. DSCope Command Statement
DSCope provides a series of debug commands. In the command file, DSCope only supports these statements and the representations of the forewords, and the C language statements are not supported, but the user-defined function (the function in the non-user source program) is supported in the command file. However, arrays, structures, joints, and pointers are also not supported in user-defined functions.
1. (1) ASM
The online assembly command, the format is as follows:
ASM C: 0xnnnn (or label); setting the address inserted into the assembly instruction
ASM assembly instruction
ASM assembly instruction
After insertion, select "Assemble-> assemble" to complete compilation in the Debug window.
2. (2) Assign
The serial port allocation instruction, the format is as follows:
Assign Channel
For MCS51: Assign Win
But the current DSCope version does not provide a full serial port window.
3. (3) Define
User-defined variable instructions, the format is as follows:
Define
Type One is a variable type as described above, and the variable defined by the define command may be a global variable, which can be referenced for user-defined functions.
4. (4) Display
The memory display command, the format is as follows:
D start address, end address
The address is as previously described, identifying the constant.
5. (5) ENTER
Memory modification instruction, the format is as follows:
E Type Address = Expression [Expression 2], [...]
The type of address constant described earlier is as described above. Expression as described above, but if it is a function name (including label, pointer variable), keyword E → EP
6. (6) Map / Reset Map
MAP is modified for memory segment, and reset Map resets or defaults in memory segments.
7. (7) Object
Used to reference structural (combined), arrays, formats in the user source program:
Obj expression [n,], [line]
Expression is an array, structure (combined) name in the user source program. When Line defaults, the number, structure (combined) content is output by N row; if there is line, the single line output.
8. (8) U
The disassembly command, the format is as follows:
U [address]
The address includes address constitudes and identifier, indicating the starting address of the disassembly.
9. (9) WK
The observation point deletes the command, the format is as follows:
WK N1 [N2], [...]; Delete the specified observation point, n is a character type, integer
constant
WK *; delete all observation points
10. (10) WS
The observation point setting command, the format is as follows:
WS expression [, n] [line]
When the keyword line exists, observe the point expression in a single line output
Line default, observe the point expression N row output.
11. (11) g
Continuous run command, the format is as follows:
G [start address], [termination address]
The address is an identifier constant or address constant, and the address is default, it is running. 12. (12) T / P
Single step running command, the format is as follows:
T / P n; n When the number of steps to the single line, P refers to the user as a function as a function, does not enter the function.
13. (13) PA
Performance Analysis Operation Directive, which is divided into the following:
PA
Displays the currently set performance analysis level
Pa Kill