STM32f4 timer interrupt experimental code

Publisher:rnm888Latest update time:2018-10-05 Source: eefocusKeywords:STM32f4 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Let's take a look at our time.c file. The code of the timer.c file is as follows:

//General timer 3 interrupt initialization

//arr: automatic reload value. psc: clock pre-division number

//Timer overflow time calculation method: Tout=((arr+1)*(psc+1))/Ft us.

//Ft=timer operating frequency, unit: Mhz

//Timer 3 is used here!

void TIM3_Int_Init(u16 arr,u16 psc)

{

  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //Enable TIM3 clock

  

   TIM_TimeBaseInitStructure.TIM_Period = arr; //Automatically reload value

  TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //Timer frequency division

  TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //Upward counting mode

  TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;  

  

  TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure); // Initialize timer TIM3

  

  TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //Enable timer 3 update interrupt

  

  NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //Timer 3 interrupt

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //Preemption priority 1

  NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //Response priority 3

  NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

  NVIC_Init(&NVIC_InitStructure); // Initialize NVIC  

 

TIM_Cmd(TIM3,ENABLE); //Enable timer 3  

}

 

//Timer 3 interrupt service function

void TIM3_IRQHandler(void)

{

  if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) // Overflow interrupt

  {

    LED1=!LED1;

  }

  TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //Clear interrupt flag

}

This file contains an interrupt service function and a timer 3 interrupt initialization function. The interrupt service function is relatively simple. After each interrupt, the interrupt type of TIM3 is determined. If the interrupt type is correct, the flip of LED1 (DS1) is executed. The TIM3_Int_Init function executes the 5 steps we introduced above to make TIM3 start working and enable interrupts. Here we use the label ~ to mark the five steps of timer initialization. The 2 parameters of this function are used to set the overflow time of TIM3. Because the APB1 clock has been initialized to 4 divisions in the system initialization SystemInit function, the APB1 clock is 42M. From the internal clock tree diagram of STM32F4 (Figure 4.3.1.1), we know that when the APB1 clock division number is 1, the clocks of TIM2~7 and TIM12~14 are the clocks of APB1. If the APB1 clock division number is not 1, the clock frequencies of TIM2~7 and TIM12~14 will be twice the APB1 clock. Therefore, the clock of TIM3 is 84M. Based on the values ​​of arr and psc we designed, we can calculate the interrupt time. The calculation formula is as follows:

Tout= ((arr+1)*(psc+1))/Tclk;

in:

Tclk: Input clock frequency of TIM3 (in MHz).

Tout: TIM3 overflow time (in us).

  The content of the timer.h header file is relatively simple, so we will not explain it here.

Finally, let's look at the main function code as follows:

int main(void)

{  

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set system interrupt priority group 2

  delay_init(168); //Initialize delay function

  LED_Init(); //Initialize LED port

    TIM3_Int_Init(5000-1,8400-1); //Timer clock 84M, frequency division factor 8400, so 84M/8400=10Khz

//Counting frequency, counting 5000 times is 500ms      

  while(1)

  {

    LED0=!LED0;

    delay_ms(200); //delay 200ms

  };

}

The code here is similar to the previous one. After initializing TIM3, this code enters an infinite loop waiting for the TIM3 overflow interrupt. When the value of TIM3_CNT is equal to the value of TIM3_ARR, a TIM3 update interrupt is generated, and then LED1 is negated in the interrupt, and TIM3_CNT starts counting from 0 again.

Here, the timer timing duration of 500ms is calculated as follows: the timer clock is 84Mhz, and the division factor is 8400, so the counting frequency after division is 84Mhz/8400=10KHz, and then it counts to 5000, so the duration is 5000/10000=0.5s, which is 500ms.


Keywords:STM32f4 Reference address:STM32f4 timer interrupt experimental code

Previous article:STM32f4 PWM output experimental code
Next article:STM32 program transplantation_internal flash boot times management lib library establishment

Recommended ReadingLatest update time:2024-11-16 14:51

STC MCU timer2 capture mode frequency measurement
The most common method of measuring frequency using an STC microcontroller is to count the number of pulses within a certain period of time. This method generally requires a counter and a timer to work together, and it is not very accurate for low-frequency signals. Next, we can use the capture mode of timer2 to calcu
[Microcontroller]
Using STM32CubeMX for STM32F429 LCD Programming Global Configuration
illustrate: The following programs all take the library in STM32Cube_FW_F4_V1.16.0 as an example. The STM32CubeMX version number is STM32CubeMX 4.22.0. The LCD is 1024 x 768 15-inch LCD. Use SDRAM as LCD frame buffer. The overall pin configuration diagram of the STM32CubeMX chip is as follows: 1. STM32F429 LTDC int
[Microcontroller]
STM32F407 advanced timer function description
Mainly introduce some of the functions I use 1. Time base unit mainly include: ● Counter register (TIMx_CNT) ● Prescaler register (TIMx_PSC) ● Auto-reload register (TIMx_ARR) ● Repeat Counter Register (TIMx_RCR) Prescaler Description: The prescaler can divide the counter clock frequency by a factor between 1 and
[Microcontroller]
STM32F407 advanced timer function description
Getting started with STM32F4 DISCOVERY - power consumption decryption
In today's energy-constrained era, how to save power has always been the focus of major MCU manufacturers. While continuously improving performance, system power consumption is also constantly decreasing. As a signal processing MCU with an M4 core in the industrial control field, the STM32F4 must have outstanding powe
[Microcontroller]
Getting started with STM32F4 DISCOVERY - power consumption decryption
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号