Misconceptions about software delays

Publisher:uioo9158Latest update time:2016-08-15 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Usually when learning MCU programming in college, teachers teach us to write program flow, and the flow is almost a long line with occasional branch judgments. The program written in this way is clear and easy to understand, but there are usually many problems. For example, if I suddenly want to add something, I find that the system is not working properly.

Often these problems are caused by too much delay added to the entire process. The delay program is a relatively straightforward program, but it also destroys the overall performance of the microcontroller because the delay microcontroller is running at full speed.

What comes to my mind now is the process I wrote before: initialization->display->button->other functions->end.

1. For example, the naked eye cannot distinguish above 50HZ, so the average display time for each display is 5ms when using 4 digital tubes for dynamic display. According to the previous view, it is written like this: the first digital tube lights up -> delay for a few milliseconds, turn off the first one, light up the second one ->... -> delay, turn off the last one, light up the first one. If 8 digital tubes are used for dynamic display, wouldn't there be a delay of 8 * a few milliseconds?

2. There is another button, and the button needs to have a jitter removal function, usually with software delay of about 20ms. If you add the tens of milliseconds of display, the delay is quite terrifying.

3. Other functions also have delays, especially drivers. For example, the MAX7279 read operation requires a certain delay, but this delay is at the us level, which can be ignored compared to the ms level.

The author suggests that when writing a program, especially a delay program, you should consider how long your delay will be. If it is at the ms level, consider using a timer interrupt to handle it.

For example, in the display program, if 8 digital tubes are displayed dynamically, then you have to consider that the 8 digital tubes are refreshed once every 20ms, which means that the display time of each digital tube is 2.5ms.

Then the procedure is as follows:

volatile unsigned char time_2500_flag; //2.5ms flag

void TimerA(void) //Timer initialization

{

       // Initialization, configure to interrupt once every 500us

}

#pragma interrupt_handler TimerA_ISR:NUM
void Timer1_ISR(void) //Interrupt service routine

{

     static unsigned char cnt = 0;

        //reload reassignment

       if (++cnt > 5)

      {

              cnt = 0;

             time_2500_flag = 1;

       } 

}

 

void Display(void)

{

      ;

}

 

void main(void)

{

        //initialization

        for (;;)

      {

               if (time_2500_flag == 1)

              {

                     time_2500_flag = 0;

                    Display();

              }

      }

}

 

The program is written here.

The display program needs special processing here. Execute the display program once and the digital tubes will light up one by one.

Reference address:Misconceptions about software delays

Previous article:MSP430G2553 test program (serial port program, USCI mode)
Next article:MCU 4-button menu

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号