MOTOR_STEP_ATY.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @file MOTOR_STEP_ATY.h
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of SteppingMotor control
  20. *
  21. * @version
  22. * - 1_01_221231 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_02_230408 > ATY
  25. * -# Add State Machine and so
  26. * - 1_03_230426 > ATY
  27. * -# Change name "SteppingMotor_ATY" to "MOTOR_STEP_ATY"
  28. ********************************************************************************
  29. */
  30. #ifndef __MOTOR_STEP_ATY_H
  31. #define __MOTOR_STEP_ATY_H
  32. #include "INCLUDE_ATY.h"
  33. #include "HW_GPIO_ATY.h"
  34. #include "HW_PWM_ATY.h"
  35. /******************************* For user *************************************/
  36. // #define __DEBUG_MOTOR_STEP_ATY
  37. // #define S_PARAM_C 0.5f // 0-1
  38. // #define Motor_StartLine_2
  39. #define Motor_StartLine_T
  40. // #define Motor_StartLine_S
  41. #define Motor_Channel PWM_CHANNEL_8P
  42. /******************************************************************************/
  43. extern uint8_t motorEn;
  44. extern uint8_t motorDir;
  45. extern uint32_t motorSoftSpeed;
  46. extern uint32_t motorSpeed;
  47. extern uint32_t motorSoftTime;
  48. extern uint32_t motorTime;
  49. extern uint32_t motorStartCounter;
  50. extern uint32_t motorStopCounter; // for stall detection or driver error(TMC2209)
  51. extern uint8_t motorSetState; // 0: stop, 1: start hold, 2: start soft, 3: run speed, 4: change dir, 5: stop state, soft set 0 after use
  52. #define MOTOR_EN_SET_ENABLE \
  53. do{GPIO_SET_L(MOTOR_EN_PORT, MOTOR_EN_PIN); motorEn = 1;}while(0)
  54. #define MOTOR_EN_SET_DISABLE \
  55. do{GPIO_SET_H(MOTOR_EN_PORT, MOTOR_EN_PIN); motorEn = 0;}while(0)
  56. #define MOTOR_DIR_OUT 0
  57. #define MOTOR_DIR_IN 1
  58. #define MOTOR_DIR_SET_IN \
  59. do{ \
  60. if(MOTOR_DIR_IN) \
  61. GPIO_SET_H(MOTOR_DIR_PORT, MOTOR_DIR_PIN); \
  62. else \
  63. GPIO_SET_L(MOTOR_DIR_PORT, MOTOR_DIR_PIN); \
  64. motorDir = MOTOR_DIR_IN; \
  65. }while(0)
  66. #define MOTOR_DIR_SET_OUT \
  67. do{ \
  68. if(MOTOR_DIR_OUT) \
  69. GPIO_SET_H(MOTOR_DIR_PORT, MOTOR_DIR_PIN); \
  70. else \
  71. GPIO_SET_L(MOTOR_DIR_PORT, MOTOR_DIR_PIN); \
  72. motorDir = MOTOR_DIR_OUT; \
  73. }while(0)
  74. #define MOTOR_STEP_SET_H \
  75. (GPIO_SET_H(MOTOR_STEP_PORT, MOTOR_STEP_PIN))
  76. #define MOTOR_STEP_SET_L \
  77. (GPIO_SET_L(MOTOR_STEP_PORT, MOTOR_STEP_PIN))
  78. #define MOTOR_DIAG_GET_H (GPIO_GET_H(MOTOR_DIAG_PORT, MOTOR_DIAG_PIN))
  79. #define MOTOR_DIAG_GET_L (GPIO_GET_L(MOTOR_DIAG_PORT, MOTOR_DIAG_PIN))
  80. #define MOTOR_PARAM_SET(dir, softSpeed, speed, softTime, time, setState) \
  81. do \
  82. { \
  83. motorDir = (uint8_t)dir; \
  84. motorSoftSpeed = (uint32_t)softSpeed; \
  85. motorSpeed = (uint32_t)speed; \
  86. motorSoftTime = (uint32_t)softTime; \
  87. motorTime = (uint32_t)time; \
  88. motorSetState = (uint8_t)setState; \
  89. } while(0)
  90. void MotorStart(uint8_t dir, uint32_t speed);
  91. void MotorStartSpeed(uint32_t speed);
  92. void MotorStop(void);
  93. void MotorStateMachine_Step(void);
  94. #endif /* __MOTOR_STEP_ATY_H */
  95. /******************************** End Of File *********************************/