当前位置:   article > 正文

stm32驱动四相五线步进电机(附源码)_五线四相步进电机驱动代码

五线四相步进电机驱动代码

目录

一.四相五线步进电机驱动原理

二.源码(HAL库)


(学习自用,原理网上找的,侵权可删)

一.四相五线步进电机驱动原理

        

        电机的转速、停止的位置只取决于脉冲信号的频率和脉冲数,而不受负载变化的影响,即给电机加一个脉冲信号,电机则转过一个步距角。  

步进电机的主要特性:

1、 步进电机必须加驱动才可以运转, 驱动信号必须为脉冲信号,没有脉冲的时候,步进电机静止, 如果加入适当的脉冲信号, 就会以一定的角度(称为步角)转动。转动的速度和脉冲的频率成正比。

2、 黑金刚配套的是 28BYJ48 5V 驱动的 4 相 5 线的步进电机,而且是减速步进电机,减速比为 1:64,步进角为 5.625/64 度。如果需要转动 1 圈,那么需要360/5.625*64=4096 个脉冲信号。

采用八拍的方式进行驱动:AA AB BB BC CC CD DD DA

注意事项:

步进电机停止后需要使四个相位引脚都为高电平,否则步进电机会发热。因为不进电机公共端为高电平,所有引脚都为高电平就不会产生电流,就不会发热。

