51 MCU-Function Encapsulation

Publisher:devilcoreLatest update time:2021-08-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Clean code

Generally, the reading of single-chip microcomputer programs starts with the main function. The program writing method in the previous lecture is not concise in the main function, which affects the efficiency of reading the code. Sometimes we only need to know what a statement means, so we encapsulate the delay part into a function, and call the function name in the main function to indicate that the statement here is delayed for 1 second. This is much more readable and concise. Readers can also first understand Section 4.6 in the document "Teaching You to Learn 51 Single-chip Microcomputers Step by Step".

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

twenty one

twenty two

twenty three

twenty four

25

26

27

28

29

30

31

32

33

34

#include  

sbit LED2 = P0^0;

sbit ADDR2 = P1^2;

sbit ADDR1 = P1^1;

sbit ADDR0 = P1^0;

sbit ENLED = P1^4;

sbit ADDR3 = P1^3;

 

void delay()//The delay function is encapsulated as a function

{

    unsigned int i,j; //define two variables to complete the delay function

    for(i=0;i<19601;i++)//delay 1s

    {

        for(j=5;j>0;j--);

    }

}

 

void main()

{  

    ADDR3 = 1; // Enable 38 decoder

    ENLED = 0; // 

 

    ADDR2 = 1; //****************************

    ADDR1 = 1; //Let IO6 of the 38 decoder output low level

    ADDR0 = 0; //****************************

 

    while (1)

    {

        LED2=0; //light up the rightmost light

        delay(); //delay 1s  

        LED2=1; //Turn off the rightmost light

        delay(); //delay 1s

    }

}

The principle is very simple. We can use a diagram to show what function encapsulation is.

2.10.png

Here, the variables i and j are simply defined in "void delay()", and the functions implemented are the same as the code in the previous lecture. However, the author found through software simulation that this will take up 4 microseconds of the CPU calling the function. For the convenience of reading, it is necessary to sacrifice these 4 microseconds. However, we do not need to add statements such as "LED2=0;" or "i=0;" at the end to solve the strange phenomenon mentioned in the previous lecture that the light jump and the stopwatch timing digital jump are not synchronized.


We recommend using function encapsulation for modular programming, because this is more in line with programming ideas and is also a programming technique we must use.

 

2. Suggestions

Although there is not much code in this section, we should develop the habit of reading the code from the main function instead of reading the code from the beginning. For example, the first thing that catches our eyes is the "void delay()" function. At this time, we should not enter the function body to read the content inside, but immediately find "void main()" to read and study the code inside. When we see "delay();", we have a first impression of this function with the comment, and then we can check the content of this function.


The common steps are as follows:

2.11.png

2.12.png

You must press F12 after compiling. Although the advantage of this method is not obvious when the amount of code is small, when we read other people's code, the amount of code is generally large, and this method can quickly understand the layers of meaning of the program written by others. Regarding this habit, everyone will understand it after learning the following programs.

Reference address:51 MCU-Function Encapsulation

Previous article:51 MCU-Delay 1 second
Next article:51 MCU-function parameter calling

Recommended ReadingLatest update time:2024-11-15 07:54

What is the use of the program status register PSW of the MCS-51 microcontroller?
PSW is an 8-bit register used to set the CPU status and indicate the status after instruction execution. CY (PSW.7): ​​Carry flag. When executing addition or subtraction instructions, if the highest bit (D7) of the operation result has a carry or borrow, CY is automatically set to 1 by hardware. AC (PSW.6): Half c
[Microcontroller]
What is the use of the program status register PSW of the MCS-51 microcontroller?
51 MCU simulated serial port source program
MCU simulated serial port experiment. If you want to use the serial port function on a MCU without a serial port, you need to simulate a serial port. The microcontroller source program is as follows: #include "reg51.h" typedef unsigned char BYTE; typedef unsigned WORD; typedef bit BOOL; #define BAUD  0xFE80       
[Microcontroller]
51 MCU C language learning notes 1: MCS-51 system structure
                51 MCU Pin Diagram 51 MCU System Structure   The traditional 51 MCU is DIP40 (dual in-line 40 pins), and there are also PLCC44, which are mostly used in places where the board requires compactness. If you want to occupy less space, you can use 2051, which has only 20 pins. Of course, the enha
[Microcontroller]
STC C51 MCU EEPROM
   1.   STC series microcontrollers all have built-in EEPROM units, and the operation mode of the EEPROM unit is similar to that of NOR FLASH, with the Sector unit for erasing and writing. At the same time, when rewriting the Flash data, the internal EEPROM data is not affected. This is different from the EEPROM operat
[Microcontroller]
51 MCU button controls LED water light mode
1. Copy Delay.ch from the previous file and add it 2. Modular Programming 3.main.c #include REGX52.H #include "Timer0.h" #include "Key.h" #include INTRINS.H   unsigned char KeyNum,LEDMode; void main() { P2=0xFE; Timer0Init(); while(1) { KeyNum=Key();       if(KeyNum==1) { LEDMode++; if(LEDMode
[Microcontroller]
51 MCU button controls LED water light mode
51 MCU - I2C bus driver
To facilitate transplantation, a multi-file project approach is adopted. void Delay10us()//Delay 10us { unsigned char a,b; for(b=1;b 0;b--) for(a=2;a 0;a--);   } Start signal: When the SCL clock signal is at a high level, the SDA signal generates a falling edge. void I2cStart() //To facilitate the connection wi
[Microcontroller]
51 MCU-External interrupt pulse count
1. Common functions of external interrupts External interrupts are often used to record the number of external pulses. That is, when we use external interrupt 1, if we want to record the number of falling edges of a pin of our own microcontroller, we only need to connect this pin to P3.3 with a DuPont line. For exa
[Microcontroller]
51 MCU-External interrupt pulse count
51 MCU data operation and instruction types
The MCS-51 single-chip microcomputer instruction system includes 111 instructions, which can be divided into the following five categories according to their functions. Data transfer instructions (29 items) Arithmetic operation instructions (24) Logical operation instructions (24) Control transfer instructions
[Microcontroller]
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号