The STM32 timer sometimes interrupts as soon as it is turned on

Publisher:gamma13Latest update time:2017-09-30 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    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. 


 


Keywords:STM32 Reference address:The STM32 timer sometimes interrupts as soon as it is turned on

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

Summary of stm32 program download methods
Program downloading is the basis of everything. For those who are engaged in single-chip microcomputers, the first thing they learn is program downloading. When I first started learning 51 single-chip microcomputers, I only knew about serial port downloading, so I didn’t understand the role of USB-to-serial port chips
[Microcontroller]
List of DMA1 channels of stm32, related operations of stm32 using DMA
DMA (Direct Memory Access) is often translated as "direct memory access". DMA applications have been available as early as Intel's 8086 platform. A complete microcontroller usually consists of components such as CPU, memory and peripherals. These components are generally independent in structure and function, and th
[Microcontroller]
List of DMA1 channels of stm32, related operations of stm32 using DMA
SYSTICK_Init() configuration for STM32
void   SYSTICK_Init(void) {    /* SysTick end of count event each 1ms with input clock equal to 4.5MHz (HCLK/8, default)       SysTick_SetReload(4500);    /* Enable SysTick interrupt     SysTick_ITConfig(ENABLE);    /* Enable the SysTick Counter       SysTick_CounterCmd(SysTick_Counter_Enable); }  The peri
[Microcontroller]
STM32 - GPIO input
The lab that lights up the LED lamp uses the GPIO output configuration to achieve this. Next, write a program that uses GPIO as input to light up and turn off the LED lamp at the same time. Since the program is simple, the code is directly posted for reference: #include "stm32f10x.h" /**   * @brief Initialize GPIO,
[Microcontroller]
How to debug STM32 in ram and flash under ulink-Jlink
Keil MDK3.20 debug stm32 method under ULINK 1. Programs run in RAM Key points: (1) Change the download address of the program to the RAM space (2) Before debugging the program, set the SP and PC pointers to the Ram space. Create a new project and select the specific model of STM32. I bought Wanli
[Microcontroller]
How to debug STM32 in ram and flash under ulink-Jlink
The principle and function of crystal oscillator in STM32
A crystal oscillator can be electrically equivalent to a two-terminal network consisting of a capacitor and a resistor in parallel and a capacitor in series. In electrical engineering, this network has two resonant points, with the lower frequency being the series resonance and the higher frequency being the parallel
[Microcontroller]
The principle and function of crystal oscillator in STM32
FreeRTOS interrupt settings based on STM32 applications
1. Introduction to FreeRTOS interrupt settings Two macros are defined in FreeRTOSConfig.h: configKERNEL_INTERRUPT_PRIORITY configMAX_SYSCALL_INTERRUPT_PRIORITY        ​configKERNEL_INTERRUPT_PRIORITY is used to set the interrupt priority of the RTOS kernel. Because the RTOS kernel interrupt is not allowed to p
[Microcontroller]
FreeRTOS interrupt settings based on STM32 applications
Selection of reference source
tl431 lm285    385 or the like, 1.2V The AD reference voltage requirement of STM32 is >2.4V   REF2930-3V costs more than 20 yuan, which is too high. Chaliyuan only quoted a few yuan. Is there a problem with TI's supply? Since 3.0V references are rare and hard to find, I want to use 2.5V references. For 3V voltag
[Microcontroller]
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号