[活动专区] 【赛元易码魔盒】SC95F8616驱动步进电机

[复制链接]
1392|0
 楼主| jinglixixi 发表于 2020-7-5 10:30 | 显示全部楼层 |阅读模式
本帖最后由 jinglixixi 于 2020-7-5 10:44 编辑

步进电机是一种常用的执行器件,使用它能进行精准的定位及转速调节等工作。一个简单的步进电机驱动要由步进电机、驱动模块及单片机SC95F8616构成,见下图所示。

1.png
步进电机驱动的构成图

为了驱动步进电机,除了硬件方面的准备,还需必要的软件配合。在易码魔盒中,就提供了步进电机的外设驱动。
由于刚开始学习用易码魔盒来设计程序,便打算在这方面练练手。
起初试了许多次,都一直没有成功,就打算用手工编程的方法先让步进电机转起来,然后再与易码魔盒生成的程序进行对比来发现问题。
要手工编写步进电机驱动程序主要分为以下几步:
1)分配引脚的使用
以步进电机的MA~ MD引脚分别与P32~ P35相连接
sbit MA       =   P3^2;
sbit MB       =   P3^3;
sbit MC       =   P3^4;
sbit MD       =   P3^5;

2)定义输出高低电平的宏定义
#define MAL    MA  =   0;
#define MAH    MA  =  1;
#define MBL    MB  =   0;
#define MBH    MB  =   1;
#define MCL    MC  =   0;
#define MCH    MC  =   1;
#define MDL    MD  =   0;
#define MDH    MD  =   1;

3)配置延时函数
配置延时函数的工作在步进电机的驱动中十分重要,它控制着步进电机运转的快慢。
但问题也正出在这里,对于步进电机来说其速度是与驱动脉冲有关,但也并非脉冲越快,电机的转速越快。因为电机电机属于机械器件,
要建立相应的磁场及达到相应的扭矩才能使电机转动。过快的脉冲只会使电机在原地震颤和抖动,并无法旋转。
使电机转动的延时函数为:
  1. void delay_ms(void)
  2. {
  3.            unsigned char b,c;
  4.            for(c=0;c<180;c++)
  5.            {
  6.                for(b=0;b<8;b++);
  7.            }
  8. }

4)控制电机旋转方向
对于4相5线式步进电机来讲,可通过8个节拍的脉冲序列来控制其正反转。
正转的驱动函数为:
  1. void zx()
  2. {
  3.     unsigned char X,Y;  
  4.     for(X=0;X<64;X++)
  5.     {
  6.        for(Y=0;Y<8;Y++)
  7.        {
  8.          MDL;
  9.          MAH; //A可
  10.          delay_ms();
  11.          MBH; //AB
  12.          delay_ms();
  13.          MAL; //B
  14.          delay_ms();
  15.          MCH; //BC
  16.          delay_ms();
  17.          MBL; //C
  18.          delay_ms();
  19.          MDH; //CD
  20.          delay_ms();
  21.          MCL; //D
  22.          delay_ms();
  23.          MAH; //DA
  24.          delay_ms();
  25.        }               
  26.     }  
  27. }

反转的驱动函数为:
  1. void fx()
  2. {
  3.     unsigned char X,Y;
  4.     for(X=0;X<64;X++)    // 控制节拍数
  5.     {
  6.        for(Y=0;Y<8;Y++)   // 控制节拍
  7.        {
  8.          MDH;
  9.          MAH; //A
  10.          delay_ms();
  11.          MAL; //AB
  12.          delay_ms();
  13.          MCH; //B
  14.          delay_ms();
  15.          MDL; //BC
  16.          delay_ms();
  17.          MBH; //C
  18.          delay_ms();
  19.          MCL; //CD
  20.          delay_ms();
  21.          MAH; //D
  22.          delay_ms();
  23.          MBL; //DA
  24.          delay_ms();
  25.        }               
  26.     }
  27. }

