STM32驱动42步进电机启动时出现卡顿
RT。用一个THB6128驱动42步进电机,但是每次启动总会有三次卡顿,示波器看出来有三个时间段没有输出,看起来象是单片机被复位。
在启动的时候又没法连接上单片机,无法用串口看出来,也无法确定。坛友有没有遇到此样问题
电机初始化如下
void TIM2_Int_Init(u16 arr,u16 psc)
{
RCC->APB1ENR |= RCC_APB1Periph_TIM2; //TIM2时钟使能
TIM2->ARR=arr; //设定计数器自动重装值
TIM2->PSC=psc; //预分频器7200
TIM2->DIER |= TIM_IT_Update; //允许更新中断
// TIM2->CR1|=TIM_ENABLE; //使能定时器2,向上计数模式
TIM2->CR1&=TIM_DISABLE;
MY_NVIC_Init(2,1,TIM2_IRQChannel,2); //组2(4组抢占(0,1,2,3),4组优先(0,1,2,3)),次高抢占级,次高优先级
// MotParY.Load_T= arr;
}
void TIM2_IRQHandler(void)
{
if(TIM2->SR&TIM_IT_Update)//溢出中断
{
TIM2->SR &=~TIM_IT_Update ;//清除中断标志位
TIM2->ARR = MotorPara.Load_T;
MotorClockOut();
}
}
void MotorInit(void)
{
RCC->APB2ENR |= (RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD) ; //使能PORTA,PORTC时钟
GPIOA->CRL &= (GPIO_Crl_P6 & GPIO_Crl_P7);
GPIOA->CRL |= ( GPIO_Mode_Out_PP_50MHz_P6 | GPIO_Mode_Out_PP_50MHz_P7);
GPIOA->ODR |= (GPIO_Pin_6 | GPIO_Pin_7); //初始化输出高
GPIOC->CRL &= (GPIO_Crl_P4);
GPIOC->CRL |= (GPIO_Mode_Out_PP_50MHz_P4);
GPIOC->ODR |= (GPIO_Pin_4); //初始化输出高
MotorOff();
TIM2_Int_Init(4687,71); //1转/分钟的速度,64细分后每个脉冲为4687.5us
// Port_THB6128_DIR_Y = MOVE_OPPOSITE;
Port_THB6128_DIR_Y = MOVE_POSITIVE;
}
void MotorClockOut(void)
{
Port_THB6128_Step_Y = 0;
if(Y_ORIGIN_RACK_OFF()) //开口光栅
{
MotorPara.EncoderStep_H++;
if(MotorPara.Sta != SIGNAL_HIGH) //开口
{
MotorPara.Sta= SIGNAL_HIGH;
if(MotorPara.EncoderStep_L>= 445 &&MotorPara.EncoderStep_L<= 464)
{
MotorPara.OriginalSta = 1;
}
else
{
// ++error;
}
MotorPara.EncoderStep_L = 0;
}
}
else //闭口光栅
{
MotorPara.EncoderStep_L++;
if(MotorPara.Sta != SIGNAL_LOW) //实口
{
MotorPara.Sta= SIGNAL_LOW;
if(MotorPara.EncoderStep_H >= 1525 &&MotorPara.EncoderStep_H <= 1545
&& MotorPara.OriginalSta == 1)
{
MotorPara.OriginalSta = 3;
}
else if(MotorPara.EncoderStep_H < 1525 &&MotorPara.EncoderStep_H > 1545)
{
}
MotorPara.EncoderStep_H = 0;
}
}
Port_THB6128_Step_Y = 1;
//每1%加减调整速度
if(MotorPara.PulseCnt)
{
if(MotorPara.PulseCnt > MotorPara.Speed)
{
if(MotorPara.PulseCnt > MOVE_SPEED_Start_cnt_Y)
{
MotorPara.PulseCnt *= 99;
MotorPara.PulseCnt /= 100;
if(MotorPara.PulseCnt < MotorPara.Speed)
{
MotorPara.PulseCnt = MotorPara.Speed;
}
}
else
{
MotorPara.PulseCnt--;
}
}
else if(MotorPara.PulseCnt < MotorPara.Speed)
{
if(MotorPara.PulseCnt > (MOVE_SPEED_Start_cnt_Y*3))
{
MotorPara.PulseCnt *= 101;
MotorPara.PulseCnt /= 100;
if(MotorPara.PulseCnt > MotorPara.Speed)
{
MotorPara.PulseCnt = MotorPara.Speed;
}
}
else
{
MotorPara.PulseCnt++;
}
}
}
}
int main(void)
{
Stm32_Clock_Init(9); //系统时钟设置
uart1_init(72,19200); //串口初始化为19200
delay_init(72); //延时初始化
LED_Init(); //初始化与LED连接的硬件接口
MotorInit();
MotorPara.EncoderStep_H = 0;
MotorPara.EncoderStep_L = 0;
MotorPara.OriginalSta = 0;
MotorParaX.EncoderStep_H = 0;
MotorParaX.EncoderStep_L = 0;
MotorParaX.OriginalSta = 0;
delay_ms(2000);
MotorSpeedTransfer(50);
MotorOn();
while(1)
{
MotoSpeedCalculate();
}
}
加速时,注意别超过步进电机能接受稳定加速的频率 本帖最后由 myqiang1990 于 2015-6-12 11:46 编辑
如果是上电卡一下或者动一下,完后正常运行,说明是步进电机上电的时候在找力矩的平衡点~是正常的啊~因为开电的时候步进电机并不一定是在整步的位置上~可以能是任意位置~ myqiang1990 发表于 2015-6-12 11:44
如果是上电卡一下或者动一下,完后正常运行,说明是步进电机上电的时候在找力矩的平衡点~是正常的啊~因为开 ...
并不是启动时的震动,而是启动时,转一下,顿,再转,顿,再转,顿,正常常常常常常常常常常常常常常常常常常常常常常
每次上电总会重复以上动作。
现在暂时的解决方法是,直接加延时5秒启动,避开那段时间。
但是问题还是没有找到,纠结
页:
[1]