菜鸟一枚,用stm32f103同时捕获两路信号的频率和高电平宽度时,频率不对,脉冲宽度也偏小,不清楚是哪里问题,还请各位大神帮忙,程序如下:
void CAPTURE_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; /
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
/*频率捕获IO口配置*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_7);
}
void SetTIM4_NVIC(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM4_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure ;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 72-1;
TIM_TimeBaseStructure.TIM_Period = 0XFFFF;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up ;
TIM_TimeBaseStructure.TIM_ClockDivision =0 ;
TIM_TimeBaseInit( TIM4, &TIM_TimeBaseStructure);
/*频率捕获寄存器配置TIM1_Channel_1*/
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising ;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI ;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1 ;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit( TIM4, &TIM_ICInitStructure);
TIM_ARRPreloadConfig(TIM4,ENABLE);
TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE) ;
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
/*高电平脉宽寄存器配置TIM1_Channel_2*/
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising ;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI ;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1 ;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit( TIM4, &TIM_ICInitStructure);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE) ;
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE) ;
TIM_Cmd(TIM4, ENABLE);
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus( TIM4, TIM_IT_CC1)==SET)
{
/* Clear TIM4 update interrupt */
TIM_ClearITPendingBit(TIM4,TIM_IT_CC1);
if(Capture_number==0)
{
Ic1_Read_Value = TIM_GetCapture1(TIM4);
Capture_number = 1 ;
}
if(Capture_number==1)
{
Ic2_Read_Value= TIM_GetCapture1(TIM4) ;
if(Ic2_Read_Value>Ic1_Read_Value)
{
Capture = (Ic2_Read_Value-Ic1_Read_Value);
}
else
{
Capture = (65536 -Ic1_Read_Value + Ic2_Read_Value) ;
}
Capture_number = 0 ;
}
}
/*脉宽捕获*/
else if((TIM4CH2_CAPTURE_STA&0X8000)==0)
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
{
if(TIM4CH2_CAPTURE_STA&0X4000)
{
if((TIM4CH2_CAPTURE_STA&0X3FFF)==0X3FFF
{
TIM4CH2_CAPTURE_STA|=0X8000;
TIM4CH2_CAPTURE_VAL=0XFFFF;
}
else
TIM4CH2_CAPTURE_STA++;
}
}
else if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)/
{
if(TIM4CH2_CAPTURE_STA&0X4000)
{
TIM4CH2_CAPTURE_STA|=0X8000;
TIM4CH2_CAPTURE_VAL=TIM_GetCapture2(TIM4);
TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Rising);
}
else
{
TIM4CH2_CAPTURE_STA=0;
TIM4CH2_CAPTURE_VAL=0;
TIM_SetCounter(TIM4,0);
TIM4CH2_CAPTURE_STA|=0X4000;
TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Falling);
}
}
TIM_ClearITPendingBit(TIM4,TIM_IT_Update); //中断结束后,清除中断标志位
TIM_ClearITPendingBit(TIM4,TIM_IT_CC2); //中断结束后,清除中断标志位
}
/*脉宽捕获计算*/
if(TIM4CH2_CAPTURE_STA&0X8000)
{
temp=TIM4CH2_CAPTURE_STA&0X3FFF;
temp*=65536;
temp+=TIM4CH2_CAPTURE_VAL;
TIM4CH2_CAPTURE_STA=0;
}
}
|