STM32 learning: discussion on timer programming

Publisher:平和思绪Latest update time:2018-10-21 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Assume that timer 3 is used to time every 1 millisecond; the function saved to the SD card is StartSave();
Case 1: timer is fast, main loop is slow
1. Code design 1 (wrong design)
[cpp] view plain copy
int cnt = 0; //Counting  
//TIM3 interrupt handling function  
void TIM3_IRQHandler(void)  
{  
    if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)   
    {  
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update );    
        cnt ++;     
    }  
}  
void main(void)  
{  
   Code segment 1  
   while(1)  
   {  
     Code segment 2  
     if(cnt %100 == 0)  
     {  
    StartSave();  
     }  
     Code segment 3  
   }  
}  
Analysis: After testing, it was found that the first design was not saved at the expected 100 millisecond intervals; what is the reason?
It is obvious that the background program runs faster. When cnt becomes a multiple of 100, the main loop may reach "Code Segment 3". When the main loop reaches
"Code Segment 2" again, the timer interrupt has changed the value of cnt.


2. Code Design 2 (correct in this case)
[cpp] view plain copy
int cnt = 0; //Count  
unsigned char isOK = 0;  
void TIM3_IRQHandler(void)  
{  
    if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)   
    {  
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update );    
        if(cnt++ % 100 == 0)  
             isOK = 1;  
    }  
}  
void main(void)  
{  
   Code Segment 1  
   while(1)  
   {  
     Code Segment 2  
     if(isOK == 1)  
     {  
    isOK = 0;  
        StartSave();  
     }  
     Code Segment 3  
   }  
}  
Design 2 avoids the problems in 1.
The second case: the timer is slow and the main loop is fast.
In this case, the code design 2 above has problems. Too many values ​​are saved.
The reason is obvious. The change of the isOK variable is too slow compared to the main loop. IsOK will always be 1.
Assume that the timer is set to 1ms and the main loop has a period of 0.5ms.
1. Code Design 1 (wrong design)
[cpp] view plain copy
void TIM3_IRQHandler(void)  
{  
    if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)   
    {  
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update );    
        cnt++;//The theoretical holding time of this value is only 1ms  
    }  
}  
void main(void)  
{  
   unsigned char saveFin = 0;  
   Code Segment 1  
   while(1)  
   {  
     Code Segment 2  
     if(cnt%100 )  
     {  
        StartSave();//It is obvious that multiple saves occur within 100ms because the main loop is fast  
     }  
     Code Segment 3  
   }  
}  
2. Code Design 2 (correct design)
[cpp] view plain copy
unsigned char saveFin = 0;  
void TIM3_IRQHandler(void)  
{  
    if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)   
    {  
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update );    
        cnt++; //The theoretical holding time of this value is only 1ms  
    }  
}  
void main(void)  
{  
   unsigned char saveFin = 0;  
   Code segment 1  
   while(1)  
   {  
     Code segment 2  
     if(cnt%100 == 0 && saveFin == 0 )  
     {  
        saveFin = 1;  
        StartSave();  
     }  
     else  
     {  
        saveFin = 0;   
     }  
     Code segment 3  
   }  
}  
The third case: It is not certain which of the timer and the main loop cycle is faster.
Of course, the main loop cycle cannot be greater than the save cycle of 100ms
[cpp] view plain copy
unsigned char isOK = 0;  
unsigned int clkCnt = 0;  
void TIM3_IRQHandler(void)  
{  
    if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)   
    {  
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update );   
        if(clkCnt++ % 10 == 0)  
             cnt++; //Theoretically, this value is maintained for 10ms and needs to be cleared in the main loop.  
    }  
}  
void main(void)  
{  
   unsigned char saveFin = 1; //Note that the initial value here is 1, which is different from the previous design.  
   Code segment 1  
   while(1) //The loop period cannot be greater than 10ms, otherwise the judgment of cnt will be lost.  
   {  
     Code segment 2  
     if(cnt%10 == 0) //For example, clear the "save flag" between 100ms--110ms.  
     {  
    saveFin = 0;   
     }  
     else //For example, complete the save between 110ms--200ms.  
     {  
        if(saveFin == 0) //Code design that can only save once between 110ms--200ms.  
        {  
           startSave();  
           saveFin = 1;  
        }  
     }  
     Code segment 3  
   }  
}

Keywords:STM32 Reference address:STM32 learning: discussion on timer programming

Previous article:STM32 Learning: Event Flag Group
Next article:STM32 Learning: Context-M3 Introduction

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号