In the previous section, Brother Hong mentioned that the delay() function takes too long and other tasks have no chance to execute. How can we improve this? This section teaches you how to solve this problem by accumulating the number of main loops. This section will teach you two knowledge points:
First point: Use the method of accumulating the number of main loops to achieve time delay
Second point: The initial experience of the switch core statement. All of Hongge’s practical projects are based on the switch statement to achieve multi-task parallel processing.
(1) Hardware platform: Based on Zhu Zhaoqi 51 single-chip microcomputer learning board.
(2) Implement function: make an LED flash.
(3) The source code is explained as follows:
#include "REG52.H"
/* Note 1:
* const_time_level is the upper limit of the statistical cycle times. The larger the value, the longer the LED delay time.
*/
#define const_time_level 10000
void initial_myself();
void initial_peripheral();
void delay_long(unsigned int uiDelaylong);
void led_flicker();
sbit led_dr=P3^5;
/* Note 2:
* Wu Jianhong's personal naming style: all step variables in the switch statement are suffixed with Step.
* The prefix uc, ui, or ul indicates that the variable is unsigned char, unsigned int, or unsigned long, respectively.
*/
unsigned char ucLedStep=0; //step variable
unsigned int uiTimeCnt=0; //Delay counter for counting the number of cycles
void main()
{
initial_myself();
delay_long(100);
initial_peripheral();
while(1)
{
led_flicker();
}
}
void led_flicker() ////The third zone LED flashing application
{
switch(ucLedStep)
{
case 0:
/* Comment 3:
* uiTimeCnt accumulates the number of loops. Only when its number is greater than or equal to the set upper limit const_time_level,
* will change the state of the LED light, otherwise the CPU exits the led_flicker() task and continues to quickly scan other tasks.
* Such a program structure can achieve the purpose of multi-tasking parallel processing.
* This program is based on Zhu Zhaoqi 51 single-chip computer learning board
*/
uiTimeCnt++; //Accumulate the number of cycles,
if(uiTimeCnt>=const_time_level) //Time is up
{
uiTimeCnt=0; //Time counter cleared
led_dr=1; //Make the LED light up
ucLedStep=1; //Switch to the next step
}
break;
case 1:
uiTimeCnt++; //Accumulate the number of cycles,
if(uiTimeCnt>=const_time_level) //Time is up
{
uiTimeCnt=0; //Time counter cleared
led_dr=0; //Turn off the LED
ucLedStep=0; //Return to the previous step
}
break;
}
}
void delay_long(unsigned int uiDelayLong)
{
unsigned int i;
unsigned int j;
for(i=0;i { for(j=0;j<500;j++) //Number of empty instructions in the embedded loop { ; //A semicolon is equivalent to executing an empty statement } } } void initial_myself() //Initialize the MCU in the first zone { led_dr=0; //LED off } void initial_peripheral() // Initialize the periphery of the second zone { ; //This example is empty } Closing remarks: In actual projects, it is a good choice to use the cumulative main loop times to achieve time delay. This method is competent for multi-tasking program frameworks, but it also has a small disadvantage. As the number of tasks in the main function increases, we need to constantly revise the upper limit const_time_level to ensure the accuracy of the delay time. How can we solve this problem? For more details, please listen to the next analysis - cumulative timer interrupt times make the LED light flash.
Previous article:Section 2: delay() delays the flashing of LED lights
Next article:Section 4: Accumulated timer interrupt times make the LED light flash
- 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
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Experiment with LIS25BA bone vibration sensor to collect audio
- 【micropython】Optimizing I2C initialization in STM32
- Power Supply Design Considerations
- Weekly review information is here~
- Learning Linux kernel semaphores on SinlinxA33 development board
- STM32 encoder signal interference.
- [GD32L233C-START Evaluation] +ADC collects PT1000 to obtain temperature
- Three traditional methods of wireless video transmission
- Displaying the SARS-CoV-2 virus with flashing LEDs
- How to improve simulation? Learn to generate LTspice models yourself