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
}
}
Previous article:STM32 Learning: Event Flag Group
Next article:STM32 Learning: Context-M3 Introduction
- Popular Resources
- Popular amplifiers
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
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Analog Electronics Popular Data Download Collection
- Terminal Software XShell Manual
- Ultra-long CIS image acquisition system based on FPGA.pdf
- EEWORLD University Hall ---- Introduction to Internet of Things Engineering by Wang Zhiliang, University of Science and Technology Beijing
- GaN Hardware and Software Co-design & RF Device Architecture
- What parameters should be considered when selecting a voltage regulator?
- New version of the micro http server MicroWebSrv2
- Quectel EC20 module power supply problem
- Thank you for being there + the extraordinary me in 2019
- Macro base stations, distributed base stations, small base stations