C51 constants
1. Description of constant data types:
(1) Integer constants can be expressed as decimals such as 123, 0, -89, etc. Hexadecimal numbers start with 0x such as 0x34,-0x3B, etc. For long integers, add the letter L after the number, such as 104L, 034L, 0xF340, etc.
(2) Floating-point constants can be divided into decimal and exponential representations. The index is expressed in the form of [±]number[.number]e[±]number. The content in [] is optional. The content is optional according to the specific situation, but the remaining parts are required, such as 125e3, 7e9, -3.0 e-3.
(3) Character constants are characters within single quotes, such as 'a', 'd', etc. For control characters that cannot be displayed, a backslash "" can be added in front of the character to form a special escape character. Please see the table for commonly used escape characters:
(4) String constants consist of characters within double quotes, such as "test", "OK", etc. When there are no characters within the quotation marks, it is an empty string. When using special characters, also use escape characters such as double quotes. In C, string constants are processed as character type arrays. When storing a string, the system will add the o escape character at the end of the string as the terminator of the string. The string constant "A" is different from the character constant 'A'. The former occupies one more byte of space during storage.
(5) Bit scalar, its value is a binary.
2. Application
constants can be used in situations where the value does not need to be changed, such as fixed data tables, fonts, etc. There are several ways to define constants, which are explained below.
#difine False 0x0;//Constants can be defined using predefined statements
#difine True 0x1;//False is defined here as 0 and True as 1
//When False is used in the program, it will be automatically replaced with 0 during compilation, and True is replaced by 1
unsigned int code a=100;//This sentence uses code to define a in the program memory and assign a value
const unsigned int c=100;//Use const to define c as an unsigned int constant and assign a value. The values of the above two sentences are the same. It is stored in the program memory, and the program memory is not allowed to be modified during operation, so if an assignment statement like a=110, a++ is used after these two sentences, an error will occur during compilation.
3. C51 variables
1. Variable format
[storage type] Data type [memory type] Variable name table
Except for the data type and variable name table that are necessary in the definition format, the others are optional.
2.
Storage types There are four types of storage: automatic (auto), external (extern), static (static) and register (register). The default type is automatic (auto).
(1) Static (static local) variables
will not release memory during the entire running time of the program. If no value is assigned when defining a local variable, it will be automatically assigned a value of 0 during compilation. For automatic variables, if they are not assigned a value when they are defined, they will have an uncertain value. Other functions cannot be referenced.
(2) Use extern to declare external variables.
A program can be composed of multiple source program files. If a program needs to reference an external variable that has been defined in another file, it needs to be declared using extern.
Example: In one file: int abc;
in another file: extern abc;
3. Data type
(1) Data type
The highest-order byte in the byte represents the sign of the data, "0" represents a positive number, "1" represents a negative number, and negative numbers are represented by two's complement.
(2) Special
bit Bit scalar is an extended data type of the c51 compiler. It can be used to define a bit scalar, but it cannot define a bit pointer or a bit array. Its value is a binary bit, either 0 or 1, similar to True and False in the Boolean type in some high-level languages.
sfr is also an extended data type. It uses a memory unit and the value range is 0~255. It can be used to access all special function registers inside the 51 microcontroller.
sfr16 occupies two memory units, and the value range is 0~65535. sfr16 is used to operate special function registers like sfr, but the difference is that it is used to operate registers that occupy two bytes, such as timers T0 and T1. sfr16 T2 = 0xCC; //The 8052 timer 2 is defined here, the address is T2L=CCH, T2H=CDH. When using sfr16 to define a 16-bit special function register, the equal sign is followed by its low-order address, and the high-order address must be located above the physical low-order address. Note that it cannot be used for the definition of timers 0 and 1.
sbit is also an extended data type in the C language of the microcontroller. It can be used to access the addressable bits in the RAM inside the chip or the addressable bits in the special function register. As previously defined, sfr P1=0x90; //Because the register of the P1 port is bit addressable, sbit P1_1=P1^1;//P1_1 is the P1.1 pin in P1. Similarly, we can use the address of P1.1 to write, such as sbit P1_1=0x91; in this way, P1_1 can be used to read and write the P1.1 pin in subsequent program statements.
(3) Statement typedef that redefines the data type
The syntax of typedef: typedef existing data type new data type name
样写:typedef int integer; integer a,b;
typedef cannot be used directly to define variables. It only replaces the name of an existing data type and does not create a new data type.
4. Memory type
Specify the storage area used by this variable in the microcontroller C language hardware system, and accurately locate it during compilation.
注意的是在AT89c51芯片中RAM只有低128位,位于80H到FFH的高128位则在52芯片中才有用,并和特殊寄存器地址重叠。如果省略存储器类型,系统则会按编译模式SMALL,COMPACT或LARGE所规定的默认存储器类型去指定变量的存储区域。无论什么存储模式都能声明变量在任何的8051存储区范围,然而把最常用的命令如循环计数器和队列索引放在内部数据区能显著的提高系统性能。还有要指出的就是变量的存储种类与存储器类型是完全无关的。
(1) Address of special register (SFR)
AT89C51 special function register list (applicable to chips of the same architecture)
Special function registers marked with * are bit-addressable registers.
(2) Data storage mode
①Small mode: All default variable parameters are loaded into the internal RAM. The advantage is fast access, but the disadvantage is limited space and 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. It is explained in the STARTUP.A51 file. It can also be specified by pdata. The advantage is that it has more space than Small and is faster. Small is slower and faster than large. It is an intermediate state.
③Large mode: All default variables can be placed in an external RAM area of up to 64KB. The advantage is that it has large space and can store many variables, but the disadvantage is that it is slower.
5. Keil c51 pointer variable
The microcontroller c language supports general pointer (Generic Pointer) and memory pointer (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: char * xdata ptr; ptr is a pointer to char data, and ptr itself is placed in the external RAM area. Generally, the pointer itself is stored in 3 bytes, which are memory type, high offset and low offset.
(2) Memory pointer
The memory-based pointer specifies the storage type when describing it, for example: char data * str; str points to char type data in the data area; when storing this kind of pointer, only one byte or 2 bytes are enough, because Just store the offset.
(3) Pointer conversion
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 into a general pointer, causing an error. Therefore, please use "#include" to specify all function prototypes.
6. Several methods for space allocation of variables in the C language of single-chip microcomputer
(1) The data area has a small space, so only variables that are frequently used or require high computing speed are placed in the data area, such as the count value in a for loop.
It is best to place local variables in the data area. The local variable space is released when exiting the function, except for static local variables, whose memory usage is the same as global variables;
(2) Make sure there are no uncalled functions in your program. In Keil
When an uncalled function is encountered in C, the compiler considers it to be an interrupt function. The space of local variables used in functions is not released, that is, it is treated the same as global variables.
(3) The logical flag variables encountered in the program can be defined in bdata, which can greatly reduce the memory footprint.
(4) Other variables that are not frequently used and do not require high computing speed are placed in the xdata area. If you want to save data space, you must use large mode and place all variables with undefined memory locations in the xdata area. Of course, it is best to specify the memory type for all variables.
(5) When using a pointer, specify the memory type pointed by the pointer. A general pointer to an undefined memory type occupies 3 bytes; a pointer specified to the data area only occupies 1 byte; a pointer specified to the xdata area occupies 2 bytes. If the pointer p points to the data area, it should be defined as: char
data *p;. You can also specify the storage memory type of the pointer itself, such as: char data *xdata p;
Previous article:Summary of important knowledge points of C51 microcontroller
Next article:Summary of knowledge about C language for single-chip microcomputer
- 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
- 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
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- Development environment installation (II) Cross-compilation environment
- Interpretation and test application of IEC harmonic standards
- VICOR engineers invite you to chat: How to improve the throughput and running time of automatic test equipment?
- About MSP430-Timer WDT
- Does anyone know the pin map of the YC1021 chip?
- Why Japan failed to kill South Korea's semiconductor industry
- How to select all device bit numbers or nominal values in the SCH file for AD1904 version
- Protocols and services required for the WiFi authentication process
- Testing methods and diagnostic analysis for DSP-containing circuit boards
- Fundamentals of Circuits and Analog Electronics Technology