Overview of C51 Language

Overview of C51 Language

Little Science Station

1. Overview of C51 Language

Overview of C51 Language

1) Advantages: C51 is a high-level programming language designed for the 51 microcontroller and is a subset of standard C language. It features strong readability, easy debugging and maintenance, and a small programming workload. It allows direct access to physical addresses, enabling direct operations on hardware and achieving some functionalities of assembly language, thus combining features of both high-level and low-level languages, making it widely applicable. Currently, C51 has become the mainstream programming method for developing programs for the 51 microcontroller.

Structured language, compact code – efficiency comparable to assembly language (but still not as good).

Close to real language, strong program readability – easy to debug and maintain.

Rich library functions, small programming workload – short product development cycle.

Strong machine-level control capability – suitable for embedded system development.

Independent of assembly instructions, easy to master – quick to get started on the microcontroller.

2) Disadvantages: Execution efficiency is not as good as assembly language.

Source programs written in C51 cannot be directly recognized by the computer; they must be converted into executable language (or target code) before execution.

The method of converting the high-level language source program into target code entirely before execution is called compiled execution.

C51 uses compiled execution, and the generated target code can be executed independently of the C51 programming environment, resulting in fast execution speed and high code efficiency.

The method of converting and executing the source program simultaneously is called interpreted execution.

Overview of C51 Language

2. C51 vs C Language

Similarities: Syntax rules, program structure, programming methods.

Differences: Data structures, interrupt handling, port expansion.

Overview of C51 Language

3. Creating a C51 Program

“Create a new project file” → “Select microcontroller” → “Edit source program” → “Add source program” → “Modify project configuration” → “Compile program” → “Generate executable file”.

Overview of C51 Language

4. Program Structure of C51

· The basic unit of a C51 program is a function; a C51 source program must contain at least one main function, which can also include several other functions.

· The main function is the entry point of the program;

· When all statements in the main function have been executed, the program ends.

Overview of C51 Language

Example: LED blinking control function.

Overview of C51 Language
Overview of C51 Language

3. Data Structures in C51

1. Variables in C51

a) In C language programming, a quantity that can change is called a variable.

b) The basic attributes of a variable are its name and value.

c) When defining a variable in a program, the C51 compiler allocates the corresponding storage unit for that variable, thus binding the variable name with the address of the storage unit, and the variable value corresponds to the content of the storage unit (if different content is placed in the storage unit, the variable will have different values).

d) The process of using a variable is to find the corresponding memory address through the variable name and then perform data reading operations on that storage unit.

Overview of C51 Language

Factors to Consider When Using Variables:

a) If a variable needs to correspond to larger values, it requires linking multiple consecutive storage units to increase the bit count, which involves the issue of variable data types.

b) The 51 microcontroller has both internal RAM and external RAM, which involves the issue of variable storage types.

c) Permanently binding variables to storage units reduces the utilization of storage units; dynamically allocating storage units temporarily reduces the convenience of using variables, which involves the issue of variable storage categories.

Complete format for defining a variable (4 main elements):

[Storage category] Data type [Storage type] Variable name;

Variable definition must also consider the variable’s scope.

To improve variable storage efficiency, a more scientific approach is:

① For variables that only have current usage value, they can be automatically released after use to allow the compiler to reallocate variable storage space; — auto

② For variables with long-term usage value, they can be kept under static protection and not released during program execution; — static

③ For variables that need to pass data between multiple programs or functions, they can be defined in one place, with their external attributes declared in other programs or functions to achieve data sharing; — extern

④ For variables that need to frequently change their values, their values can be stored in the CPU’s registers to avoid repeated memory access, thus achieving higher execution efficiency. — register

There are 4 types of variable storage:

1. Automatic type (auto).

Variables with the auto attribute are called automatic variables. The scope of automatic variables is within the function body or statement block where they are defined. When the function call ends or the statement block execution is complete, the storage unit occupied by automatic variables is released. Since the values in the storage unit are random, the initial value of automatic variables before assignment is also random. Automatic is the default option for “storage category”; if the “storage category” item is omitted when defining the variable, the variable is assumed to be automatic.

2. Static type (static).

Variables with the static attribute are called static variables. The scope of static variables is within the function body, program file, or statement block where they are defined. Static variables have three characteristics: variable hiding, persistent storage, and default initial value of 0.

If you want a variable to retain its value after leaving the scope, or to prevent it from being used by other variables with the same name outside the scope, or to ensure that a variable defined without an initial value defaults to 0, you can declare it with static during variable definition.

3. External type (extern).

Variables with the extern attribute are called external variables. If the definition and use of a variable are not in the same scope, it can be extended to the declaration location using extern, thereby bringing the variable value into the new scope. The extensibility of extern is the opposite of the hiding nature of static. After a variable is declared extern, it can be allocated a fixed storage unit and remain valid throughout the program’s execution period.

4. Register type (register).

