单片机控制步进电机

单片机控制步进电机源码

硬件选型:

单片机:STM32开发板

stm32单片机开发板

步进电机及步进电机驱动控制器:谱思一体化步进电机

一体化步进电机

单片机控制步进电机源码


#include "drv_step_motor.h"

uint8_t receivecmd[8];//接收的回传信息

uint8_t mspreceivecmd[30];//接收回传信息

 

uint8_t receivecmdflag=0;//成功接收到回传信息标志位

uint8_t mspreceivecmdflag=0;

 

uint8_t sdata[8];//发送命令数据

uint8_t motorcheckstate;//单片机控制步进电机状态检测

uint8_t motorflag=0;//串口中断状态机调用标志位

uint8_t motorstates[7];//电机状态

 

uint32_t msp60_start=0;//延时时间开始时刻

uint8_t msp60_flag=0;

//复位信号输入口配置

static void Step_Motor_Reset_GPIO_Config(void)

{

    GPIO_InitTypeDef GPIO_InitStructure;

 

    /* GPIOA clock enable */

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

        

    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7;//IN1-IN2-IN3-IN4

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 

    GPIO_Init(GPIOA, &GPIO_InitStructure);

        

         GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_1;//IN5-IN6

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 

    GPIO_Init(GPIOB, &GPIO_InitStructure);

}

 

static uint8_t Step_Motor_Receive_Cmd(void)

{

    uint32_t i=0;

    while(receivecmdflag==0)

    {

        Delayms(1);

        if(i++>1000)

        {

            return 0;

        }

//               if(keynum==stop)break;

    }

    receivecmdflag=0;

    return 1;

}

 

static uint8_t Step_Motor_Send_Cmd(uint8_t id,uint8_t cmd,uint32_t cdata)

{

    uint8_t i=0;

    motorflag=0;

    sdata[0]=0xa5;

    sdata[1]=id;

    sdata[2]=cmd;

    sdata[3]=(uint8_t)cdata;

    sdata[4]=(uint8_t)(cdata>>8);

    sdata[5]=(uint8_t)(cdata>>16);

    sdata[6]=(uint8_t)(cdata>>24);

    sdata[7]=(uint8_t)(sdata[0]+sdata[1]+sdata[2]+sdata[3]+sdata[4]+sdata[5]+sdata[6]);

    Set_DE;

    Delayus(10);

    for(i=0; i<8; i++)

    {

        USART3_Send_Byte(sdata[i]);

    }

    Delayus(10);

    Clr_DE;

    return(Step_Motor_Receive_Cmd());

}

 

static void Step_Motor_Clear_Ext_Stop1(uint8_t id)//清除EXT_Stop1标志位

{

    Step_Motor_Send_Cmd(id,0x62,0);

}

 

static void Step_Motor_Clear_Ext_Stop2(uint8_t id)//清除EXT_Stop2标志位

{

    Step_Motor_Send_Cmd(id,0x6c,0);

}

 

static void Step_Motor_Mov(uint8_t id,uint32_t step)//运行步数1~0x7fffffff

{

    Step_Motor_Send_Cmd(id,0x73,(uint32_t)step);

 

}

 

static void Step_Motor_Set_Dir(uint8_t id,uint8_t dir)//设置运行方向

{

    Step_Motor_Send_Cmd(id,0x64,(uint32_t)dir);

}

 

static void Step_Motor_Set_Speed(uint8_t id,uint16_t speed)//设置速度1~16000

{

    Step_Motor_Send_Cmd(id,0x76,(uint32_t)speed);

}

 

static void Step_Motor_Set_Sub(uint8_t id,uint8_t sub)//设置细分0/2/4/8/16/32/64/128

{

    Step_Motor_Send_Cmd(id,0x6d,(uint32_t)sub);

}

 

static void Step_Motor_Stop_Mov(uint8_t id)//单片机控制步进电机停止运行

{

    Step_Motor_Send_Cmd(id,0x49,0x00);

}

 

static void Step_Motor_Set_IO(uint8_t id,uint8_t io)//IO输出

