stm32定时器的设置
函数实现过程:
一、定时器配置初始化
void TIME3_config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;//定义一个定时器的结构体
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//使能时钟,在RCC.c里找
TIM_TimeBaseInitStruct.TIM_Prescaler = 900-1; //1-65535 //分频赋值
TIM_TimeBaseInitStruct.TIM_Period = 10000 -1; //1-65535 //重载值
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; // 计数模式向上
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟源分频,一般固定选DIVI1
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);//初始化定时器
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //定时器使能
}
二、编辑中断函数
void TIM3_IRQHandler(void) //NVIC 里的中断函数
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)//判断中断标致位
{
中断函数
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //清理中断标志位
}
}
三、中断优先级设置
void NVIC_tim3_config(void)
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn; //stm32f4xx.h里找的
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}