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); //時(shí)鐘使能
? ??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復(fù)用位定時(shí)器5
? ? ?TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期的值?
? ???TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來(lái)作為TIMx時(shí)鐘頻率除數(shù)的預(yù)分頻值? 10Khz的計(jì)數(shù)頻率
? ? ?TIM_TimeBaseStructure.TIM_ClockDivision = 0; //設(shè)置時(shí)鐘分割:TDTS = Tck_tim
? ???TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;? //TIM向上計(jì)數(shù)模式
? ??TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時(shí)間基數(shù)單位
? ??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;? //先占優(yōu)先級(jí)0級(jí)
? ?NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;? //從優(yōu)先級(jí)3級(jí)
? ?NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
? ?NVIC_Init(&NVIC_InitStructure);? //根據(jù)NVIC_InitStruct中指定的參數(shù)初始化外設(shè)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外設(shè)
}
u16 speed_JC;
void TIM2_IRQHandler(void)? ?//TIM2中斷
{
?if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) //檢查指定的輸入捕獲中斷發(fā)生與否:TIM 中斷源
?{
?speed_JC++;
?printf ("%d\r\n",speed_JC);
?TIM_ClearITPendingBit(TIM2,TIM_IT_CC1);? //清除TIM定時(shí)器更新中斷待處理位:TIM 中斷源
?}
}