1.定时器初始化
void TIM6_Init() //定时器6初始化
{
TIM_TimeBaseInitTypeDef TIM_TimBaseStructure; //定义定时器结构体
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE); //使能定时器6的时钟
TIM_TimBaseStructure.TIM_Period = 1000; //定时器6的周期为1000
TIM_TimBaseStructure.TIM_Prescaler = 71; //定时器6的预分频为71
TIM_TimeBaseInit(TIM6,&TIM_TimBaseStructure); //初始化定时器6
TIM_ClearFlag(TIM6,TIM_FLAG_Update); //清除定时器6的更新标志
TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE); //使能定时器6的更新中断
TIM_Cmd(TIM6,ENABLE); //使能定时器6
NVIC_InitTypeDef NVIC_InitStructure; //定义NVIC结构体
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置NVIC的优先级分组为0
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn; //设置定时器6的中断通道为TIM6_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //设置定时器6的中断优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //设置定时器6的中断子优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能定时器6的中断通道
NVIC_Init(&NVIC_InitStructure); //初始化NVIC
}
STM32的定时器中断频率计算公式为:主频/(TIM_Period * (TIM_Prescaler-1)).
2.定时器中断处理函数
void TIM6_IRQHandler() //定时器6的中断处理函数
{
if(TIM_GetITStatus(TIM6,TIM_IT_Update) != RESET) //判断定时器6的更新中断是否发生
{
TIM_ClearITPendingBit(TIM6,TIM_IT_Update); //清除定时器6的更新中断标志
}
}
3. 总结
STM32的基本定时器比较简单,基本只有定时作用。
标签:NVIC,定时器,TIM6,TIM,中断,STM32,标准,InitStructure From: https://www.cnblogs.com/tqht7h/p/18052777