{

    uint8_t iotemp=1;

    Step_Motor_Send_Cmd(id,0x79,(uint32_t)(iotemp<<=(io-1)));

}

 

static uint8_t Step_Motor_Read_IO(uint8_t id)//IO状态读取

{

    Step_Motor_Send_Cmd(id,0x78,0x00);

    Step_Motor_Receive_Cmd();

    return receivecmd[4];

}

 

static void Step_Motor_Set_Speed_Mod(uint8_t id,uint8_t state)//是否使能速度模式

{

    Step_Motor_Send_Cmd(id,0x4e,(uint32_t)state);

}

 

static uint8_t Step_Motor_Get_Motoring(uint8_t id,uint8_t reg)//读取控制寄存器状态

{

    Step_Motor_Send_Cmd(id,reg,(uint32_t)0x00);

    Step_Motor_Receive_Cmd();

    return receivecmd[3];

}

 

static void Step_Motor_Set_EXT(uint8_t id,uint8_t ext)//外部急停设置

{

    Step_Motor_Send_Cmd(id,0x66,(uint32_t)ext);

}

 

static uint16_t Step_Motor_Read_Position(uint8_t id)//读取位置

{

    Step_Motor_Send_Cmd(id,0x63,0x00);

    Step_Motor_Receive_Cmd();

    return ((receivecmd[4]<<8)+receivecmd[3]);

}

 

 

uint8_t Step_Motor_Read_State(uint8_t motor)//读取电机状态

{

                   uint8_t state=0;

        if(Step_Motor_Send_Cmd(motor,0x6a,0x00)==1)

        {

            state=receivecmd[3]&0x01;

        }                

                   return state;

}

void Step_Motor_Mov_Control(uint8_t motor,uint8_t dir,uint16_t speed,uint32_t step)

{

    switch(motor)

    {

    case my1:

        if(dir==cw)Step_Motor_Set_Dir(my1,1);

        else if(dir==ccw)Step_Motor_Set_Dir(my1,0);

        Step_Motor_Set_Speed(my1,speed);

 

        Step_Motor_Mov(my1,step);

 

//        Step_Motor_Send_Cmd(my1,0x6a,0x00);//每隔10ms读取一次电机运行是否完成

//        while((receivecmd[3]&0x01)==1)

//        {

//            Step_Motor_Send_Cmd(my1,0x6a,0x00);

//            Delayms(10);

//                         if(keynum==stop)break;

//        }

        break;

 

    case mx1:

        if(dir==cw)Step_Motor_Set_Dir(mx1,0);

        else if(dir==ccw)Step_Motor_Set_Dir(mx1,1);

        Step_Motor_Set_Speed(mx1,speed);

 

        Step_Motor_Mov(mx1,step);

//        Step_Motor_Send_Cmd(mx1,0x6a,0x00);

//        while((receivecmd[3]&0x01)==1)

//        {

//            Step_Motor_Send_Cmd(mx1,0x6a,0x00);

//            Delayms(10);

//                        if(keynum==stop)break;

//        }

        break;

 

    case my2:

        if(dir==cw)

        {

            Step_Motor_Set_Dir(my2,0);

        }

        else if(dir==ccw)

        {

            Step_Motor_Set_Dir(my2,1);

        }

        Step_Motor_Set_Speed(my2,speed);

 

        Step_Motor_Mov(my2,step);

 

//        Step_Motor_Send_Cmd(my2,0x6a,0x00);

//        while((receivecmd[3]&0x01)==1)

//        {

//            Step_Motor_Send_Cmd(my2,0x6a,0x00);

//            Delayms(10);

//                                                     if(keynum==stop)break;

//        }

        break;

 

    case pump1:

        if(dir==cw)Step_Motor_Set_Dir(pump1,0);

        else if(dir==ccw)Step_Motor_Set_Dir(pump1,1);

        Step_Motor_Set_Speed(pump1,speed);

 

        Step_Motor_Mov(pump1,step);

//        Step_Motor_Send_Cmd(pump1,0x6a,0x00);

//        while((receivecmd[3]&0x01)==1)

//        {

//            Step_Motor_Send_Cmd(pump1,0x6a,0x00);

//            Delayms(10);

//                         if(keynum==stop)break;

//        }

        break;

 

    case pump2:

        if(dir==cw)Step_Motor_Set_Dir(pump2,0);

        else if(dir==ccw)Step_Motor_Set_Dir(pump2,1);

        Step_Motor_Set_Speed(pump2,speed);

 

        Step_Motor_Mov(pump2,step);

//        Step_Motor_Send_Cmd(pump2,0x6a,0x00);

//        while((receivecmd[3]&0x01)==1)

//        {

//            Step_Motor_Send_Cmd(pump2,0x6a,0x00);

//            Delayms(10);

//                                                     if(keynum==stop)break;

//        }

        break;

   

    case mr1:

        if(dir==cw)Step_Motor_Set_Dir(mr1,0);

        else if(dir==ccw)Step_Motor_Set_Dir(mr1,1);

        Step_Motor_Set_Speed(mr1,speed);

 

        Step_Motor_Mov(mr1,step);

//        Step_Motor_Send_Cmd(pump2,0x6a,0x00);

//        while((receivecmd[3]&0x01)==1)

//        {

//            Step_Motor_Send_Cmd(pump2,0x6a,0x00);

//            Delayms(10);

//                                                     if(keynum==stop)break;

//        }

        break;

    case mr2:

        if(dir==cw)Step_Motor_Set_Dir(mr2,0);

        else if(dir==ccw)Step_Motor_Set_Dir(mr2,1);

        Step_Motor_Set_Speed(mr2,speed);

 

        Step_Motor_Mov(mr2,step);

//        Step_Motor_Send_Cmd(pump2,0x6a,0x00);

//        while((receivecmd[3]&0x01)==1)

//        {

//            Step_Motor_Send_Cmd(pump2,0x6a,0x00);

//            Delayms(10);

//                                                     if(keynum==stop)break;

//        }

        break;

    default :

 

        break;

    }

}