二.源码(HAL库

驱动代码:

  1. #include "./BSP/sun_motor/sun_motor.h"
  2. int32_t location_now[ArrayCount(LINES_MOTOR_STEP)]; //the val of motor location now
  3. int32_t location_aim[ArrayCount(LINES_MOTOR_STEP)]; //the val of motor location aim
  4. StepMotorState motorState[ArrayCount(LINES_MOTOR_STEP)]; //mark the state of motor
  5. void StepMotor_AA(uint8_t choose_motor)
  6. {
  7. PIN_OUT( LINES_MOTOR_STEP[choose_motor][0], LINE_ON);
  8. PIN_OUT( LINES_MOTOR_STEP[choose_motor][1], LINE_OFF);
  9. PIN_OUT( LINES_MOTOR_STEP[choose_motor][2], LINE_OFF);
  10. PIN_OUT( LINES_MOTOR_STEP[choose_motor][3], LINE_OFF);
  11. }
  12. void StepMotor_AB(uint8_t choose_motor)
  13. {
  14. PIN_OUT( LINES_MOTOR_STEP[choose_motor][0], LINE_ON);
  15. PIN_OUT( LINES_MOTOR_STEP[choose_motor][1], LINE_ON);
  16. PIN_OUT( LINES_MOTOR_STEP[choose_motor][2], LINE_OFF);
  17. PIN_OUT( LINES_MOTOR_STEP[choose_motor][3], LINE_OFF);
  18. }
  19. void StepMotor_BB(uint8_t choose_motor)
  20. {
  21. PIN_OUT( LINES_MOTOR_STEP[choose_motor][0], LINE_OFF);
  22. PIN_OUT( LINES_MOTOR_STEP[choose_motor][1], LINE_ON);
  23. PIN_OUT( LINES_MOTOR_STEP[choose_motor][2], LINE_OFF);
  24. PIN_OUT( LINES_MOTOR_STEP[choose_motor][3], LINE_OFF);
  25. }
  26. void StepMotor_BC(uint8_t choose_motor)
  27. {
  28. PIN_OUT( LINES_MOTOR_STEP[choose_motor][0], LINE_OFF);
  29. PIN_OUT( LINES_MOTOR_STEP[choose_motor][1], LINE_ON);
  30. PIN_OUT( LINES_MOTOR_STEP[choose_motor][2], LINE_ON);
  31. PIN_OUT( LINES_MOTOR_STEP[choose_motor][3], LINE_OFF);
  32. }
  33. void StepMotor_CC(uint8_t choose_motor)
  34. {
  35. PIN_OUT( LINES_MOTOR_STEP[choose_motor][0], LINE_OFF);
  36. PIN_OUT( LINES_MOTOR_STEP[choose_motor][1], LINE_OFF);
  37. PIN_OUT( LINES_MOTOR_STEP[choose_motor][2], LINE_ON);
  38. PIN_OUT( LINES_MOTOR_STEP[choose_motor][3], LINE_OFF);
  39. }
  40. void StepMotor_CD(uint8_t choose_motor)
  41. {
  42. PIN_OUT( LINES_MOTOR_STEP[choose_motor][0], LINE_OFF);
  43. PIN_OUT( LINES_MOTOR_STEP[choose_motor][1], LINE_OFF);
  44. PIN_OUT( LINES_MOTOR_STEP[choose_motor][2], LINE_ON);
  45. PIN_OUT( LINES_MOTOR_STEP[choose_motor][3], LINE_ON);
  46. }
  47. void StepMotor_DD(uint8_t choose_motor)
  48. {
  49. PIN_OUT( LINES_MOTOR_STEP[choose_motor][0], LINE_OFF);
  50. PIN_OUT( LINES_MOTOR_STEP[choose_motor][1], LINE_OFF);
  51. PIN_OUT( LINES_MOTOR_STEP[choose_motor][2], LINE_OFF);
  52. PIN_OUT( LINES_MOTOR_STEP[choose_motor][3], LINE_ON);
  53. }
  54. void StepMotor_DA(uint8_t choose_motor)
  55. {
  56. PIN_OUT( LINES_MOTOR_STEP[choose_motor][0], LINE_ON);
  57. PIN_OUT( LINES_MOTOR_STEP[choose_motor][1], LINE_OFF);
  58. PIN_OUT( LINES_MOTOR_STEP[choose_motor][2], LINE_OFF);
  59. PIN_OUT( LINES_MOTOR_STEP[choose_motor][3], LINE_ON);
  60. }
  61. void StepMotor_Stop(uint8_t choose_motor)
  62. {
  63. PIN_OUT( LINES_MOTOR_STEP[choose_motor][0], LINE_OFF);
  64. PIN_OUT( LINES_MOTOR_STEP[choose_motor][1], LINE_OFF);
  65. PIN_OUT( LINES_MOTOR_STEP[choose_motor][2], LINE_OFF);
  66. PIN_OUT( LINES_MOTOR_STEP[choose_motor][3], LINE_OFF);
  67. }
  68. /*
  69. @brief: Init the location and series of the stepmotor
  70. @para: none
  71. @retval: none
  72. */
  73. void StepMotor_Init()
  74. {
  75. uint8_t flag;
  76. uint8_t i;
  77. GPIO_InitTypeDef motor_gpio_init_struct;
  78. MOTOR_GPIO_CLK_ENABLE();
  79. for(flag = 0;flag < ArrayCount(LINES_MOTOR_STEP); flag++)
  80. {
  81. for(i = 0; i < 4; i++)
  82. {
  83. motor_gpio_init_struct.Pin=LINES_MOTOR_STEP[flag][i];
  84. motor_gpio_init_struct.Mode=GPIO_MODE_OUTPUT_PP;
  85. motor_gpio_init_struct.Pull=GPIO_PULLUP;
  86. }
  87. StepMotor_AA(flag);
  88. delay_ms(1);
  89. StepMotor_AB(flag);
  90. delay_ms(1);
  91. StepMotor_BB(flag);
  92. delay_ms(1);
  93. StepMotor_BC(flag);
  94. delay_ms(1);
  95. StepMotor_BB(flag);
  96. delay_ms(1);
  97. StepMotor_AB(flag);
  98. delay_ms(1);
  99. StepMotor_AA(flag);
  100. delay_ms(1);
  101. StepMotor_Stop(flag);
  102. delay_ms(1);
  103. location_now[flag] = 0;
  104. location_aim[flag] = 0;
  105. motorState[flag] = StepMotor_Stop;
  106. }
  107. HAL_GPIO_Init(GPIOB, &motor_gpio_init_struct);
  108. }
  109. /*
  110. @brief: motor walk one step
  111. @para: choose motor
  112. @retval: NULL
  113. */
  114. void StepMotor_WalkOneStep(uint8_t choose_motor)
  115. {
  116. uint8_t state = 0;
  117. //if the location now is the same with the location aim, step_motor will stop
  118. if(location_now[choose_motor]==location_aim[choose_motor])
  119. {
  120. StepMotor_Stop(choose_motor);
  121. motorState[flag] = StepMotor_Stop;
  122. return;
  123. }
  124. //if the location now is not the same with the location aim, step_motor will stop
  125. state = (location_now[choose_motor]%8 + 8)%8; //Calculate the current coil that needs to be connected
  126. switch(state)
  127. {
  128. case 0:Step_AB(choose_motor);break;
  129. case 1:Step_BB(choose_motor);break;
  130. case 2:Step_BC(choose_motor);break;
  131. case 3:Step_CC(choose_motor);break;
  132. case 4:Step_CD(choose_motor);break;
  133. case 5:Step_DD(choose_motor);break;
  134. case 6:Step_DA(choose_motor);break;
  135. case 7:Step_AA(choose_motor);break;
  136. }
  137. if(location_now[choose_motor] < location_aim[choose_motor] )//if now location less than aim location
  138. {
  139. location_now[choose_motor]++; //location_now add 1
  140. }
  141. }

头文件

  1. #ifndef _sun_motor_H_
  2. #define _sun_motor_H_
  3. #include "./SYSTEM/sys/sys.h"
  4. typedef enum
  5. {
  6. StepMotor_Backward=-1,
  7. StepMotor_Stop=0,
  8. StepMotor_Forward=1
  9. }StepMotorState;
  10. #define LINE_ON GPIO_PIN_SET
  11. #define LINE_OFF GPIO_PIN_RESET
  12. /******************************************************************************************/
  13. /* 引脚 定义 */
  14. #define MOTOR_GPIO_PORT GPIOB
  15. #define MOTOR0_A_GPIO_PIN GPIO_PIN_0
  16. #define MOTOR0_B_GPIO_PIN GPIO_PIN_1
  17. #define MOTOR0_C_GPIO_PIN GPIO_PIN_8
  18. #define MOTOR0_D_GPIO_PIN GPIO_PIN_3
  19. #define PB0 MOTOR0_A_GPIO_PIN
  20. #define PB1 MOTOR0_B_GPIO_PIN
  21. #define PB8 MOTOR0_C_GPIO_PIN
  22. #define PB3 MOTOR0_D_GPIO_PIN
  23. #define MOTOR1_A_GPIO_PIN GPIO_PIN_4
  24. #define MOTOR1_B_GPIO_PIN GPIO_PIN_5
  25. #define MOTOR1_C_GPIO_PIN GPIO_PIN_6
  26. #define MOTOR1_D_GPIO_PIN GPIO_PIN_7
  27. #define PB4 MOTOR1_A_GPIO_PIN
  28. #define PB5 MOTOR1_B_GPIO_PIN
  29. #define PB6 MOTOR1_C_GPIO_PIN
  30. #define PB7 MOTOR1_D_GPIO_PIN
  31. static const uint16_t LINES_MOTOR_STEP[][4] = {{PB0,PB1,PB8,PB3},{PB4,PB5,PB6,PB7}};
  32. #define MOTOR_GPIO_CLK_ENABLE() do{ __HAL_RCC_GPIOB_CLK_ENABLE(); }while(0) /* PB口时钟使能 */
  33. #define PIN_OUT(uint16_t GPIO_Pin, GPIO_PinState PinState) HAL_GPIO_WritePin(GPIOB, uint16_t GPIO_Pin, GPIO_PinState PinState)
  34. #endif

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/734140
推荐阅读
相关标签
  

闽ICP备14008679号