STM32控制二相步进电机的驱动程序(串口CRC校验、有步进细分、指数加速等算法)

2019-07-21 06:10发布

[mw_shl_code=c,true]/****************************************************************************** *????????:static void Motor1PointerControl(void) *???????÷:???ú1???????? *????????:?? *????????:?? *******************************************************************************/ static void Motor1PointerControl(void) //100us*32 == 3.2ms LOOP in Time0 { static u16 iStop = 0; //???? //----------------------------------------------------------------------DmxFinedataUser if ((Motor1_postion_Current == Motor1_postion_user)&&(!MotorXSpeedPos)) //?????±?°?????????è???????°??????????0 { //?????±?°???????????§?????????????ú?????? Motor1_user_postion_gap = 0; //???§?¨????????0 Motor1_dynamic_postion_gap = 0; //?????¨????????0 LineFlag = 2; //1 ???? 0 ???? 2 ???? (????) //-----------?????±?????????¨???????????ú??????±?---------------------------- if (iStop > 30) //655us/2*500 ?????±???÷????????1/4 {//????????2?????????÷???? Current_Attenuation(2); //???????ú???÷???????????? DECAY = 0; //???????ú???????÷?????????? TIM3->CCR2 = SinDataA[MotorPositionPointer]>>0; //Sin?¨??????A TIM3->CCR1 = SinDataB[MotorPositionPointer]>>0; //Sin?¨??????B TIM3->CCR4 = CosDataC[MotorPositionPointer]>>0; //Sin?¨??????C TIM3->CCR3 = CosDataD[MotorPositionPointer]>>0; //Sin?¨??????D } else { iStop++; //?????ó???÷???????? Current_Attenuation(1); //???????ú???÷???????????? DECAY = 0; //???????ú???????÷?????????? } } else { iStop = 0; //???????????? if (DecayXCurrentFlag) //?????±?°???÷????±ê??=1 { //??°???????2 Current_Attenuation(2); //???????ú???÷???????????? DECAY = 0; //???????ú???????÷?????????? TIM3->CCR2 = SinDataA[MotorPositionPointer]>>0; //Sin?¨??????A TIM3->CCR1 = SinDataB[MotorPositionPointer]>>0; //Sin?¨??????B TIM3->CCR4 = CosDataC[MotorPositionPointer]>>0; //Sin?¨??????C TIM3->CCR3 = CosDataD[MotorPositionPointer]>>0; //Sin?¨??????D } else { //·??ò???????????? Current_Attenuation(1); //???????ú???÷???????????? DECAY = 1; //???????ú???????÷?????????? TIM3->CCR2 = SinDataA[MotorPositionPointer]>>0; //Sin?¨??????A TIM3->CCR1 = SinDataB[MotorPositionPointer]>>0; //Sin?¨??????B TIM3->CCR4 = CosDataC[MotorPositionPointer]>>0; //Sin?¨??????C TIM3->CCR3 = CosDataD[MotorPositionPointer]>>0; //Sin?¨??????D } //---------??·?×?????----------------------------------------- if (Motor1_dir == 1) //???§??????·??ò ?????ò?± { Motor1_dir_Current = 1; //???ú?±?°·??ò??1???ò } else //???§??????·??ò ??·??ò?± { Motor1_dir_Current = 0; //???ú?±?°·??ò??0·??ò } //------------------------------------------------------------ if (Motor1_dir_Current == YES) //???????????ú ???ò×??? { //?????ò????×??? ???¨×??? MotorPositionPointer += MaxStep; //16*2; //???????ú??????????16 CounterMotorXStepPostionCurrent += 1; //???????ú?¨????????1 //-------------×??ê?????????ú????1024??????------------------------ if (MotorPositionPointer >= 1024) //?????????ó??1024?????? { MotorPositionPointer = 0; //??????????×??????ú?? } } else //·??ò???????????ú ·??ò×??? { MotorPositionPointer -= MaxStep; //16*2; //???????ú??????????16 CounterMotorXStepPostionCurrent -= 1; //???????ú?¨????????1 //-------------------------------------------------- if (MotorPositionPointer <= 0) //????????????????0????×??ó1024 { MotorPositionPointer = 1024; //???????????ú????×??ó?????? } } } //--------------------------------------------------------------------------------------------------------------- Motor1_postion_Current = CounterMotorXStepPostionCurrent; //°????????????±?°?????????÷ //?±?°×????ì?? ??????×????????÷ ?????±?°×??? //--------------------------------------------------------------------------------------------------------------- }[/mw_shl_code]
在淘宝上买的快300大洋的步进电机驱动,我把它精简成只有一路步进电机的驱动程序,还自己写了一个上位机软件通过串口来控制电机的粗调和细调功能,现在来原子贵坛分享下!
不过还有一个问题就是速度还不能满足我的项目需求,想调节步进电机的速度指数曲线的数组但是不懂怎么生成?
还望有做过的大神不吝赐教,谢谢!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
30条回答
wgh1990
1楼-- · 2019-07-21 11:06
 精彩回答 2  元偷偷看……
潜龙探渊
2楼-- · 2019-07-21 16:58
回复【2楼】wgh1990:
---------------------------------
 我也知道是改变那个指数曲线的数据,但是不知道那数据怎么计算出来的?有没有C语言的计算公式,还望大神帮帮我
正点原子
3楼-- · 2019-07-21 17:52
不错,谢谢分享。
潜龙探渊
4楼-- · 2019-07-21 23:38
回复【4楼】正点原子:
---------------------------------
谢谢原子支持!
lansing1648
5楼-- · 2019-07-22 03:46
楼主:程序和电路对不上。
        BI0 = 0;         BI1 = 0;         AI0 = 0;         AI1 = 0;  电路呢?不理解。
潜龙探渊
6楼-- · 2019-07-22 04:13
 精彩回答 2  元偷偷看……

一周热门 更多>