stm32定時器的設置
函數(shù)實現(xiàn)過程:
一垫桂、定時器配置初始化
void TIME3_config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;//定義一個定時器的結(jié)構(gòu)體
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;? // 計數(shù)模式向上
TIM_TimeBaseInitStruct.TIM_ClockDivision? = TIM_CKD_DIV1;//時鐘源分頻搅方,一般固定選DIVI1
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);//初始化定時器
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);? ? ? ? //定時器使能
}
二扒俯、編輯中斷函數(shù)
void TIM3_IRQHandler(void)? //NVIC 里的中斷函數(shù)
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)//判斷中斷標致位
{
中斷函數(shù)
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);? //清理中斷標志位
}
}
三、中斷優(yōu)先級設置
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);
}