|
上图为本人设计的双BLCD控制系统,主要是通过2.4G遥控器来控制直流无刷电机的转速。现欲实现转速闭环控制,但在速度的采集上出现了问题,速度的采集原理大概是通过定时器捕捉中断,读取此时的捕捉值,但定时器的捕捉值不稳定,偏差太大,根本无法实现速度的采集。代码如下,希望大家看看问题出在了哪,谢谢
捕捉中断处理函数
void TIM4_IRQHandler()
{
static uint8_t R_Count=0;
DriveR_Motor();
if(TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET) //溢出中断
{
R_Overflow++;
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET)
{
if(TIM4->CCER&0x0002)
{
TIM4->CCER&=0xfffD;
R_SpeedValData+=TIM_GetCapture1(TIM4)+(R_Overflow*65535); //读取捕捉值
R_Overflow=0;
TIM_SetCounter(TIM4,0);
R_Count++;
if(R_Count>200)
{
R_Count=0;
R_SpeedVal=R_SpeedValData/200;
R_SpeedValData=0;
}
}
else
{
TIM4->CCER|=0x0002;
}
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
if(TIM4->CCER&0x0020)
{
TIM4->CCER&=0xffdf;
}
else
{
TIM4->CCER|=0x0020;
}
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC3);
if(TIM4->CCER&0x0200)
{
TIM4->CCER&=0xfDFf;
}
else
{
TIM4->CCER|=0x0200;
}
}
}
定时器初始化函数
TIM_TimeBaseStructure.TIM_Period = 0xffff;
TIM_TimeBaseStructure.TIM_Prescaler = 27;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* TIM2 configuration in Input Capture Mode */
TIM_ICStructInit(&TIM_ICInitStructure);
//TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_PWMI;
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 = 0;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
// TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
TIM_Cmd(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_Update|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4, ENABLE);
/* TIM4 configuration in Input Capture Mode */
TIM_ICStructInit(&TIM_ICInitStructure);
//TIM_ICInitStructure.TIM_ICMode = TIM_ICMode_PWMI;
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 = 0;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
// TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);
TIM_Cmd(TIM4, ENABLE);
TIM_ITConfig(TIM4, TIM_IT_Update|TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3, ENABLE); |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|