5)主程序控制电机运行
  1. void main()
  2. {
  3.      unsigned char rdata=9,f,c;
  4.      delay_ms();
  5.      //停止电机转动
  6.      MAL;
  7.      MBL;
  8.      MCL;
  9.      MDL;
  10.      delay_ms();
  11.      f=1;//控制转向
  12.      while(1)
  13.      {
  14.              for(c=0;c<rdata;c++)    //控制转动的位置
  15.             {
  16.                                 if(f==0)
  17.                                      fx();
  18.                                 else
  19.                                 {
  20.                                      zx();
  21.                                 }
  22.             }
  23.      }
  24. }

相当于手工编程,使用易码魔盒生成代码也大致需要这几个步骤。
1)在芯片配置视图,选取步进电机(4相),然后配置引脚的使用。
2.png

2)添加延时函数
  1. void delay_ms(void)
  2. {
  3.            unsigned char b,c;
  4.            for(c=0;c<200;c++)
  5.            {
  6.                for(b=0;b<80;b++);
  7.            }
  8. }

3)在用户程序图形化编辑视图,完成下面图示的程序流程以生成程序代码。
3.png    
程序流程图
生成的程序主程序为:
  1. void main(void)
  2. {      
  3.     SC_Init();
  4.     SCD_StepMotor_Init( 1 , 0 , 0 );
  5.     SCD_StepMotor_Move( 1 , 64*64 );
  6.     SCD_StepMotor_Enable( 1 , 1 );
  7.     while(1)
  8.     {
  9.         SCD_StepMotor_Rotate();
  10.         delay_ms();
  11.     }
  12. }

其中:SC_Init()为整体的初始化函数,SCD_StepMotor_Init( 1 , 0 , 0 )为步进电机的初始化函数,并选取第一组电机工作,旋转方向为逆时针方向;SCD_StepMotor_Enable( 1 , 1 )函数为使能控制函数,以驱动第一组电机工作;SCD_StepMotor_Move( 1 , 64*64 )函数则是控制步进电机按指定步长的方式控制运行。

SCD_StepMotor_Init()的主要内容为:
  1. void SCD_StepMotor_Init(unsigned char MotorSelect,unsigned char Direction,unsigned char ChangeTime)
  2. {       // ChangeTime 为节拍间隔时间,控制步进电机的转速。
  3.          if(MotorSelect==1)
  4.          {      
  5.                    SCD_StepMotor_PinMode(SCD_StepMotor1_PhaseA_INIT, GPIO_MODE_OUT_PP);
  6.                    SCD_StepMotor_PinMode(SCD_StepMotor1_PhaseB_INIT, GPIO_MODE_OUT_PP);
  7.                    SCD_StepMotor_PinMode(SCD_StepMotor1_PhaseC_INIT, GPIO_MODE_OUT_PP);
  8.                    SCD_StepMotor_PinMode(SCD_StepMotor1_PhaseD_INIT, GPIO_MODE_OUT_PP);
  9.                    SCD_StepMotor_Control(MotorSelect,Direction,ChangeTime);
  10.          }
  11. }

