MCU Getting Started Guide Series (14) program analysis for lighting the first LED

Source: Internet
Author: User

In this article, we will analyze the program code written in the previous article. In the future, we will focus most of our efforts on programming code. But don't worry. I will introduce every program's writing ideas and various precautions in detail.

The program we wrote earlier is as follows:

# Include <reg52.h>

Sbit LED = P1 ^ 0;

Void main ()

{

LED = 0;

While (1 );

}

Header file

The first line contains the header file, which is the same as # include when HelloWorld is written in C. The header file reg52.h defines some registers of the microcontroller, such as P0 ~ P3. As for what is a register, we will introduce it in principle. Now we just need to think of it as the memory that is often said in C language. If you are interested, you can right-click reg52.h to open the header file.

Variable type of C51

The language we use here is C51. Compared with the standard C language, the most important feature is the addition of two variable types: sfr and sbit. Like char, sfr is an 8-bit data type, indicating an 8-bit register. For example, the header file reg52.h contains an sfr P1 = 0x90, which defines the register P1. Since P1 is an IO port register, if P1 is assigned a value, IO port P1 outputs the corresponding level. Sbit is a one-bit data type with only 0 and 1 values. It corresponds to one bit in the register.

So in the second row, we defined the sbit variable LED and set the leading bit bit0 of the LED corresponding to P1. In this case, the variable LED is associated with the IO port P1.0. Execute the value assignment statement LED = 0, the IO port P1.0 will output a low level, so the LED in our circuit will be on. Is it amazing? In principle, I will explain step by step how the single-chip microcomputer implements this function.

Bit addressing

Among many other single-chip computers, we can only read and write the entire P1 register, rather than directly defining a data type similar to sbit that corresponds to one register, in addition, some registers in 51 single-chip microcomputer can only read and write the entire 8-bit. This feature of directly reading and writing a register bit is called the bit addressing function.

It is a difficult concept to understand. Similarly, you still need to understand the principles to understand it well. Now you don't have to worry about it. I mentioned this to give beginners a general impression on it. After all, a lot of knowledge is accumulated slowly. If we put a lot of such small knowledge in the principle article and introduce it, it would be unacceptable.

Main function features: no parameters or returned values

The reader should have seen void main () functions without parameters and without return values, as well as the while (1) statement in the last and last rows, which may be confusing. Yes, the main function has no parameters and no return value. The last and second rows of this sentence are an endless loop, and the program will never exit. This is very different from the program on the computer. The following is a brief description of the reasons for doing so. For more in-depth reasons, you need to read the principles before you can fully understand them.

On a computer, the startup interface we see during boot is an operating system, such as Windows and Linux. The programs we write run in the operating system environment. When the program is executed, the system calls the main function. Therefore, the system can pass parameters to the main function, or obtain the return values at the end of the main function execution. After the program is executed, will return to the operating system environment. Here, the program we write runs directly on the single-chip microcomputer and does not rely on the operating system. The main function in our program will not be called by the operating system. Therefore, it usually does not need parameters or return values. In fact, the main function is called by the startup code in some cases, and the startup code is written in assembly language. Do you still remember the startup code mentioned in the previous article?

The main function features an endless loop that does not end with execution.

On the computer, after the program is executed, it will return to the operating system environment. In single-chip microcomputer, once the single-chip microcomputer is powered on and reset, it will execute the main function we wrote until the power is down. If the execution of the main function ends, unpredictable results will occur.

In actual experiments, the STC microcontroller resets and re-executes the program. It may be a coincidence that this is not explicitly stated in the official chip manual. With a rigorous scientific attitude, we do not use this unofficially confirmed feature to develop our programs. Program reliability is very important, which should be paid attention.

Imagine if you use a single-chip microcomputer to develop medical-related products or control some large machines, once an accident occurs, the consequences are hard to imagine. Even if you are developing a common product, if there is always a strange fault, it is enough to drive users crazy. Therefore, this should be noticed by readers. Therefore, the single-chip microcomputer program should not be completed, so the end must be an endless loop, in order to ensure the reliability of the single-chip microcomputer system.

Summary

So we sum up the following features of the 51 single-chip microcomputer program:

1. Contains REG52.H, which defines information such as P0-P3 interfaces (sfr and sbit ).

2. The single-chip microcomputer main function has no parameters and no return value (of course, the int type will not report an error, but it is not necessary ).

3. There will be an endless loop at the end of the MCU main function and it will not exit.


Self-published personal website, reprinted please note Source: http://www.hainter.com/mcu-primer-14

MCU Getting Started Guide Series (14) program analysis for lighting the first LED

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.