void Step_Motor_MX1_Reset(void)

{

    if(IN2==0)

    {

        Step_Motor_Set_Dir(mx1,cw);

        Step_Motor_Set_Speed(mx1,500);

        Step_Motor_Mov(mx1,1000);

        while(IN2==0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(mx1);

       

        Step_Motor_Mov_Control(mx1,cw,500,100);

        Step_Motor_Send_Cmd(mx1,0x6a,0x00);//每隔10ms读取一次电机运行是否完成

        while((receivecmd[3]&0x01)==1)

        {

            Step_Motor_Send_Cmd(mx1,0x6a,0x00);

            Delayms(10);

                            if(keynum==stop)break;

        }

    }

    else if(IN2!=0)

    {

        Step_Motor_Set_Dir(mx1,ccw);

        Step_Motor_Set_Speed(mx1,3000);

        Step_Motor_Mov(mx1,65000);

        while(IN2!=0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(mx1);

        Step_Motor_Set_Dir(mx1,cw);

        Step_Motor_Set_Speed(mx1,500);

        Step_Motor_Mov(mx1,1000);

        while(IN2==0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(mx1);

        Step_Motor_Mov_Control(mx1,cw,500,100);

        Step_Motor_Send_Cmd(mx1,0x6a,0x00);//每隔10ms读取一次电机运行是否完成

        while((receivecmd[3]&0x01)==1)

        {

            Step_Motor_Send_Cmd(mx1,0x6a,0x00);

            Delayms(10);

                            if(keynum==stop)break;

        }

    }

    mresetflag[mx1]=1;

    Delayms(10);

}

 

void Step_Motor_MY1_Reset(void)

{

    if(IN1==0)

    {

        Step_Motor_Set_Dir(my1,1);

        Step_Motor_Set_Speed(my1,500);

        Step_Motor_Mov(my1,1000);

        while(IN1==0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(my1);

        Step_Motor_Mov_Control(my1,cw,500,100);

        Step_Motor_Send_Cmd(my1,0x6a,0x00);//每隔10ms读取一次电机运行是否完成

        while((receivecmd[3]&0x01)==1)

        {

            Step_Motor_Send_Cmd(my1,0x6a,0x00);

            Delayms(10);

                            if(keynum==stop)break;

        }

    }

    else if(IN1!=0)

    {

        Step_Motor_Set_Dir(my1,0);

        Step_Motor_Set_Speed(my1,3000);

        Step_Motor_Mov(my1,65000);

        while(IN1!=0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(my1);

        Step_Motor_Set_Dir(my1,1);

        Step_Motor_Set_Speed(my1,500);

        Step_Motor_Mov(my1,1000);

        while(IN1==0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(my1);

        Step_Motor_Mov_Control(my1,cw,500,100);

        Step_Motor_Send_Cmd(my1,0x6a,0x00);//每隔10ms读取一次电机运行是否完成

        while((receivecmd[3]&0x01)==1)

        {

            Step_Motor_Send_Cmd(my1,0x6a,0x00);

            Delayms(10);

                            if(keynum==stop)break;

        }

    }

    mresetflag[my1]=1;

    Delayms(10);

}

void Step_Motor_MY2_Reset(void)

{

    if(IN3==0)

    {

        Step_Motor_Set_Dir(my2,cw);

        Step_Motor_Set_Speed(my2,500);

 

        Step_Motor_Mov(my2,1000);

        while(IN3==0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(my2);

        Step_Motor_Mov_Control(my2,cw,500,50);

        Step_Motor_Send_Cmd(my2,0x6a,0x00);//每隔10ms读取一次电机运行是否完成

        while((receivecmd[3]&0x01)==1)

        {

            Step_Motor_Send_Cmd(my2,0x6a,0x00);

            Delayms(10);

                            if(keynum==stop)break;

        }

    }

    else if(IN3!=0)

    {

        Step_Motor_Set_Dir(my2,ccw);

        Step_Motor_Set_Speed(my2,3000);

        Step_Motor_Mov(my2,65000);

        while(IN3!=0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(my2);

 

        Step_Motor_Set_Dir(my2,cw);

        Step_Motor_Set_Speed(my2,500);

        Step_Motor_Mov(my2,1000);

        while(IN3==0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(my2);

        Step_Motor_Mov_Control(my2,cw,500,50);

        Step_Motor_Send_Cmd(my2,0x6a,0x00);//每隔10ms读取一次电机运行是否完成

        while((receivecmd[3]&0x01)==1)

        {

            Step_Motor_Send_Cmd(my2,0x6a,0x00);

            Delayms(10);

                            if(keynum==stop)break;

        }

    }

    mresetflag[my2]=1;

    Delayms(10);

}

void Step_Motor_MR1_Reset(void)

{

    if(IN6==0)

    {

        Step_Motor_Set_Dir(mr1,cw);

        Step_Motor_Set_Speed(mr1,500);

 

        Step_Motor_Mov(mr1,1000);

        while(IN6==0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(mr1);

        Step_Motor_Mov_Control(mr1,ccw,500,250);

        Step_Motor_Send_Cmd(mr1,0x6a,0x00);//每隔10ms读取一次电机运行是否完成

        while((receivecmd[3]&0x01)==1)

        {

            Step_Motor_Send_Cmd(mr1,0x6a,0x00);

            Delayms(10);

                            if(keynum==stop)break;

        }

    }

    else if(IN6!=0)//未挡住光电

    {

        Step_Motor_Set_Dir(mr1,ccw);

        Step_Motor_Set_Speed(mr1,3000);

        Step_Motor_Send_Cmd(mr1,0x4e,1);

        //Step_Motor_Mov(mr1,65000);

        while(IN6!=0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Send_Cmd(mr1,0x4e,0);

//        Step_Motor_Stop_Mov(mr1);

 

        Step_Motor_Set_Dir(mr1,cw);

        Step_Motor_Set_Speed(mr1,500);

        Step_Motor_Mov(mr1,1000);

        while(IN6==0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(mr1);

        Step_Motor_Mov_Control(mr1,ccw,500,250);

        Step_Motor_Send_Cmd(mr1,0x6a,0x00);//每隔10ms读取一次电机运行是否完成

        while((receivecmd[3]&0x01)==1)

        {

            Step_Motor_Send_Cmd(mr1,0x6a,0x00);

            Delayms(10);

                            if(keynum==stop)break;

        }

    }

    mresetflag[mr1]=1;

    Delayms(10);

}

void Step_Motor_MR2_Reset(void)

{

    if(IN5==0)//光电里面

    {

        Step_Motor_Set_Dir(mr2,cw);

        Step_Motor_Set_Speed(mr2,500);

 

        Step_Motor_Mov(mr2,15000);

        while(IN5==0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(mr2);

        Step_Motor_Mov_Control(mr2,cw,500,50);

        Step_Motor_Send_Cmd(mr2,0x6a,0x00);//每隔10ms读取一次电机运行是否完成

        while((receivecmd[3]&0x01)==1)

        {

            Step_Motor_Send_Cmd(mr2,0x6a,0x00);

            Delayms(10);

                            if(keynum==stop)break;

        }

    }

    else if(IN5!=0)

    {

        Step_Motor_Set_Dir(mr2,ccw);

        Step_Motor_Set_Speed(mr2,3000);

        Step_Motor_Send_Cmd(mr2,0x4e,1);

//        Step_Motor_Mov(mr2,65000);

        while(IN5!=0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Send_Cmd(mr2,0x4e,0);

//        Step_Motor_Stop_Mov(mr2);

 

        //Step_Motor_Set_Dir(mr2,ccw);

        Step_Motor_Set_Dir(mr2,cw);

        Step_Motor_Set_Speed(mr2,500);

        Step_Motor_Mov(mr2,1000);

        while(IN5==0)

        {

            if(keynum==stop)break;

        }

        Step_Motor_Stop_Mov(mr2);

        Step_Motor_Mov_Control(mr2,cw,500,50);

        //Step_Motor_Mov_Control(mr2,ccw,500,50);

        Step_Motor_Send_Cmd(mr2,0x6a,0x00);//每隔10ms读取一次电机运行是否完成

        while((receivecmd[3]&0x01)==1)

        {

            Step_Motor_Send_Cmd(mr2,0x6a,0x00);

            Delayms(10);

                            if(keynum==stop)break;

        }

    }

//    Step_Motor_Mov_Control(mr2,ccw,500,2000);

    mresetflag[mr2]=1;

    Delayms(10);

}

uint8_t Step_Motor_Check(void)

{

    uint8_t i=0,j=0;

//    Step_Motor_Send_Cmd(5,0x77,6);

    for(i=0; i<7; i++)

    {

        if(Step_Motor_Send_Cmd(i,0x6a,0x00)==1)

        {

            j=j|(1<<i);

        }

    }

    return j;

}

void Step_Motor_Init(void)

{

 

    Step_Motor_Reset_GPIO_Config();

 

    Step_Motor_Send_Cmd(0,0x6d,8);//单片机控制步进电机设置细分8

    Delayms(2);

    Step_Motor_Send_Cmd(1,0x6d,8);

    Delayms(2);

    Step_Motor_Send_Cmd(2,0x6d,8);

    Delayms(2);

    Step_Motor_Send_Cmd(3,0x6d,8);

    Delayms(2);

    Step_Motor_Send_Cmd(4,0x6d,8);//蠕动泵2

    Delayms(2);

    Step_Motor_Send_Cmd(5,0x6d,8);//蠕动泵2

    Delayms(2);

    Step_Motor_Send_Cmd(6,0x6d,8);//蠕动泵2

    Delayms(2);

   

    Step_Motor_Send_Cmd(0,0x74,1);//设置加减速使能

    Delayms(2);

    Step_Motor_Send_Cmd(1,0x74,1);

    Delayms(2);

    Step_Motor_Send_Cmd(2,0x74,1);

    Delayms(2);

//    Step_Motor_Send_Cmd(3,0x74,1);

//    Delayms(2);

    Step_Motor_Send_Cmd(4,0x74,1);

    Delayms(2);

    Step_Motor_Send_Cmd(5,0x74,1);

    Delayms(2);

    Step_Motor_Send_Cmd(6,0x74,1);

    Delayms(2);

   

    Step_Motor_Send_Cmd(0,0x61,1);//设置自动电流衰减使能

    Delayms(2);

    Step_Motor_Send_Cmd(1,0x61,1);

    Delayms(2);

    Step_Motor_Send_Cmd(2,0x61,1);

    Delayms(2);

    Step_Motor_Send_Cmd(3,0x61,1);

    Delayms(2);

    Step_Motor_Send_Cmd(4,0x61,1);

    Delayms(2);

    Step_Motor_Send_Cmd(5,0x61,1);

    Delayms(2);

    Step_Motor_Send_Cmd(6,0x61,1);

    Delayms(2);

   

    Step_Motor_Send_Cmd(0,0x75,3);//设置加减速系数

    Delayms(2);

    Step_Motor_Send_Cmd(1,0x75,3);

    Delayms(2);

    Step_Motor_Send_Cmd(2,0x75,3);

    Delayms(2);

    Step_Motor_Send_Cmd(3,0x75,3);

    Delayms(2);

    Step_Motor_Send_Cmd(3,0x6f,3);

    Delayms(2);

    Step_Motor_Send_Cmd(4,0x75,6);

    Delayms(2);

    Step_Motor_Send_Cmd(5,0x75,6);

    Delayms(2);

    Step_Motor_Send_Cmd(6,0x75,6);

    Delayms(2);

   

    Step_Motor_Send_Cmd(0,0x4c,100);//设置启动速度

    Delayms(2);

    Step_Motor_Send_Cmd(1,0x4c,100);

    Delayms(2);

    Step_Motor_Send_Cmd(2,0x4c,100);

    Delayms(2);

    Step_Motor_Send_Cmd(3,0x4c,100);

    Delayms(2);

    Step_Motor_Send_Cmd(4,0x4c,300);

    Delayms(2);

    Step_Motor_Send_Cmd(5,0x4c,300);

    Delayms(2);

    Step_Motor_Send_Cmd(6,0x4c,300);

    Delayms(2);

   

    Step_Motor_Set_Speed(0,3000);//设置速度

    Delayms(2);

    Step_Motor_Set_Speed(1,3000);

    Delayms(2);

    Step_Motor_Set_Speed(2,3000);

    Delayms(2);

    Step_Motor_Set_Speed(3,1200);

    Delayms(2);

    Step_Motor_Set_Speed(4,1200);

    Delayms(2);

    Step_Motor_Set_Speed(5,4500);

    Delayms(2);

    Step_Motor_Set_Speed(6,4500);

    Delayms(2);

   

    Step_Motor_Set_EXT(0,0);//失能外部急停

    Delayms(2);

    Step_Motor_Set_EXT(1,0);

    Delayms(2);

    Step_Motor_Set_EXT(2,0);

    Delayms(2);

    Step_Motor_Set_EXT(3,0);

    Delayms(2);

    Step_Motor_Set_EXT(4,0);

    Delayms(2);

    Step_Motor_Set_EXT(5,0);

    Delayms(2);

    Step_Motor_Set_EXT(6,0);

    Delayms(2);

   

    Step_Motor_Set_Dir(0,0);//设置方向

    Delayms(2);

    Step_Motor_Set_Dir(1,0);

    Delayms(2);

    Step_Motor_Set_Dir(2,1);

    Delayms(2);

    Step_Motor_Set_Dir(3,1);

    Delayms(2);

    Step_Motor_Set_Dir(4,1);

    Delayms(2);

    Step_Motor_Set_Dir(5,1);

    Delayms(2);

    Step_Motor_Set_Dir(6,1);

    Delayms(2);

   

    Step_Motor_Send_Cmd(0,0x65,1500);//设置最大相电流mA

    Delayms(2);

    Step_Motor_Send_Cmd(1,0x65,1500);

    Delayms(2);

    Step_Motor_Send_Cmd(2,0x65,1000);

    Delayms(2);

    Step_Motor_Send_Cmd(3,0x65,1500);

    Delayms(2);

    Step_Motor_Send_Cmd(4,0x65,1500);

    Delayms(2);

    Step_Motor_Send_Cmd(5,0x65,1500);

    Delayms(2);

    Step_Motor_Send_Cmd(6,0x65,1500);

    Delayms(2);

 

    Step_Motor_Send_Cmd(0,0x50,1000);//设置高速模式切换速度

    Delayms(2);

    Step_Motor_Send_Cmd(1,0x50,1000);

    Delayms(2);

    Step_Motor_Send_Cmd(2,0x50,1000);

    Delayms(2);

    Step_Motor_Send_Cmd(3,0x50,1000);

    Delayms(2);

    Step_Motor_Send_Cmd(4,0x50,1000);

    Delayms(2);

    Step_Motor_Send_Cmd(5,0x50,1000);

    Delayms(2);

    Step_Motor_Send_Cmd(6,0x50,1000);

    Delayms(2);

 

}

 

void Msp60_Reset(void)

{

    motorflag=1;

    Set_DE;

    Delayus(100);

    USART3_Send_String("/1Z14R\r\n");

    Clr_DE;

    Delayms(13000);

}

 

void Msp60_Set_Radiotube(uint8_t state)

{

    motorflag=1;

    if(state==open)

    {

        Set_DE;

        Delayus(100);

        USART3_Send_String("/1OR\r\n");

        Clr_DE;

    }

    else if(state==close)

    {

        Set_DE;

        Delayus(100);

        USART3_Send_String("/1IR\r\n");

        Clr_DE;

    }

    Delayms(20);

}

 

void Msp60_Mov_Control(char *speed,char *position)

{

 

    char fhs[]="/1S",fhp[]="/1A",fe[]="R\r\n";

    char spe[9];

    char pos[11];

 

    motorflag=1;

    memcpy(spe,fhs,3);

    memcpy(spe+3,speed,2);

    memcpy(spe+5,fe,strlen(fe));

 

    Set_DE;

    Delayus(100);

    USART3_Send_String(spe);

    Clr_DE;

    Delayms(5);

 

    memcpy(pos,fhp,3);

    memcpy(pos+3,position,4);

    memcpy(pos+7,fe,strlen(fe));

    Set_DE;

    Delayus(100);

    USART3_Send_String(pos);

    Clr_DE;

    Delayms(5);

//               Msp60_Send_Cmd("/1Q\r\n");

//               while(mspreceivecmd[2]!=0x60)

//               {

//                                  Msp60_Send_Cmd("/1Q\r\n");

//                                  Delayms(10);

//               }

}

void Msp60_Send_Cmd(char *step)

{

    motorflag=1;

    Set_DE;

    Delayus(100);

    USART3_Send_String(step);

    Clr_DE;

}

 

uint8_t Msp60_Delay_nms(uint32_t ms)

{

    uint32_t now_time=0;

    if(msp60_flag==0)

    {

        msp60_start=time;

        msp60_flag=1;

    } 

    else if(msp60_flag==1)

    {

        now_time=time;

        if(now_time>=(msp60_start+ms))

        {

            msp60_flag=0;

            return 1;

        }

    }

    return 0;

}

void Step_Motor_Stop(void)//停止所有动作

{

    Step_Motor_Stop_Mov(my1);

    Step_Motor_Stop_Mov(mx1);

    Step_Motor_Stop_Mov(mr1);

    Step_Motor_Stop_Mov(mr2);

    Step_Motor_Stop_Mov(my2);

    Step_Motor_Stop_Mov(pump1);

         Step_Motor_Stop_Mov(pump2);

//      Msp60_Send_Cmd("/1TR\r\n");

 

 

}

 

//查询电机运行状态

void Step_Motor_States(void)

{

    uint8_t i=0;

    memset(motorstates,0,sizeof(motorstates));

    for(i=0;i<5;i++)

    {

        if(Step_Motor_Send_Cmd(i,0x6a,0x00)==1)

        {

            if((receivecmd[3]&0x01)==1)

            {

                motorstates[i]=1;

            }

            else

            {

                motorstates[i]=0;

            }

        }

    }

}

 

void Set_ID(uint8_t old_id,uint8_t new_id)

{

    Step_Motor_Send_Cmd(old_id,0x77,new_id);

}

以上为完整的基于STM32单片机控制步进电机源码