[mw_shl_code=c,true]//外部输入
void TIM2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitTypeDef;
GPIO_InitTypeDef GPIO_InitTypeDef;
NVIC_InitTypeDef NVIC_InitTypeDef;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
GPIO_InitTypeDef.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitTypeDef.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitTypeDef.GPIO_OType=GPIO_OType_OD;
GPIO_InitTypeDef.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_InitTypeDef.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitTypeDef);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2);
TIM_DeInit(TIM2);
TIM_TimeBaseInitTypeDef.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitTypeDef.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitTypeDef.TIM_Period=0xffff;
TIM_TimeBaseInitTypeDef.TIM_Prescaler=0x00;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitTypeDef);
TIM_ARRPreloadConfig(TIM2,ENABLE);
TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Regular);//Configures the TIMx Update Request Interrupt source.
//Configures the TIMx External Trigger (ETR)
TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
//配置输入触发源
TIM_SelectInputTrigger(TIM2,TIM_TS_ETRF);// External Trigger input
TIM_SetCounter(TIM2,0);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_ClearFlag(TIM2,TIM_FLAG_Trigger);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_ITConfig(TIM2,TIM_FLAG_Trigger,ENABLE);
NVIC_InitTypeDef.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitTypeDef.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitTypeDef.NVIC_IRQChannelSubPriority=2;
NVIC_InitTypeDef.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitTypeDef);
TIM_Cmd(TIM2,DISABLE);
}
u32 num=0;
u32 m=0;
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)
{
num++;
TIM_SetCounter(TIM2,0);
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
if(TIM_GetITStatus(TIM2,TIM_IT_Trigger) == SET)
{
m++;
TIM_SetCounter(TIM2,0);
TIM_ClearITPendingBit(TIM2,TIM_IT_Trigger);
}
}
//定时1s[/mw_shl_code]
求助 为什么可以计数 但是没办法触发中断.
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
问题出在这里吧,定时器都被你关了。。。
一周热门 更多>