标题: 自己用的步进电机程序STM32 四相八拍 源程序 [打印本页]

作者: 520899    时间: 2019-11-13 09:14
标题: 自己用的步进电机程序STM32 四相八拍 源程序
  1. #include "sys.h"
  2. #include "delay.h"
  3. #include "diver.h"
  4. #include "stdbool.h"
  5. //IN4: PB8  d
  6. //IN3: PB7  c
  7. //IN2: PB6  b
  8. //IN1: PB5  a
  9. //四相八拍
  10. u8 phasecw[8] ={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};// 逆时针
  11. u8 phaseccw[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};// 顺时针

  12. //引脚初始化
  13. void Step_Motor_GPIO_Init(void)
  14. {
  15.     GPIO_InitTypeDef GPIO_InitStructure;
  16.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

  17.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  18.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  19.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  20.     GPIO_Init(GPIOB,&GPIO_InitStructure);   
  21. }
  22. //引脚映射
  23. void SetMotor(unsigned char InputData)
  24. {
  25.     if(InputData&0x01)
  26.     {
  27.         GPIO_SetBits(GPIOB,GPIO_Pin_5);
  28.     }
  29.     else
  30.     {
  31.         GPIO_ResetBits(GPIOB,GPIO_Pin_5);
  32.     }
  33.     if(InputData&0x02)
  34.     {
  35.         GPIO_SetBits(GPIOB,GPIO_Pin_6);
  36.     }
  37.     else
  38.     {
  39.         GPIO_ResetBits(GPIOB,GPIO_Pin_6);

  40.     }
  41.     if(InputData&0x04)
  42.     {
  43.         GPIO_SetBits(GPIOB,GPIO_Pin_7);
  44.     }
  45.     else
  46.     {
  47.         GPIO_ResetBits(GPIOB,GPIO_Pin_7);
  48.     }
  49.     if(InputData&0x08)
  50.     {
  51.         GPIO_SetBits(GPIOB,GPIO_Pin_8);
  52.     }
  53.     else
  54.     {
  55.         GPIO_ResetBits(GPIOB,GPIO_Pin_8);
  56.     }
  57. }
  58. //步距角5.625 360/5.625=64 减速比1/64
  59. //故64*64个脉冲转一圈
  60. //n圈数
  61. //direction 方向
  62. void motorNcircle(int n,bool direction)
  63. {
  64.     int i,j,k=0;
  65.     for(j=0;j<n;j++)
  66.     {
  67.         for(i=0;i<64*8;i++)
  68.         //for(i=0;i<8;i++)
  69.         {
  70.             for(k=0;k<8;k++)
  71.             {
  72.                 if(1 == direction)
  73.                 {
  74.                     SetMotor(phasecw[k]);
  75.                 }
  76.                 else
  77.                 {
  78.                     SetMotor(phaseccw[k]);
  79.                 }
  80.                 delay_ms(2);
  81.             }
  82.         }
  83.     }
  84. }
  85. //speed 速度
  86. //direction 方向
  87. void motorNspeed(int speed,bool direction)
  88. {
  89.         unsigned char k=0;
  90.         for(k=0;k<8;k++)
  91.         {
  92.                 if(direction==1)
  93.                       SetMotor(phasecw[k]);  //取数据
  94.                 else
  95.                       SetMotor(phaseccw[k]);
  96.                 delay_ms(speed);        //调节转速        
  97.         }                          
  98. }
复制代码

全部资料51hei下载地址:
步进电机.7z (179.69 KB, 下载次数: 29)

作者: 电子科123    时间: 2019-12-10 16:05
能测电机的速度么?




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1