Summary of knowledge of C language for single-chip microcomputer (2)

Publisher:EnchantedMelodyLatest update time:2022-12-14 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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;


Reference address:Summary of knowledge of C language for single-chip microcomputer (2)

Previous article:Summary of important knowledge points of C51 microcontroller
Next article:Summary of knowledge about C language for single-chip microcomputer

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号