Microcontroller beginner's guide (14) Program analysis for lighting the first LED

Publisher:DelightfulSmileLatest update time:2016-05-23 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
In this article, we will analyze the program code written in the previous article. In the future, we will focus most of our energy on learning microcontrollers on writing program codes. But don't worry, I will introduce the writing ideas and various precautions of each program in great detail.

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.

================================================== =====

Keywords:MCU Reference address:Microcontroller beginner's guide (14) Program analysis for lighting the first LED

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

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号