定时器中断只进入一次
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3时钟
TIM_TimeBaseStructure.TIM_Period = arr; //指定下次更新事件时要加载到活动自动重新加载寄存器中的周期值
TIM_TimeBaseStructure.TIM_Prescaler = psc; //指定用于划分TIM时钟的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_1; //时钟分频因子
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM计数模式,向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //使能TIM3中断,允许更新中断
//初始化TIM NVIC,设置中断优先级分组
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //设置抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //设置响应优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能通道1中断
NVIC_Init(&NVIC_InitStructure); //初始化NVIC
TIM_Cmd(TIM3, ENABLE); //TIM3使能
}
void TIM3_IRQHandler(void)
{
static u8 i=0;
if (TIM_GetITStatus(TIM3, TIM_IT_Update)!=RESET) //检查TIM3中断是否发生
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //清除TIM3的中断挂起位
GPIO_WriteBit(GPIOA, GPIO_Pin_1, (i==0) ? (i=Bit_SET):(i=Bit_RESET));
printf("GPIO Toggle\r\n");
}
}