代码如下::
TIM_ICInitTypeDef TIM1_ICInitStructure;
void time1_CH1_CAP_init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
TIM1_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM1_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM1, &TIM1_ICInitStructure);
TIM_ITConfig(TIM1,TIM_IT_Update|TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM1,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
u8 TIM1CH1_CAPTURE_STA=0; //ê?è?2???×′ì?
u16 TIM1CH1_CAPTURE_VAL;
u32 TIM1CH1_CAPTURE_SUM_TIME=0; //×üμ?ê±??
void TIM1_UP_TIM10_IRQHandler(void)
{
if((TIM1CH1_CAPTURE_STA&0X80)==0)
{
if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
if(TIM1CH1_CAPTURE_STA&0X40)
{
if((TIM1CH1_CAPTURE_STA&0X3F)==0X3F)
{
TIM1CH1_CAPTURE_STA|=0X80;
TIM1CH1_CAPTURE_VAL=0XFFFF;
TIM1CH1_CAPTURE_SUM_TIME=TIM1CH1_CAPTURE_STA&0X3F;
TIM1CH1_CAPTURE_SUM_TIME*=0XFFFF;
TIM1CH1_CAPTURE_SUM_TIME+=TIM1CH1_CAPTURE_VAL;
}
else
{
TIM1CH1_CAPTURE_STA++;
}
}
}
if(TIM_GetITStatus(TIM1, TIM_IT_CC1) != RESET)
{
if(TIM1CH1_CAPTURE_STA&0X40)
{
TIM1CH1_CAPTURE_STA|=0X80;
TIM1CH1_CAPTURE_VAL=TIM_GetCapture1(TIM1);
TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Rising);
TIM1CH1_CAPTURE_SUM_TIME=TIM1CH1_CAPTURE_STA&0X3F;
TIM1CH1_CAPTURE_SUM_TIME*=0XFFFF;
TIM1CH1_CAPTURE_SUM_TIME+=TIM1CH1_CAPTURE_VAL;
}
else
{
TIM1CH1_CAPTURE_STA=0;
TIM1CH1_CAPTURE_VAL=0;
TIM1CH1_CAPTURE_SUM_TIME=0;
TIM1CH1_CAPTURE_STA|=0X40;
TIM_Cmd(TIM1,ENABLE );
TIM_SetCounter(TIM1,0);
TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Falling);
TIM_Cmd(TIM1,ENABLE );
}
}
}
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1|TIM_IT_Update);
}
初始化
TIM3_PWM_Init(500-1,84-1);
TIM_SetCompare3(TIM3,300);
time1_CH1_CAP_init(0XFFFF,168-1);
串口打印出来的都是 0 ,但是如将time4 做的程序和time1 做的程序基本一样的 time4 是可以的!!! 求大神指教????谢谢,有谁做过time1 的捕获的 希望传个例子,谢谢!! |