[STM32F1] STM32单片机步进电机驱动正反转程序

[复制链接]
720|2
 楼主| mickit 发表于 2024-3-18 08:14 | 显示全部楼层 |阅读模式


  1. //IN4: PB6  d
  2. //IN3: PB5  c
  3. //IN2: PB4  b
  4. //IN1: PB3   a
  5. //四相八拍
  6. u8 phasecw[8] ={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};// 逆时针100000001100000001000000011000000010000000110000000100001001
  7. u8 phaseccw[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};// 顺时针100100000001000000110000001000000110000001000000110000001000


  8. //引脚初始化
  9. void Step_Motor_GPIO_Init(void)
  10. {
  11.     GPIO_InitTypeDef GPIO_InitStructure;
  12.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  13.                 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//打开AFIO时钟

  14.     GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);   //Full SWJ Disabled (JTAG-DP + SW-DP)  
  15.    // 改变指定管脚的映射 GPIO_Remap_SWJ_Disable SWJ 完全禁用(JTAG-DP+SW-DP)

  16.     GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //JTAG-DP Disabled and SW-DP Enabled
  17.    // 改变指定管脚的映射 GPIO_Remap_SWJ_JTAGDisable ,JTAG-DP 禁用 + SW-DP 使能

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

  41.     }
  42.     if(InputData&0x04)
  43.     {
  44.         GPIO_SetBits(GPIOB,GPIO_Pin_6);
  45.     }
  46.     else
  47.     {
  48.         GPIO_ResetBits(GPIOB,GPIO_Pin_6);
  49.     }
  50.     if(InputData&0x08)
  51.     {
  52.         GPIO_SetBits(GPIOB,GPIO_Pin_7);
  53.     }
  54.     else
  55.     {
  56.         GPIO_ResetBits(GPIOB,GPIO_Pin_7);
  57.     }
  58. }
  59. //步距角5.625 360/5.625=64 减速比1/64
  60. //故64*64个脉冲转一圈
  61. //n圈数
  62. //position 方向
  63. void motorNcircle(int n,bool position)
  64. {
  65.     int i,j,k=0;
  66.     for(j=0;j<n;j++)
  67.     {
  68.         //for(i=0;i<64*8;i++)
  69.         for(i=0;i<8;i++)
  70.         {
  71.             for(k=0;k<8;k++)
  72.             {
  73.                 if(1 == position)
  74.                 {
  75.                     SetMotor(phasecw[k]);
  76.                 }
  77.                 else if(0 == position)
  78.                 {
  79.                     SetMotor(phaseccw[k]);
  80.                 }
  81.                 delay_ms(2);
  82.             }
  83.         }
  84.     }
  85. }


LEDyyds 发表于 2024-3-18 15:00 | 显示全部楼层
这个这么简洁,能行吗
小夏天的大西瓜 发表于 2024-3-18 17:30 | 显示全部楼层
STM32单片机步进电机驱动正反转原理图有嘛
您需要登录后才可以回帖 登录 | 注册

本版积分规则

89

主题

1560

帖子

1

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