STM32 TIM1 CH1 1ms的中断定时函数
时间: 2023-11-20 11:05:56 浏览: 86
stm32 定时器中断算法
以下是使用STM32 TIM1 CH1实现1ms定时器中断的示例代码:
```c
#include "stm32f4xx.h"
void TIM1_UP_TIM10_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
// 在这里编写你需要执行的1ms定时任务
// ...
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
}
}
void TIM1_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 8399; // 预分频器分频后计数器重装值,1ms时钟周期
TIM_TimeBaseStructure.TIM_Prescaler = 99; // 预分频器分频系数,84MHz时钟,分频系数为100
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM1, ENABLE);
}
```
在主函数中,只需调用`TIM1_Configuration()`函数即可配置TIM1定时器,中断服务函数`TIM1_UP_TIM10_IRQHandler()`中编写你需要执行的1ms定时任务即可。
阅读全文