delay() delay to realize the flashing of LED lights -- learning notes of "teach you the program framework of single chip microcomputer by hand - Wu Jianhong"

Posted by kjelle392 on Mon, 11 Oct 2021 01:01:17 +0200

Disclaimer: This article is my notes on learning "teach you the program framework of single chip microcomputer hand in hand - Wu Jianhong"
Original link: Section II: delay to realize the flashing of LED lights.

original text

Section II: delay to realize the flashing of LED lights.
Opening remarks:
In the last section, brother Hong listed seven misunderstandings for beginners. What is the core of beginners' attention? That is the program structure of bare metal running. A good program structure itself is a micro multitasking operating system. What brother Hong taught you is how to write this simple operating system. In the main function loop, the switch statement is used to realize the task switching of multi task parallel processing, plus a timer interrupt. The combination of the two is the core of all practical projects of Hongge for many years. Hongge's program structure seems simple, but it's actually that simple. Don't worry. This serial article has just officially started. In this section, I want to teach you two knowledge points:
First point: the theory of "three areas and one line" put forward by Hongge for the first time. This theory divides the program code into three areas, a delay divider.
Second point: the purpose of delay().
(1) Hardware platform: Based on Zhu Zhaoqi 51 single chip microcomputer learning board.
(2) Function: let an LED flash.
(3) The source code is explained as follows:

#include "REG52.H"

void initial_myself();    
void initial_peripheral();

void delay_short(unsigned int uiDelayshort);
void delay_long(unsigned int uiDelaylong);
void led_flicker();

/* Note 1:
* Wu Jianhong's personal naming style: all output suffixes are_ dr, all input suffixes are_ sr. 
* dr Represents drive and sr represents sensor
*/
sbit led_dr=P3^5;  

void main()  //Learning points: deeply understand the three areas and one line theory proposed by brother Hong for the first time
{
/* Note 2:
* initial_myself()The function belongs to the first zone of Hongge's three zone one line theory,
* It is specially used to initialize the MCU's own registers and individual peripheral output devices with fast response speed,
* Prevent the peripheral equipment from misoperation due to the uncertain level state of the output IO port just after power on,
* Such as misoperation of relay, etc. 
*/
   initial_myself();

/* Note 3:
* Delay here_ The long() delay function belongs to the division line between the first zone and the second zone,
* The delay time is generally between 0.3 seconds and 2 seconds, waiting for the peripheral chip and module to power on stably.
* For example, LCD module, AT24C02 memory chip, DS1302 clock chip,
* This kind of chip has a characteristic. Generally, it communicates with single chip microcomputer through serial port or parallel port,
* And do not require immediate treatment after power on.
*/
  delay_long(100);

/* Note 4:
* initial_peripheral()The function belongs to the second zone of Hongge's three zone one line theory,
* It is specially used to initialize peripheral chips and modules that do not require immediate processing after power on
* For example, LCD module, AT24C02 memory chip and DS1302 clock chip.
* This program is based on Zhu Zhaoqi 51 single chip microcomputer learning board.
*/
  initial_peripheral();

/* Note 5:
* while(1){}The cyclic region of the main function belongs to the third region of Hongge's three region one line theory,
* Designed to write non disruptive applications that are scanned circularly
*/
  while(1)
  {
     led_flicker();   //LED flashing application
  }

}

void led_flicker() //LED flashing application
{
 led_dr=1;  //LED on
 delay_short(50000);  //Time to delay 50000 empty instructions

/* Note 6:
* delay_long(100)Delay 50000 empty instructions because a 500 for loop is embedded
*/
 led_dr=0;  //LED off
 delay_long(100);    //Time to delay 50000 empty instructions  
}


/* Note 7:
* delay_short(unsigned int uiDelayShort)Is a small delay function,
* It is specially used for small delay of timing drive. Generally, the value of uiDelayShort is about 10,
* The maximum value is generally no more than 100. In this example, in order to explain the characteristics of this function, the value range exceeds 100.
* This function is characterized by high subdivision of time, and the delay time should not be too long. uiDelayShort value
* The size of represents how many empty instructions have been executed. The larger the value, the longer the delay.
* Don't deliberately calculate the time accuracy. It feels almost OK.
*/
void delay_short(unsigned int uiDelayShort) 
{
  unsigned int i;  
  for(i=0;i<uiDelayShort;i++)
  {
    ;   //A semicolon is equivalent to executing an empty statement
  }
}


/* Note 8:
* delay_long(unsigned int uiDelayLong)Is a large delay function,
* It is specially used for large delay of power on initialization,
* The feature of this function is that it can achieve a relatively long delay. The subdivision degree depends on the number of embedded for loops,
* uiDelayLong The value of represents how many times 500 empty instructions have been executed.
* The larger the value, the longer the delay. Don't deliberately calculate the time accuracy. It feels almost OK.
*/
void delay_long(unsigned int uiDelayLong)
{
  unsigned int i;
  unsigned int j;
  for(i=0;i<uiDelayLong;i++)
  {
     for(j=0;j<500;j++)  //Number of empty instructions in an embedded loop
     {
        ; //A semicolon is equivalent to executing an empty statement
     }
  }
}



void initial_myself()  //Initialize MCU
{
 led_dr=0;  //LED off
}
void initial_peripheral() //Initialize periphery
{
 ;   //This example is empty
}

Concluding remarks:
The theory of "three areas and one line" put forward by Hongge for the first time outlines the basic zoning of various project procedures. My subsequent programs are written according to this partition.
The long delay of the Delay() function is applicable to power on initialization.
The short delay of the Delay() function is applicable to the pulse delay of the driving sequence. At this time, the time cannot be too long. There are no examples in this example, which will be mentioned in later chapters.
In the source code of this example, LED_ The two delay delays used in the flicker () flash application are too long. They can't be used in actual combat projects because the time consumed is too long and other tasks have no chance to execute.
Then what shall I do? How should we improve? For more information, please listen to the next step - the cumulative number of main cycles causes the LED to flash.

(to be continued, the next section is more exciting. Don't go away)

experiment

Through the experiment of LED flashing, Mr. Wu explained his "three zones and one line" theory to us, and introduced the precautions when using while delay. There are no difficulties in the code function, so I directly simulated Mr. Wu's code with Proteus:

Topics: Single-Chip Microcomputer