When using the update interrupt of the STM32 timer, it is found that in some cases, an interrupt is immediately entered as long as the timer is turned on. To be precise, as long as the update interrupt enable bit is enabled, an update interrupt is immediately responded to [of course, the premise is that the relevant NVIC has also been configured]. In other words, as long as the relevant timer update interrupt is enabled, no matter how long your timing interval is or even whether you start the relevant timer, it will immediately enter a timer update interrupt service routine.
Taking the STM32F051 chip as an example, several combinations of tests in different orders were conducted. According to the test, in some cases, once TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); is run, [even if the update interrupt is enabled], the update interrupt service routine is immediately entered. Of course, the subsequent interrupts are normal.
To be honest, this problem is easy to ignore and in some cases it is not important, but in some cases it may cause trouble to the application. It seems that it is not obvious to find a very suitable or logical cause and effect about this problem from the ST MCU related technical manual.
After verification and testing, it was found that this problem can be avoided if attention is paid to the order of relevant instruction codes.
First clear the update interrupt flag, that is, clear the UIF flag in the TIMx->SR register, and then enable the timer update interrupt. As for the placement of the instructions to start the relevant timers, it is not strict. The following is the operation sequence and results of the relevant actions, which can be referenced and verified. There are 6 ways of writing listed here, 3 of which will immediately enter the interrupt, and the other 3 will not.
TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //Clear update interrupt request bit
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //Enable timer 1 update interrupt
TIM_Cmd(TIM1, ENABLE); //Start the timer
(1) The update interrupt routine will not be entered immediately.
TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //Clear update interrupt request bit
TIM_Cmd(TIM1, ENABLE);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //Enable timer 1 update interrupt
(2) The update interrupt routine will not be entered immediately.
TIM_Cmd(TIM1, ENABLE);
TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //Clear update interrupt request bit
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //Enable timer 1 update interrupt
(3) The update interrupt procedure will not be entered immediately.
TIM_Cmd(TIM1, ENABLE);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //Enable timer 1 update interrupt
TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //Clear update interrupt request bit
(4) Immediately enter the update interrupt program.
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //Enable timer 1 update interrupt
TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //Clear update interrupt request bit
TIM_Cmd(TIM1, ENABLE);
(5) . . . . . . Enter the update interrupt procedure immediately.
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //Enable timer 1 update interrupt
TIM_Cmd(TIM1, ENABLE);
TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //Clear update interrupt request bit
(6) . . . . . . Enter the update interrupt procedure immediately.
By the way, let me mention the use of the UG bit and URS bit in the timer, which are in the TIMx->EGR and TIMx->CR1 registers respectively. Setting the UG bit to 1 can generate an update event and reinitialize the related counters and registers. If the URS bit is 0, an update interrupt will be generated at the same time. If you do not want to generate an update interrupt when setting the UG bit to 1, you must set the URS bit to 1, otherwise you will immediately enter the update interrupt.
Previous article:The value of the weak pull-up and weak pull-down resistors inside the STM32F103
Next article:Keil environment, mistakes made in debugging STM32
Recommended ReadingLatest update time:2024-11-16 16:37
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [Repost] Analysis of single-point grounding, multi-point grounding, floating grounding and mixed grounding in PCB design
- Duty cycle adjustment of sg3525 chip
- The calculation of constant voltage buck inductor is somewhat confusing
- PCB copper plating problem
- Several rules for power supply design of RF circuits
- Why does TI C6000 need cache?
- Switching power supply waveform
- [Raspberry Pi 4B Review] + Real-time status monitoring website test service resource usage
- The latency of cache in arm system refers to
- 【W806 Lianshengde Development Board】Real-time performance