当前位置:网站首页>Stm32f4 --- general timer update interrupt
Stm32f4 --- general timer update interrupt
2022-07-07 02:01:00 【The moon shines on the silver sea like a dragon】
STM32F4 The timer is very powerful , Yes TIME1 and TIME8 Advanced timer, etc , Also have TIME2-TIME5,TIM9-TIM14 And so on , also TIME6 and TIME7 Wait for the basic timer , Total amount 14 There are as many timers .
STM32F4 Timer classification ( common 14 individual ):
- Advanced timer :TIME1 、TIME8
- Universal timer : TIME2-TIME5,TIM9-TIM14
- Basic timer : TIME6 、 TIME7
STM32F4 General timer Introduction
STM32F4 The universal timer contains a 16 Bit or 32 Bit auto overload counter (CNT), The counter consists of a programmable prescaler (PSC) drive .
STM32F4 A universal timer can be used for : Measure the pulse length of the input signal ( Input capture ) Or produce an output waveform ( Output comparison and PWM) etc. .
Use a timer prescaler and RCC Clock controller prescaler , Pulse length and waveform period can be adjusted from a few microseconds to a few milliseconds .
STM32F4 Each universal timer is completely independent , There are no resources shared with each other .
STM3 Common to TIMx (TIM2-TIM5 and TIM9-TIM14) Timer functions include :
16 position /32 position ( only TIM2 and TIM5) Up 、 Down 、 Up / Auto load counter down (TIMx_CNT), Be careful :TIM9~TIM14 Only support upward ( Increasing ) Count by .
16 Bit programmable ( It can be modified in real time ) Preassigned frequency counter (TIMx_PSC), The frequency division coefficient of the counter clock frequency is 1~65535 Any number between .
4 A separate channel (TIMx_CH14,TIM9TIM14 most 2 Channels ), These channels can be used as :
A. Input capture
B. Output comparison
C.PWM Generate ( Edge or center alignment mode ) , Be careful :TIM9~TIM14 Middle alignment mode is not supported
D. Monopulse mode outputExternal signals can be used (TIMx_ETR) Control timer and timer Interconnection ( It can be used 1 One timer controls another timer ) The synchronous circuit of .
Interrupt occurs when the following events occur /DMA(TIM9~TIM14 I won't support it DMA):
A. to update : The counter overflows up / Spilling down , Counter initialization ( Through software or internal / External trigger )
B. Triggering event ( The counter starts 、 stop it 、 Initialized or internally / External trigger count )
C. Input capture
D. Output comparison
E. Support for incremental positioning ( orthogonal ) Encoder and Hall sensor circuits (TIM9~TIM14 I won't support it )
F. Trigger input as an external clock or current management by cycle (TIM9~TIM14 I won't support it )
STM32F4 Register of general timer
Control register 1
Control register 1(TIMx_CR1)
The description of each bit of this register is shown in Fig
TIMx_CR1 The lowest point of , That is, the counter enable bit , This bit must be set to 1, To get the timer to start counting .
DMA/ Interrupt enable register
DMA/ Interrupt enable register (TIMx_DIER)
The register is a 16 Bit register , Their descriptions are shown in the figure :
The first 0 position , This bit is the update interrupt allowed bit , Update interrupt using timer , This bit needs to be set to 1, To allow interrupts due to update Events .
Prescaler register
Prescaler register (TIMx_PSC).
This register divides the clock by setting , Then provide it to the counter , As a counter clock . The description of each bit of this register is shown in Fig
here , The clock sources of timers are 4 individual :
1) Internal clock (CK_INT)
2) External clock mode 1: External input pins (TIx)
3) External clock mode 2: External trigger input (ETR), Only applicable to TIM2、TIM3、TIM4
4) Internal trigger input (ITRx): Use A Timer as B Timer prescaler (A by B Provide clock ).
These clocks , The specific choice can be made by TIMx_SMCR Register to set . there CK_INT The clock is from APB1 Double frequency , Unless APB1 Set the number of clock dividers to 1( It's not usually 1), Otherwise, the universal timer TIMx The clock is APB1 Of the clock 2 times , When APB1 When the clock doesn't divide , Universal timer TIMx The clock is equal to APB1 The clock of . The advanced timer and TIM9~TIM11 The clock doesn't come from APB1, But from APB2 Of .
Counter register
TIMx_CNT register , This register is the counter of the timer , This register stores the count value of the current timer .
Auto reload register
Auto reload register (TIMx_ARR)
This register physically corresponds to 2 A register . One is that programmers can operate directly , The other is something that programmers can't see , This invisible register is called shadow register . In fact, what really works is the shadow register . According to the TIMx_CR1 In the register APRE Bit setting :APRE=0 when , The contents of the preloaded register can be transferred to the shadow at any time
Sub register , here 2 The is connected ; and APRE=1 when , At each update event (UEV) when , Just put the preload register (ARR) The contents of are transferred to the shadow register .
The description of each bit of the automatic reload register is shown in the figure
Status register
Status register (TIMx_SR)
This register is used to mark various events related to the timer / Whether the interruption occurs . The description of each bit of this register is shown in Fig
Configure timer through library function
Timer related library functions mainly focus on firmware library files stm32f4xx_tim.h and stm32f4xx_tim.c In file . The timer configuration steps are as follows :
TIM3 Clock enable .
TIM3 It's mounted on APB1 under , So pass APB1 The clock enable function under the bus enables TIM3. The function called is :
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); /// Can make TIM3 The clock
Initialize timer parameters , Set auto reload value , Division coefficient , Counting method, etc
In library functions , The initialization parameter of timer is through initialization function TIM_TimeBaseInit Realized :
voidTIM_TimeBaseInit(TIM_TypeDef*TIMx,
TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct);
The first parameter is to determine which timer
The second initialization parameter is the body parameter of the pointer , The structural type is TIM_TimeBaseInitTypeDef, The definition of this structure :
typedef struct
{
uint16_t TIM_Prescaler;
uint16_t TIM_CounterMode;
uint16_t TIM_Period;
uint16_t TIM_ClockDivision;
uint8_t TIM_RepetitionCounter;
} TIM_TimeBaseInitTypeDef;
This structure has 5 Member variables , For general timers, only the first four parameters are useful , Last parameter TIM_RepetitionCounter It's the advanced timer that works
The first parameter TIM_Prescaler It is used to set the frequency division coefficient
The second parameter TIM_CounterMode Is used to set the counting mode , It can be set to count up , Down counting and center aligned counting , More commonly used is the up count mode TIM_CounterMode_Up And count down mode TIM_CounterMode_Down.
The third parameter is to set the auto overload count cycle value
The fourth parameter is used to set the clock division factor
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 5000;
TIM_TimeBaseStructure.TIM_Prescaler =7199;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
Set up TIM3_DIER Allow update interrupt
To use TIM3 The update of , The corresponding bit of the register enables the update interrupt . In the library function, timer interrupt is enabled by TIM_ITConfig Function to implement :
void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState);
The first parameter is the timer
The second parameter is critical , Is used to indicate the type of timer interrupt that we enable , There are many types of timer interrupts , Including update interrupt TIM_IT_Update, Trigger interrupt TIM_IT_Trigger, And input capture interrupt and so on .
The third parameter is disable or enable .
Can make TIM3 The update of , The format is :
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );
TIM3 Interrupt priority setting .
After timer interrupt enable , Because it's going to interrupt , It's essential to set up NVIC Related registers , Set interrupt priority
Can make TIM3
Start the timer after configuration , adopt TIM3_CR1 Of CEN Bit to set . In the firmware library, the function to enable timer is through TIM_Cmd Function to implement :
void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)
Enable timer 3, Method is :
TIM_Cmd(TIM3, ENABLE); // Can make TIMx peripherals
Write interrupt service function
In the end , Or write timer interrupt service function , This function is used to process the related interrupt generated by timer .
After the interrupt is generated , The value of the status register is used to determine the type of interrupt generated . Then perform the relevant operations
Use update ( overflow ) interrupt , In the status register SR The lowest point of . After processing the interrupt, it should be directed to TIM3_SR Write at the lowest position of 0, To clear the interrupt flag .
In the firmware library function , The function used to read the value of interrupt status register and judge the type of interrupt is :
ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, uint16_t TIM_IT)
The function is to , Judge timer TIMx The interrupt type of TIM_IT Whether there is an interruption . such as , We must judge
Chronometer 3 Whether there is an update ( overflow ) interrupt , Method is :
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET){
}
The function of clearing interrupt flag bit in firmware library is :
void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT)
The function is to , Clear timer TIMx The interrupt TIM_IT Sign a . stay TIM3 After the overflow interrupt of , We want to clear the interrupt flag bit , The method is :
TIM_ClearITPendingBit(TIM3, TIM_IT_Update );
The firmware library also provides two functions to judge the timer status and clear the timer status flag bit TIM_GetFlagStatus and TIM_ClearFlag, Their role is similar to that of the previous two functions . It's just TIM_GetITStatus The function will first determine whether the interrupt is enabled , Enable to judge the interrupt flag bit , and TIM_GetFlagStatus Directly used to determine the status flag bit .
Code
// Universal timer 3 Interrupt initialization
//arr: Auto reload value . psc: Clock presplitting frequency
// Timer overflow time calculation method :Tout=((arr)*(psc))/Ft us.
//Ft= Timer operating frequency , Company :Mhz
// Here's a timer 3!
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); // Can make TIM3 The clock
TIM_TimeBaseInitStructure.TIM_Period = arr-1; // Automatic reload load value
TIM_TimeBaseInitStructure.TIM_Prescaler=psc-1; // Timer frequency division
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; // Upcount mode
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);// Initialize the timer TIM3
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); // Allow timer 3 Update interrupt
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; // Timer 3 interrupt
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; // preemption 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; // Response priority 3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);// initialization 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
{
PPS_OUT=1;
delay_ms(100);
PPS_OUT=0;
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update); // Clears the interrupt flag bit
}
Contains an interrupt service function and a timer 3 Interrupt initialization function
Of this function 2 Two parameters are used to set TIM3 Overflow time . Because the system is initialized SystemInit The function has been initialized APB1 The clock is 4 frequency division , the
With APB1 The clock is 42M,
When APB1 The number of clock frequency division is 1 When ,TIM2-7 as well as TIM12-14 The clock is APB1 The clock of , And if the APB1 The number of clock divisions is not 1, that TIM2-7 as well as TIM12-14 The clock frequency of will be APB1 Twice the clock . therefore ,TIM3 The clock is 84M, Then according to the design arr and psc Value , You can calculate the interruption time .
The calculation formula is as follows :
Tout= ((arr+1)*(psc+1))/Tclk;
among :
Tclk:TIM3 Input clock frequency ( Unit is Mhz).
Tout:TIM3 Overflow time ( Unit is s).
Then in the interrupt function , Judge after interruption , pull up AF10, Time delay 100ms Pull down again .
边栏推荐
- 一文带你走进【内存泄漏】
- PartyDAO如何在1年内把一篇推文变成了2亿美金的产品DAO
- Box stretch and pull (left-right mode)
- C language [23] classic interview questions [Part 2]
- 使用nodejs完成判断哪些项目打包+发版
- Appium foundation - appium inspector positioning tool (I)
- Unicode string converted to Chinese character decodeunicode utils (tool class II)
- Shell script quickly counts the number of lines of project code
- Flir Blackfly S 工业相机:配置多个摄像头进行同步拍摄
- STM32F4---PWM输出
猜你喜欢
随机推荐
组合导航:中海达iNAV2产品描述及接口描述
Halcon knowledge: segment_ contours_ XLD operator
JS ES5也可以创建常量?
Integrated navigation: product description and interface description of zhonghaida inav2
FLIR blackfly s industrial camera: configure multiple cameras for synchronous shooting
红外相机:巨哥红外MAG32产品介绍
JVM 内存模型
Image watermarking, scaling and conversion of an input stream
Domestic images of various languages, software and systems. It is enough to collect this warehouse: Thanks mirror
ROS学习(二十)机器人SLAM功能包——rgbdslam的安装与测试
ROS学习(26)动态参数配置
The foreground downloads network pictures without background processing
传感器:土壤湿度传感器(XH-M214)介绍及stm32驱动代码
ROS学习(23)action通信机制
Basic introduction and use of dvajs
激光雷达:Ouster OS产品介绍及使用方法
454 Baidu Mianjing 1
The use of video in the wiper component causes full screen dislocation
IDEA常用的快捷键
Flir Blackfly S工业相机:颜色校正讲解及配置与代码设置方法