The program we wrote before is as follows:
#include
sbit LED = P1^0;
void main()
{
LED = 0;
while(1);
}
head File
The first line contains the header file, which is the same as #include when writing HelloWorld in C language. The header file reg52.h defines some registers of the microcontroller, such as P0~P3, etc. As for what registers are, we will introduce them in detail in the principle section. Now we just need to think of it as the memory commonly mentioned in C language. Interested readers can right-click reg52.h to open the header file and take a look.
C51 variable types
The language we use here is C51. Compared with the standard C language, the most important feature is that it has two more variable types: sfr and sbit. Sfr is an 8-bit data type like char, representing an 8-bit register. For example, there is a sentence sfr P1 = 0x90 in the header file reg52.h, which defines register P1. Since P1 is an IO port register, if P1 is assigned a value, then IO port P1 will output the corresponding high and low levels. Sbit is a 1-bit data type with only two values, 0 and 1, and it corresponds to a bit in the register.
So in the second line, we define the sbit variable LED and set LED to the lowest bit 0 of P1. At this time, the variable LED is associated with IO port P1.0. When the assignment statement LED = 0 is executed, IO port P1.0 will output a low level, and the LED in our circuit will light up. Isn't it amazing? In the principle section, I will explain step by step how the microcontroller implements this function.
Bit addressing
In many other MCUs, we can only read and write the entire P1 register, and cannot directly define data types such as sbit that correspond to one bit of the register. In addition, some registers in the 51 MCU can only be read and written as a whole 8 bits. This feature of directly reading and writing a register bit is called bit addressing.
This is another difficult concept to understand. Again, you still need to understand the principle section to understand it well. Don't worry about it now. I mention this just to give beginners a general impression of it in advance. After all, a lot of knowledge is accumulated slowly. If a lot of such small knowledge is introduced together in the principle section, it will be difficult to accept.
Characteristics of the main function: no parameters and return values
The reader should have seen the void main() function with no parameters and no return value, as well as the while(1) statement on the second-to-last line, and may be confused. That's right, the main function has no parameters and no return value, and the second-to-last line is an infinite loop, and the program will never exit. This is very different from programs on computers. The following is a brief explanation of the reason for doing this. For a more in-depth understanding, readers need to read the principle section to fully understand it.
In a computer, the startup interface we see when we turn on the computer is the operating system, such as Windows, Linux, etc. The programs we write run in the operating system environment. When the program is executed, it is equivalent to the system calling the main function, so the system can pass parameters to the main function, and can also obtain the return value when the main function is executed. After the program is executed, it will return to the operating system environment. Here, the program we write runs directly on the microcontroller and does not rely on the operating system. The main function in our program will not be called by the operating system, so it usually does not require parameters or return values. In fact, in some cases, the main function is called by the startup code, and the startup code is written in assembly language. Do you remember the startup code mentioned in the previous article?
Characteristics of the main function: infinite loop, will not be executed to the end
In a computer, when the program we write is executed, it will return to the operating system environment. In a microcontroller, once the microcontroller is powered on and reset, the main function we wrote will be executed until the power is turned off. If the main function is executed, unpredictable results will occur.
In actual experiments, the STC microcontroller will reset and restart the program, which may be just a coincidence, because the official chip manual does not clearly state this. In line with a rigorous scientific attitude, we do not use this unofficial feature to develop our program. The reliability of the program is very important and this should be taken seriously.
Imagine if you use a microcontroller to develop medical-related products, or control some large machines, etc., once an accident occurs, the consequences are unimaginable. Even if you develop an ordinary product, if strange failures always occur, it is enough to drive users crazy. Therefore, this point should attract the attention of readers. Therefore, the program of the microcontroller should not be executed to the end, so it must be an infinite loop in the end, so as to ensure the reliability of the microcontroller system.
Summarize
So we summarize several characteristics of 51 MCU programs under normal circumstances:
1. Include REG52.H, which defines the P0-P3 interface information (sfr type and sbit type).
2. The main function of the microcontroller has no parameters and no return value (of course, it will not report an error if it is written as an int type, but it is not necessary).
3. There will be an infinite loop at the end of the main function of the microcontroller and it will not exit.
================================================== =====
Previous article:Microcontroller beginner's guide (13) Lighting up the first LED - a good start is half the battle
Next article:Microcontroller beginner's guide (15) Summary of microcontroller program download failure
- 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
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- Testing methods and diagnostic analysis for DSP-containing circuit boards
- Fundamentals of Circuits and Analog Electronics Technology
- Need a low current charging IC and a Bluetooth chip with integrated MCU
- PCB Layout Guidelines
- LAUNCHXL-F28379D comprehensive routine
- 【DWIN Serial Port Screen】One of the Nucleic Acid Sampling and Registration Systems
- Live broadcast at 10 am today [TI's new generation MSP430 helps low-cost ultrasonic water meter]
- Playing with Zynq Serial 24 - Porting of User-Defined IP Cores
- [Only in Hangzhou, register now!] 12/21 (Friday) WPI Group/ON Semiconductor Industrial Internet of Things (IIoT) Application...
- 【Qinheng CH582】Structural analysis of Bluetooth protocol stack