void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//配置TIM2中断
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel =TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //打开TIM2定时器的时钟
TIM_DeInit(TIM2); //TIMx寄存器重设为缺省值
TIM_TimeBaseStructure.TIM_Period = 20000; //自动重装载寄存器周期的值
//TIM_Period 设置了在下一个更新事件装入活动的自动重装载寄存器周期的值。它的取值必须在0x0000和0xFFFF之间。
TIM_TimeBaseStructure.TIM_Prescaler=360 - 1; //TIMx时钟频率除数的预分频值
//TIM_Prescaler 设置了用来作为TIMx时钟频率除数的预分频值。它的取值必须在 0x0000 和0xFFFF之间。
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //采样分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ARRPreloadConfig(TIM2, ENABLE); //允许自动重装载寄存器(ARR)
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //允许TIM2溢出中断
TIM_Cmd(TIM2, ENABLE); //TIM2开启时钟
}
bool bFlag=FALSE;
void TIM2_IRQHandler(void)
{
// GPIOC->ODR ^= (1<<4); //闪灯
if(bFlag){
GPIO_SetBits(GPIOA,GPIO_Pin_8);
bFlag=FALSE;
}
else {
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
bFlag=TRUE;
}
TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); //清中断
}