从中可以看出,它完成了引脚的分配和输出功能设置。
SCD_StepMotor_Move()的主要内容为:
  1. void SCD_StepMotor_Move(unsigned char MotorSelect,unsigned int step)
  2. {
  3.           if(MotorSelect == 1)
  4.           {
  5.                  SCD_StepMotor1_MoveCount = step;
  6.                    SCD_StepMotor1_MoveFlag = 1;
  7.                    SCD_StepMotor1_Switch = 1;
  8.           }

它的主要作用是通过标志SCD_StepMotor1_MoveFlag来指定按旋转步数运行。

SCD_StepMotor_Enable()的主要内容为:
  1. void SCD_StepMotor_Enable(unsigned char MotorSelect,unsigned char enable)
  2. {
  3.          if(MotorSelect==1)
  4.          {      
  5.                    if(enable)
  6.                    {
  7.                             SCD_StepMotor1_Switch = 1;
  8.                    }
  9.                    else
  10.                    {
  11.                             SCD_StepMotor1_Switch = 0;
  12.                             SCD_StepMotor1_MoveFlag = 0;
  13.                             SCD_StepMotor1_MoveCount = 0;
  14.                    }
  15.          }

其作用是通过标志SCD_StepMotor1_Switch来启动电机运行。

在所以函数中,以函数SCD_StepMotor_Rotate()最为复杂,其主要内容如下,为便于理解直接加以注释说明。
  1. void SCD_StepMotor_Rotate()
  2. {
  3.           if(SCD_StepMotor1_Switch)         //开启运行
  4.           {
  5.                     SCD_StepMotor1_TimeCount++;
  6.                     if(SCD_StepMotor1_TimeCount>SCD_StepMotor1_X.ChangeTime)  
  7.                     {      
  8.                             // ChangeTime  控制步进电机的转速
  9.                            SCD_StepMotor1_TimeCount = 0;
  10.                             #if((SCD_StepMotor1_PhaseA_INIT!=0xFF00)&&(SCD_StepMotor1_PhaseB_INIT!=0xFF00)&&(SCD_StepMotor1_PhaseC_INIT!=0xFF00)&&(SCD_StepMotor1_PhaseD_INIT!=0xFF00))
  11.                             if(SCD_StepMotor1_X.Direction)  // 正转
  12.                             {
  13.                                       
  14.                                       switch(SCD_StepMotor1_Step)
  15.                                       {
  16.                                               case0:SCD_StepMotor1_PhaseA=1;SCD_StepMotor1_PhaseB=0;SCD_StepMotor1_PhaseC=0;SCD_StepMotor1_PhaseD=0;break;
  17.                                               case1:SCD_StepMotor1_PhaseA=1;SCD_StepMotor1_PhaseB=1;SCD_StepMotor1_PhaseC=0;SCD_StepMotor1_PhaseD=0;break;
  18.                                                case2:SCD_StepMotor1_PhaseA=0;SCD_StepMotor1_PhaseB=1;SCD_StepMotor1_PhaseC=0;SCD_StepMotor1_PhaseD=0;break;
  19.                                                case3:SCD_StepMotor1_PhaseA=0;SCD_StepMotor1_PhaseB=1;SCD_StepMotor1_PhaseC=1;SCD_StepMotor1_PhaseD=0;break;
  20.                                                case4:SCD_StepMotor1_PhaseA=0;SCD_StepMotor1_PhaseB=0;SCD_StepMotor1_PhaseC=1;SCD_StepMotor1_PhaseD=0;break;
  21.                                                case5:SCD_StepMotor1_PhaseA=0;SCD_StepMotor1_PhaseB=0;SCD_StepMotor1_PhaseC=1;SCD_StepMotor1_PhaseD=1;break;
  22.                                                case6:SCD_StepMotor1_PhaseA=0;SCD_StepMotor1_PhaseB=0;SCD_StepMotor1_PhaseC=0;SCD_StepMotor1_PhaseD=1;break;
  23.                                                case7:SCD_StepMotor1_PhaseA=1;SCD_StepMotor1_PhaseB=0;SCD_StepMotor1_PhaseC=0;SCD_StepMotor1_PhaseD=1;break;
  24.                                                default:break;
  25.                                       }
  26.                              }
  27.                              else
  28.                              {      

在该函数中,依次解决了节拍输出、正反转控制、按旋转步数控制运行等。这次,就缺少相应的节拍延时函数配置了,补放到主程序的循环中即可!
经实际验证,效果正常有效,这样又掌握一种用易码魔盒设计步进电机驱动程序的方法,也对易码魔盒中的外设驱动函数有了更深的了解。

RAR文件:

BJDJ.rar

1.57 MB, 下载次数: 28

您需要登录后才可以回帖 登录 | 注册

本版积分规则

521

主题

2949

帖子

39

粉丝
快速回复 在线客服 返回列表 返回顶部