项目内容:本人测试固定时间内2个同频率方波(频率约为:3KHz,占空比约为:50%,相位差约为:25%)的占空比、周期及相位差,固定时间约为1000个周期,且知道方波A超前于方波B。通过PWM输入模式测量方波A和方波B的占空比及周期,在方波A的上升沿中断时开启定时器,在方波B的上升沿中断时停止定时器,用于测量两者的相位差。
处理器:STM32F103ZET6,系统时钟72M
问题:占空比、周期测量正常,相位差测量偶尔会出现异常,异常一般出现在开始测量的第一个相位差,希望大神帮忙分析?
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Input Capture callback in non blocking mode
* @param htim : TIM IC handle
* @retval None
*/
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(htim);
/* NOTE : This function Should not be modified, when the callback is needed,
the __HAL_TIM_IC_CaptureCallback could be implemented in the user file
*/
if(htim==&htim3)
{
//CH1上升沿中断
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
switch(a)
{
case 0:
HAL_TIM_IC_Start_IT(&htim4,TIM_CHANNEL_1);//开启方波B的PWM输入模式
HAL_TIM_IC_Start_IT(&htim4,TIM_CHANNEL_2);//开启方波B的PWM输入模式
a=1;
break;
case 1:
IC1Value1 = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_1);
HAL_TIM_Base_Start(&htim6);
duty_A = IC1Value2*1.0/IC1Value1;
duty_Asum+=duty_A;
if(flag_z==1)//规定时间到后停止测量
{
HAL_TIM_IC_Stop_IT(&htim3,TIM_CHANNEL_1);
HAL_TIM_IC_Stop_IT(&htim3,TIM_CHANNEL_2);
condition++;
}
if((duty_A<0.40) | (duty_A>0.60))//方波A的占空比不符合条件
{
HAL_GPIO_WritePin(GPIOG, LED3_Pin, GPIO_PIN_RESET);//LED报警
//condition=0;
}
break;
}
}
//CH2下降沿中断
else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
IC1Value2 = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_2);
A++;
}
}
else if(htim==&htim4)
{
if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
{
switch(b)
{
case 0:b=1;break;
case 1:
IC1Value3 = HAL_TIM_ReadCapturedValue(&htim4, TIM_CHANNEL_1);
HAL_TIM_Base_Stop(&htim6);
duty_B = IC1Value4*1.0/IC1Value3;
duty_Bsum+=duty_B;
IC1ValueAB=htim6.Instance->CNT;
phase_AB = IC1ValueAB*1.0/IC1Value1;
phase_ABsum+=phase_AB;
htim6.Instance->CNT=0;
if(flag_z==1)//规定时间到后停止测量
{
HAL_TIM_IC_Stop_IT(&htim4,TIM_CHANNEL_1);
HAL_TIM_IC_Stop_IT(&htim4,TIM_CHANNEL_2);
condition++;
}
if((duty_B < 0.40)|(duty_B > 0.60))//方波B的占空比不符合条件
{
HAL_GPIO_WritePin(GPIOG,LED4_Pin,GPIO_PIN_RESET);//LED4报警
//condition=0;
}
else if((phase_AB < 0.20)|(phase_AB > 0.30))//相位差不符合条件
{
phase=phase_AB;
HAL_GPIO_WritePin(GPIOC,LED5_Pin,GPIO_PIN_RESET);//LED5报警
//condition=0;
}
break;
}
}
//CH2下降沿中断
else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
{
IC1Value4 = HAL_TIM_ReadCapturedValue(&htim4, TIM_CHANNEL_2);
B++;
}
}
}
|