void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM2_Int_Init(u16 arr,u16 psc)
{? ?GPIO_InitTypeDef? GPIO_InitStructure ;
? ??TIM_ICInitTypeDef? ?TIM_ICInitStructure ;
? ??TIM_TimeBaseInitTypeDef? TIM_TimeBaseStructure;
? ???NVIC_InitTypeDef NVIC_InitStructure;
? ? RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
? ??RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
? ???GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
? ???GPIO_InitStructure.GPIO_Mode =? GPIO_Mode_IPD;
? ???GPIO_Init(GPIOA, &GPIO_InitStructure);
? ? ??GPIO_ResetBits(GPIOA, GPIO_Pin_15);
? ???GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); //PA0复用位定时器5
? ? ?TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值?
? ???TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值? 10Khz的计数频率
? ? ?TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
? ???TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;? //TIM向上计数模式
? ??TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
? ??TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;? //选择通道2
? ???TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//配置分频不分频
? ???TIM_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
? ???TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿捕获
? ???TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//映射到TI1上
? ???TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
? ? NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
? ? NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;? //先占优先级0级
? ?NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;? //从优先级3级
? ?NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
? ?NVIC_Init(&NVIC_InitStructure);? //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
?TIM2->INTFR = 0;
?TIM_ITConfig(TIM2, TIM_IT_CC1 , ENABLE);
?//TIM_ITConfig( TIM2, TIM_IT_Update ,ENABLE? );
?TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
?TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
?TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
?TIM_Cmd(TIM2, ENABLE);? //使能TIMx外设
}
u16 speed_JC;
void TIM2_IRQHandler(void)? ?//TIM2中断
{
?if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) //检查指定的输入捕获中断发生与否:TIM 中断源
?{
?speed_JC++;
?printf ("%d\r\n",speed_JC);
?TIM_ClearITPendingBit(TIM2,TIM_IT_CC1);? //清除TIM定时器更新中断待处理位:TIM 中断源
?}
}