Variables with the register attribute are called register variables. If a variable needs to frequently exchange data with memory during use, its storage unit can be designated as a register using register. However, with continuous optimization of compiler technology, compilers can now automatically place frequently exchanged variables in registers, reducing the necessity of register declarations.

Note:

The keywords auto, static, and register must be used simultaneously during variable definition and cannot be used alone.

Therefore, the following usage is incorrect:

int a; (defining integer variable a first)

static a; (attempting to declare a as a static variable again)

This will be considered a “redefinition” by the compiler. The correct usage should be: static int a;

However, extern can be declared alone; it can extend the scope of an already defined variable, for example:

int b; (defined variable b in another program file or function)

extern b; (extends the scope of variable b here, allowing the omission of its int type)

The issue of variable definition and variable declaration. In practice, these two are often used interchangeably, which is not strict because there is a distinction between them.

Variable definition involves both the specification of variable characteristics and the allocation of storage units.

Variable declaration only indicates the characteristics of the variable without involving the allocation of storage units.

For example, the previously mentioned “int a;” is called variable definition, while “extern b;” is called variable declaration.

Overview of C51 Language

4. Global Variables and Local Variables

According to C51 rules, the position of the variable definition statement determines the variable’s scope, where those placed at the beginning of the program (i.e., before all functions) are called global variables, while those placed inside functions are called local variables.

The scope of global variables is the entire source program, and their values remain valid throughout program execution, while the values of local variables are only valid during function calls and will become invalid after the call ends.

To make reasonable use of storage resources, it is necessary to flexibly adopt global or local variables based on the situation, generally preferring local variables.

Overview of C51 Language

Global Technology Hotspots

1

White Crane Beach Hydropower Station’s 16 Units of One Million Kilowatt Generators Pass Acceptance

According to the Three Gorges Group, the last one million kilowatt turbine generator unit, unit 9 of the White Crane Beach Hydropower Station, has recently passed acceptance, and the production operation unit is now fully equipped to take over unit 9 and related equipment. Thus, all 16 units of one million kilowatt turbine generators independently developed and installed in the White Crane Beach Hydropower Station have passed acceptance.

The White Crane Beach Hydropower Station is located at the junction of Qiaojia County in Yunnan Province and Ningnan County in Sichuan Province along the main river section of the Jinsha River. It is the second of four giant hydropower stations built by the Three Gorges Group on the lower reaches of the Jinsha River – Wudongde, White Crane Beach, Xiluodu, and Xiangjiaba. The underground powerhouses on both banks of the White Crane Beach Hydropower Station are equipped with 16 units of the world’s largest single-unit capacity million kilowatt turbine generators, with a total installed capacity of 16 million kilowatts.

Overview of C51 Language

2

China’s Bohai Sea Achieves Billion-ton Oil and Gas Discovery

On March 1, China National Offshore Oil Corporation announced a new billion-ton discovery in the Bohai Sea – the Bozhong 26-6 oil field. This oil field is the largest metamorphic rock potential mountain oil field in the country and is also the first billion-ton oil field discovered in the Bohai Oil Field, which is China’s largest crude oil production base, discovered consecutively for three years, further solidifying China’s offshore oil and gas resource reserve foundation, which is of great significance for ensuring national energy security.

The Bozhong 26-6 oil field is located in the southern waters of the Bohai Sea, about 170 kilometers from Tianjin, with an average water depth of 22.1 meters, and the structure is situated between the two rich hydrocarbon depressions of the Yellow River Estuary and Bozhong depressions. The discovery well Bozhong 26-6-2 drilled through oil and gas layers totaling more than 320 meters, with a total drilling depth of 4480 meters, and tested an average daily oil production of over 270 tons and an average daily gas production of over 320,000 cubic meters, confirming that this oil field has large reserve scale, good oil quality, and high tested production capacity.

Overview of C51 Language

3

China’s Wind Power and Photovoltaic Installed Capacity Exceeds 700 Million Kilowatts

The total export value exceeded 51.2 billion USD, a year-on-year increase of 80.3% – in 2022, China’s photovoltaic industry performed well in the international market. After years of meticulous cultivation, China’s wind power and photovoltaic industry has significantly improved competitiveness, with key components such as photovoltaic modules, wind turbines, and gearboxes accounting for 70% of the global market share. Domestically, wind power and photovoltaic have become the main sources of new installed power generation and new power generation volume, accounting for 78% and over 55% respectively in 2022, with a cumulative installed capacity exceeding 700 million kilowatts, equivalent to over 30 Three Gorges Power Stations.

Overview of C51 Language
Overview of C51 Language

Written by | Lu Wanheng

Layout | Lai Hengyi

Reviewed by | Wang Jingyan, Zhang Ye

Guiding Teacher | Sun Pengbo

Overview of C51 Language

School of Instrument Science and Electrical Engineering

Official WeChat: JLU_CIEE

Submission Email: [email protected]

Official Weibo: Jilin University School of Instrument Science and Electrical Engineering

Overview of C51 Language

Leave a Comment