我仿照原子的例程写了定时器2的输入捕获代码 可是不能用 请大家帮忙看看
定时器2用的是3和4通道 对应PB10和PB11 定时器5用1和2通道 对应PA0 PA1
void PPM(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_ICInitTypeDef TIM5_ICInitStructure;
TIM_ICInitTypeDef TIM2_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10|GPIO_Pin_11;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM5);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_TIM2);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_TIM2);
TIM_TimeBaseStructure.TIM_Prescaler=167;
TIM_TimeBaseStructure.TIM_Period=19999;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler=167;
TIM_TimeBaseStructure.TIM_Period=19999;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM5_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM5_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter=0x00;
TIM_ICInit(TIM5,&TIM5_ICInitStructure);
TIM5_ICInitStructure.TIM_Channel=TIM_Channel_2;
TIM5_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter=0x00;
TIM_ICInit(TIM5,&TIM5_ICInitStructure);
TIM5_ICInitStructure.TIM_Channel=TIM_Channel_3;
TIM5_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter=0x00;
TIM_ICInit(TIM2,&TIM2_ICInitStructure);
TIM5_ICInitStructure.TIM_Channel=TIM_Channel_4;
TIM5_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter=0x00;
TIM_ICInit(TIM2,&TIM2_ICInitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC2,ENABLE);
TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC3,ENABLE);
TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_CC4,ENABLE);
TIM_Cmd(TIM5,ENABLE);
TIM_Cmd(TIM2,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=TIM5_IRQn|TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM5_IRQHandler(void)
{
if((TIM5CH1_CAPTURE_STA&0X80)==0)//»¹Î´³É¹¦²¶»ñ
{
if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)//Òç³ö
{
if(TIM5CH1_CAPTURE_STA&0X40)//ÒѾ­²¶»ñµ½¸ßµçƽÁË
{
if((TIM5CH1_CAPTURE_STA&0X3F)==0X3F)//¸ßµçƽ̫³¤ÁË
{
TIM5CH1_CAPTURE_STA|=0X80; //±ê¼Ç³É¹¦²¶»ñÁËÒ»´Î
TIM5CH1_CAPTURE_VAL=0XFFFFFFFF;
}else TIM5CH1_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET)
{
if(TIM5CH1_CAPTURE_STA&0X40)
{
TIM5CH1_CAPTURE_STA|=0X80;
TIM5CH1_CAPTURE_VAL=TIM_GetCapture1(TIM5);
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising);
}else
{
TIM5CH1_CAPTURE_STA=0;
TIM5CH1_CAPTURE_VAL=0;
TIM5CH1_CAPTURE_STA|=0X40;
TIM_Cmd(TIM5,DISABLE );
TIM_SetCounter(TIM5,0);
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling);
TIM_Cmd(TIM5,ENABLE );
}
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update);
if((TIM5CH2_CAPTURE_STA&0X80)==0)
{
if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
{
if(TIM5CH2_CAPTURE_STA&0X40)
{
if((TIM5CH2_CAPTURE_STA&0X3F)==0X3F)
{
TIM5CH2_CAPTURE_STA|=0X80;
TIM5CH2_CAPTURE_VAL=0XFFFFFFFF;
}else TIM5CH2_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM5, TIM_IT_CC2) != RESET)
{
if(TIM5CH2_CAPTURE_STA&0X40)
{
TIM5CH2_CAPTURE_STA|=0X80;
TIM5CH2_CAPTURE_VAL=TIM_GetCapture2(TIM5);
TIM_OC2PolarityConfig(TIM5,TIM_ICPolarity_Rising);
}else
{
TIM5CH2_CAPTURE_STA=0;
TIM5CH2_CAPTURE_VAL=0;
TIM5CH2_CAPTURE_STA|=0X40;
TIM_Cmd(TIM5,DISABLE );
TIM_SetCounter(TIM5,0);
TIM_OC2PolarityConfig(TIM5,TIM_ICPolarity_Falling);
TIM_Cmd(TIM5,ENABLE );
}
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC2|TIM_IT_Update);
}
void TIM2_IRQHandler(void)
{
if((TIM2CH3_CAPTURE_STA&0X80)==0)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
if(TIM2CH3_CAPTURE_STA&0X40)
{
if((TIM2CH3_CAPTURE_STA&0X3F)==0X3F)
{
TIM2CH3_CAPTURE_STA|=0X80;
TIM2CH3_CAPTURE_VAL=0XFFFFFFFF;
}else TIM2CH3_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
{
if(TIM2CH3_CAPTURE_STA&0X40)
{
TIM2CH3_CAPTURE_STA|=0X80;
TIM2CH3_CAPTURE_VAL=TIM_GetCapture3(TIM2);
TIM_OC3PolarityConfig(TIM2,TIM_ICPolarity_Rising);
}else
{
TIM2CH3_CAPTURE_STA=0;
TIM2CH3_CAPTURE_VAL=0;
TIM2CH3_CAPTURE_STA|=0X40;
TIM_Cmd(TIM2,DISABLE );
TIM_SetCounter(TIM2,0);
TIM_OC3PolarityConfig(TIM2,TIM_ICPolarity_Falling);
TIM_Cmd(TIM2,ENABLE );
}
}
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3|TIM_IT_Update);
if((TIM2CH4_CAPTURE_STA&0X80)==0)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
if(TIM2CH4_CAPTURE_STA&0X40)
{
if((TIM2CH4_CAPTURE_STA&0X3F)==0X3F)
{
TIM2CH4_CAPTURE_STA|=0X80;
TIM2CH4_CAPTURE_VAL=0XFFFFFFFF;
}else TIM2CH4_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET)
{
if(TIM2CH4_CAPTURE_STA&0X40)
{
TIM2CH4_CAPTURE_STA|=0X80;
TIM2CH4_CAPTURE_VAL=TIM_GetCapture4(TIM2);
TIM_OC4PolarityConfig(TIM2,TIM_ICPolarity_Rising);
}else
{
TIM2CH4_CAPTURE_STA=0;
TIM2CH4_CAPTURE_VAL=0;
TIM2CH4_CAPTURE_STA|=0X40;
TIM_Cmd(TIM2,DISABLE );
TIM_SetCounter(TIM2,0);
TIM_OC4PolarityConfig(TIM2,TIM_ICPolarity_Falling);
TIM_Cmd(TIM2,ENABLE );
}
}
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4|TIM_IT_Update);
}
我也仿照原子例程写了定时器2的输入捕获程序,但也不好用,你在代码哪里错了啊?我还是没懂,
都为通用定时器,怎么又有什么大差别呢?
一周热门 更多>