当前位置:网站首页>STM32 timer interrupt learning notes
STM32 timer interrupt learning notes
2022-06-13 01:57:00 【richardgann】
stay stm32 in , Timer related library functions mainly focus on firmware library files stm32f10x_tim.h and stm32f10x_tim.c In file , So when processing the timer , You need to load these two files .
During timer operation , Generally, the following steps are followed :
1、 Turn on the clock of the relevant peripheral .
With a timer TIM3 For example , from stm32 The clock tree can be seen ,TIM3 The clock is connected to APB1 above , So open TIM3 Clock time use RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE), If other peripherals are used , Such as GPIO etc. , Turn on the relevant peripheral clock .
2、 Clear the interrupt hold bit .
Due to various unknowable factors , The interrupt pending bit of the timer to be operated before the program runs may be set , This will cause the interrupt service program to enter the timer interrupt at the beginning of the program . To eliminate this effect , We clear the interrupt hold bit at the beginning of the program .
Use... In the firmware library :
void TIM_ClearITPendingBit(TIM_TypeDef*TIMx, u16 TIM_IT)
To clear the interrupt hold bit , Refer to the firmware library manual for the specific use of this function .
3、 Timer basic configuration initialization .
In this step, it mainly determines the pre frequency division of the timer and sets the value of the automatic reload register cycle , And determine the counting mode , This mainly uses... In the firmware library TIM_TimeBaseInit() Function to operate , The prototype of this function is :
void TIM_TimeBaseInit(TIM_TypeDef* TIMx,TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
The first input parameter has nothing to say , Which timer is used .
typedef struct
{
<span style="white-space:pre;"> </span>u16 TIM_Period;// The value of the automatic reload register cycle
<span style="white-space:pre;"> </span>u16 TIM_Prescaler;// Prescaled value
<span style="white-space:pre;"> </span>u8 TIM_ClockDivision;// Partition coefficient , Generally set as 0, Do not divide
<span style="white-space:pre;"> </span>u16 TIM_CounterMode;// Count mode
} TIM_TimeBaseInitTypeDef; 4、 Enable timer TIMx.
This simple , Direct functions TIM_Cmd() Function is OK , Such as enable timer TIM3 peripherals , Available TIM_Cmd(TIM3,ENABLE).
5、 Can make TIMx interrupt .
Just call the function . Because we need 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 , This is easy to understand , The value is TIM1~TIM17.
The second parameter is critical , Is used to indicate the type of timer interrupt that we enable , Timer interrupt .
There are many types , Including update interrupt TIM_IT_Update, Trigger interrupt TIM_IT_Trigger, And input capture interrupt and so on .
The third parameter is very simple , Is it disabled or enabled .
For example, we need to enable TIM3 The update of , The format is :TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );
6、 Configure interrupt priority .
That is, configure nested vector terminal controllers NVIC. To perform this step, you first need to configure the priority grouping , You can use library functions NVIC_PriorityGroupConfig() Conduct , The number of the group is the number of bits of preemptive priority , Then configure NVIC initialization , Using functions NVIC_Init() Conduct , The prototype of this function is void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct), The structure of input parameters is defined as follows :
typedef struct
{
<span style="white-space:pre;"> </span>u8 NVIC_IRQChannel; // Interrupt channel
<span style="white-space:pre;"> </span>u8 NVIC_IRQChannelPreemptionPriority;// preemption
<span style="white-space:pre;"> </span>u8 NVIC_IRQChannelSubPriority;// Response priority
<span style="white-space:pre;"> </span>FunctionalState NVIC_IRQChannelCmd;// Specify in the member NVIC_IRQChannel As defined in IRQ Is the channel enabled or disabled
} NVIC_InitTypeDef; 7、 Write interrupt service program . ad locum , We first need to clear the interrupt hold bit , This function is the same as that in Chapter 2 In step , There is no need to repeat , Then write the interrupt processing content .
The complete timer interrupt initialization procedure is as follows , This program is the contents of timer interrupt in the CD provided by Puzhong technology .
void timeInit()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; // Declare a structure variable , Used to initialize GPIO
NVIC_InitTypeDef NVIC_InitStructure;
/* Turn on timer 3 The clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);// eliminate TIMx Interrupt pending bit of :TIM Interrupt source
TIM_TimeBaseInitStructure.TIM_Period = 2000;// Set the value of the automatic reload register cycle
TIM_TimeBaseInitStructure.TIM_Prescaler = 35999;// Set as TIMx Clock frequency prescaled value ,100Khz Counting frequency
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; // Set the clock split :TDTS = Tck_tim
TIM_TimeBaseInitStructure.TIM_CounterMode =TIM_CounterMode_Up;//TIM Upcount mode
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_Cmd(TIM3,ENABLE); // To enable or disable TIMx peripherals //* Set interrupt parameters , And turn on the interrupt */
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE ); // To enable or disable specified TIM interrupt
/* Set up NVIC Parameters */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; // open TIM3_IRQn Global interrupt
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;// Preemption priority is 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1; // The response priority is 1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Can make
NVIC_Init(&NVIC_InitStructure);
} 边栏推荐
- Ten thousand words make it clear that synchronized and reentrantlock implement locks in concurrency
- The first cell of devaxpress CXGRID after inserting a row is in focus editing status
- 指针链表的实现
- Examples of using the chromium base library
- Devaxpress Chinese description -- tdxgallerycontrol object (gallery component)
- Vs how to enter chromium subprocess debugging
- 三、上传织物图片至SQL Server并提供name进行展示织物照片
- [the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys
- Super complete regular expressions
- 白噪声的详细理解
猜你喜欢

Get started quickly cmake

移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式

四、入库管理功能的完善

Ruixing coffee 2022, extricating itself from difficulties and ushering in a smooth path

华为设备配置私网IP路由FRR

10 days based on stm32f401ret6 smart lock project practice day 1 (environment construction and new construction)
![[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO](/img/98/77191c51c1bab28448fe197ea13a33.jpg)
[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO

一种不带CPU的DPU架构:Hyperion

水管工遊戲

Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)
随机推荐
pytorch : srcIndex < srcSelectDimSize
How do you use your own data to achieve your marketing goals?
LeetCode每日一题——890. 查找和替换模式
Delphi implements adding a column of serial number to the CXGRID list
The scientific innovation board successfully held the meeting, and the IPO of Kuangshi technology ushered in the dawn
传感器:MQ-5燃气模块测量燃气值(底部附代码)
Implementation of pointer linked list
Vs how to enter chromium subprocess debugging
matplotlib画图中文乱码
[printf function and scanf function] (learning note 5 -- standard i/o function)
Opencv camera calibration (2): fish eye camera calibration
服务器安装jupyterlab以及远程登录配置
Get started quickly cmake
Magics 23.0 how to activate and use the slice preview function of the view tool page
[pytorch FAQ] numpy:dll load failed while importing_ multiarray_ Umath: the specified module could not be found.
When AI meets music, iFLYTEK music leads the industry reform with technology
Plumber game
Alertwindowmanager pop up prompt window help (Part 1)
指针链表的实现
路径字段是什么? ——竞价广告