STM32F103 study notes (VII) Timer interrupt (update interrupt)

Publisher:草木知秋Latest update time:2017-09-24 Source: eefocusKeywords:STM32F103 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The timer function of STM32 is very powerful, including TIME1 and TIME8 advanced timers, TIME2~TIME5 general timers, TIME6 and
TIME7 basic timers.

    The general TIMx (TIM2, TIM3, TIM4 and TIM5) timer functions of STM32 include:
1) 16-bit up, down, up/down auto-load counter (TIMx_CNT).
2) 16-bit programmable (can be modified in real time) prescaler (TIMx_PSC), the counter clock frequency division factor is
any value between 1 and 65535.
3) 4 independent channels (TIMx_CH1~4), these channels can be used as:
A. Input capture
B. Output compare
C. PWM generation (edge ​​or center alignment mode)
D. Single pulse mode output
4) External signal (TIMx_ETR) can be used to control the synchronization circuit of timer and timer interconnection (one timer can be used to control another
timer).
5) Interrupt/DMA is generated when the following events occur:
A. Update: counter overflow/underflow, counter initialization (by software or internal/external trigger)
B. Trigger event (counter starts, stops, initializes or counts by internal/external trigger)
C. Input capture
D. Output comparison
E. Support for incremental (quadrature) encoder and Hall sensor circuits for positioning
F. Trigger input as external clock or cycle-by-cycle current management

Next, the time.c file needs to be written:


  1. #include "timer.h"  

  2. #include "led.h"  

  3. //General timer 3 interrupt initialization  

  4. //The clock here is selected to be twice that of APB1, and APB1 is 36M  

  5. //arr: automatically reload value.  

  6. //psc: clock pre-division number  

  7. //Timer 3 is used here!  

  8. void TIM3_Int_Init(u16 arr,u16 psc)  

  9. {  

  10.  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  

  11. NVIC_InitTypeDef NVIC_InitStructure;  

  12. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //①Clock TIM3 enable  

  13. //Timer TIM3 initialization  

  14. TIM_TimeBaseStructure.TIM_Period = arr; //Set the value of the automatic reload register period  

  15. TIM_TimeBaseStructure.TIM_Prescaler =psc; //Set the prescaler value of the clock frequency divisor  

  16. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Set clock division  

  17. 199  

  18. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM counts up  

  19. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //②Initialize TIM3  

  20. TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //③Enable update interrupt  

  21. //Interrupt priority NVIC setting  

  22. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3 interrupt  

  23. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preemption priority level 0  

  24. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //From priority level 3  

  25. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel is enabled  

  26. NVIC_Init(&NVIC_InitStructure); //④Initialize NVIC registers  

  27. TIM_Cmd(TIM3, ENABLE); //⑤Enable TIM3  

  28. }  

  29. //Timer 3 interrupt service routine⑥  

  30. void TIM3_IRQHandler(void) //TIM3 interrupt  

  31. {  

  32. if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) // Check whether TIM3 update interrupt occurs  

  33. {  

  34. TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //Clear TIM3 update interrupt flag  

  35. LED1=!LED1;  

  36. }  

  37. }  


I would like to emphasize here that when I was doing the experiment, the program did not report an error, but after burning it, DS0 and DS1 were always off, so I compared the original program to find the error one by one, and finally found that the interrupt service function was written incorrectly, IRQHandler was written as IRQHanlder. At first I was very puzzled, isn’t the interrupt service function name defined by myself, and there is a problem if I write a wrong letter? In fact, it is not the case. I misunderstood how the interrupt function works from the beginning. . .


The interrupt must be initialized first, and then enabled, which is this sentence:


  1. TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); // Enable update interrupt  

After enabling, it will automatically find and enter the interrupt service function, which means that the interrupt service function has been defined internally. After I wrote it wrong, the program has entered the interrupt service function, but it is stuck inside and can't get out, causing DS1 and DS0 in the main function to not light up... (Comment out the interrupt enable, the interrupt service function of my mistake remains unchanged, and DS0 in the main function lights up after burning)



