PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.8KB

  1. // AFMotor_ConstantSpeed.pde
  2. // -*- mode: C++ -*-
  3. //
  4. // Shows how to use AccelStepper to control a 3-phase motor, such as a HDD spindle motor
  5. // using the Adafruit Motor Shield
  6. // http://www.ladyada.net/make/mshield/index.html.
  7. // Create a subclass of AccelStepper which controls the motor pins via the
  8. // Motor Shield serial-to-parallel interface
  9. #include <AccelStepper.h>
  10. // Arduino pin names for interface to 74HCT595 latch
  11. // on Adafruit Motor Shield
  12. #define MOTORLATCH 12
  13. #define MOTORCLK 4
  14. #define MOTORENABLE 7
  15. #define MOTORDATA 8
  16. // PWM pins, also used to enable motor outputs
  17. #define PWM0A 5
  18. #define PWM0B 6
  19. #define PWM1A 9
  20. #define PWM1B 10
  21. #define PWM2A 11
  22. #define PWM2B 3
  23. // The main purpose of this class is to override setOutputPins to work with Adafruit Motor Shield
  24. class AFMotorShield : public AccelStepper
  25. {
  26. public:
  27. AFMotorShield(uint8_t interface = AccelStepper::FULL4WIRE, uint8_t pin1 = 2, uint8_t pin2 = 3, uint8_t pin3 = 4, uint8_t pin4 = 5);
  28. virtual void setOutputPins(uint8_t mask);
  29. };
  30. AFMotorShield::AFMotorShield(uint8_t interface, uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4)
  31. : AccelStepper(interface, pin1, pin2, pin3, pin4)
  32. {
  33. // Enable motor control serial to parallel latch
  34. pinMode(MOTORLATCH, OUTPUT);
  35. pinMode(MOTORENABLE, OUTPUT);
  36. pinMode(MOTORDATA, OUTPUT);
  37. pinMode(MOTORCLK, OUTPUT);
  38. digitalWrite(MOTORENABLE, LOW);
  39. // enable both H bridges on motor 1
  40. pinMode(PWM2A, OUTPUT);
  41. pinMode(PWM2B, OUTPUT);
  42. pinMode(PWM0A, OUTPUT);
  43. pinMode(PWM0B, OUTPUT);
  44. digitalWrite(PWM2A, HIGH);
  45. digitalWrite(PWM2B, HIGH);
  46. digitalWrite(PWM0A, HIGH);
  47. digitalWrite(PWM0B, HIGH);
  48. setOutputPins(0); // Reset
  49. };
  50. // Use the AF Motor Shield serial-to-parallel to set the state of the motor pins
  51. // Caution: the mapping of AccelStepper pins to AF motor outputs is not
  52. // obvious:
  53. // AccelStepper Motor Shield output
  54. // pin1 M4A
  55. // pin2 M1A
  56. // pin3 M2A
  57. // pin4 M3A
  58. // Caution this is pretty slow and limits the max speed of the motor to about 500/3 rpm
  59. void AFMotorShield::setOutputPins(uint8_t mask)
  60. {
  61. uint8_t i;
  62. digitalWrite(MOTORLATCH, LOW);
  63. digitalWrite(MOTORDATA, LOW);
  64. for (i=0; i<8; i++)
  65. {
  66. digitalWrite(MOTORCLK, LOW);
  67. if (mask & _BV(7-i))
  68. digitalWrite(MOTORDATA, HIGH);
  69. else
  70. digitalWrite(MOTORDATA, LOW);
  71. digitalWrite(MOTORCLK, HIGH);
  72. }
  73. digitalWrite(MOTORLATCH, HIGH);
  74. }
  75. AFMotorShield stepper(AccelStepper::HALF3WIRE, 0, 0, 0, 0); // 3 phase HDD spindle drive
  76. void setup()
  77. {
  78. stepper.setMaxSpeed(500); // divide by 3 to get rpm
  79. stepper.setAcceleration(80);
  80. stepper.moveTo(10000000);
  81. }
  82. void loop()
  83. {
  84. stepper.run();
  85. }