Section 3: Accumulate the number of main cycles to make the LED light flash

Publisher:九九归一74Latest update time:2016-03-14 Source: eefocusKeywords:Total Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Opening remarks:

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.

Keywords:Total Reference address:Section 3: Accumulate the number of main cycles to 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

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号