Next is the main function:

  1. int main(void)  

  2. {  

  3. delay_init(); //delay function initialization   

  4. NVIC_Configuration(); //Set NVIC interrupt group 2: 2-bit preemption priority, 2-bit response priority  

  5. 200  

  6. uart_init(9600); //Serial port initialization baud rate is 9600  

  7. LED_Init(); //LED port initialization  

  8. TIM3_Int_Init(4999,7199); //10Khz counting frequency, 500ms when counting to 5000  

  9.  while(1)  

  10. {  

  11. LED0=!LED0;  

  12. delay_ms(200);   

  13. }  

  14. }  

The final experimental result is that DS0 and DS1 flash alternately, but DS0 is twice as fast as DS1.


Keywords:STM32F103 Reference address:STM32F103 study notes (VII) Timer interrupt (update interrupt)

Previous article:STM32 drives ILI9341 controller to control TFTLCD display
Next article:STM32 study notes timer configuration

Recommended ReadingLatest update time:2024-11-16 13:04

STM32F103 controls AD7606 to collect analog signals
1、配置STM32F103的SPI口 void AD7606_Port_Init(void) { GPIO_InitTypeDef  GPIO_InitStructure; SPI_InitTypeDef   SPI_InitStructure; RCC_APB2PeriphClockCmd(AD_SPI_CS_GPIO_CLK | AD_SPI_MISO_GPIO_CLK | AD_SPI_SCK_GPIO_CLK, ENABLE); RCC_APB1PeriphClockCmd(AD_SPI_CLK, ENABLE);  /////////////////////////SPI_CLK/////////////////////
[Microcontroller]
STM32F103ZET6 general timer single pulse mode experiment
Because the core board I bought before always had power supply problems, my current project has changed to the F103ZET6 microcontroller I used before! 1. Purpose of the experiment 1) Generate a single pulse with adjustable pulse width (within the allowed range) 2. Hardware: General Timer 3, General Timer 4 3. In
[Microcontroller]
STM32F103ZET6 general timer single pulse mode experiment
STM32F103 remaps the JTAG port to a normal GPIO
For beginners of Mini STM32, why can't the output be controlled when using PB3 and PB4? First, after the STM32F10x series MCU is reset, PA13/14/15 & PB3/4 are configured as JTAG ports by default. Sometimes, in order to make full use of the resources of the MCU I/O port, we will set these ports as normal I/O ports. The
[Microcontroller]
STM32F103RCT6+USART3+UART5 initialization code
The difference between USART and UART is that USART supports synchronous transmission and reception, while UART only supports asynchronous transmission and reception. Synchronous mode: USART needs a synchronous signal USART_CK. Usually, the synchronous signal is rarely used. Therefore, the general microcontroller UA
[Microcontroller]
ADC multi-channel acquisition program for STM32f103 digital electrical acquisition circuit
STM32 has 1~3 ADCs (STM32F101/102 series has only 1 ADC), which can be used independently or in dual mode (increasing the sampling rate). The ADC of STM32 is a 12-bit successive approximation analog-to-digital converter. It has 18 channels and can measure 16 external and 2 internal signal sources. The A/D conversion o
[Microcontroller]
ADC multi-channel acquisition program for STM32f103 digital electrical acquisition circuit
Setting and using the touch screen of STM32f103
The touch screen solution is adopted for human-computer interaction of the multi-functional acquisition and display platform. The touch screen function mainly relies on the resistive touch screen. The main part of the touch screen is a resistive film screen that matches the display surface very well. This is a multi-
[Microcontroller]
Setting and using the touch screen of STM32f103
【STM32F103】Key detection (GPIO input)
Button hardware circuit: Analysis circuit: When button K1 is pressed, the high level 3.3V is connected. In order to protect GPIO, a current limiting resistor (R7) is added. When it is not pressed, it is grounded and the rising edge is input. PA0 has the function of automatic wake-up (must be awakened by rising ed
[Microcontroller]
【STM32F103】Key detection (GPIO input)
STM32F103 system clock configuration
STM32F103 system clock configuration Understanding the clock tree determines the frequency division and multiplication before doing it STM32F103 clock tree Assuming the external crystal oscillator is 8MHz, and now you need to configure the system to 72MHz, you need Configuration code (external crystal oscillator
[Microcontroller]
STM32F103 system clock configuration
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号