|
| 1 | +/* This example shows basic use of the DRV8711 stepper motor driver. |
| 2 | +
|
| 3 | + It shows how to initialize the driver, set the current limit, set |
| 4 | + the micro-stepping mode, and enable the driver. It shows how to send |
| 5 | + pulses to the STEP pin to get the driver to take steps and how to |
| 6 | + switch directions using the DIR pin. |
| 7 | +
|
| 8 | + Before using this example be sure the change the setTorque and setGain |
| 9 | + lines to have an appropriate current limit for your system. |
| 10 | + |
| 11 | + */ |
| 12 | + |
| 13 | +#include <SPI.h> |
| 14 | +#include <DRV8711.h> |
| 15 | + |
| 16 | +const uint8_t slaveSelectPin = 2; |
| 17 | +const uint8_t stepPin = 3; |
| 18 | +const uint8_t resetPin = 4; |
| 19 | +const uint8_t dirPin = 5; |
| 20 | +const uint8_t sleepPin = 6; |
| 21 | + |
| 22 | +DRV8711 stepper; |
| 23 | + |
| 24 | +void setup() |
| 25 | +{ |
| 26 | + SPI.begin(); |
| 27 | + stepper.init(slaveSelectPin); |
| 28 | + delay(1); |
| 29 | + |
| 30 | + // Drive the RST, STEP, and DIR pins low initially. Drive the nSLP pin high initially. |
| 31 | + digitalWrite(resetPin, LOW); |
| 32 | + pinMode(resetPin, OUTPUT); |
| 33 | + digitalWrite(stepPin, LOW); |
| 34 | + pinMode(stepPin, OUTPUT); |
| 35 | + digitalWrite(dirPin, LOW); |
| 36 | + pinMode(dirPin, OUTPUT); |
| 37 | + digitalWrite(sleepPin, HIGH); |
| 38 | + pinMode(sleepPin, OUTPUT); |
| 39 | + |
| 40 | + // Reset the driver to it's default settings. |
| 41 | + digitalWrite(resetPin, HIGH); |
| 42 | + delay(1); |
| 43 | + digitalWrite(resetPin, LOW); |
| 44 | + delay(1); |
| 45 | + |
| 46 | + // Set the current limit. You should change the numbers here to |
| 47 | + // an appropriate value for your particular system. |
| 48 | + // The current limit is given by the equation below: |
| 49 | + // IFS = (2.75 * TORQUE)/(256 * ISGAIN * .03) |
| 50 | + stepper.setTorque(56); |
| 51 | + stepper.setGain(20); |
| 52 | + |
| 53 | + // Set the step mode and enable the driver. |
| 54 | + stepper.setStepMode(32); |
| 55 | + stepper.enableDriver(); |
| 56 | +} |
| 57 | + |
| 58 | +void loop() |
| 59 | +{ |
| 60 | + // Step in the default direction 1000 times. |
| 61 | + setDirection(0); |
| 62 | + for(unsigned int x = 0; x < 1000; x++) |
| 63 | + { |
| 64 | + step(); |
| 65 | + } |
| 66 | + |
| 67 | + // Wait for 300 ms. |
| 68 | + delay(300); |
| 69 | + |
| 70 | + // Step in the other direction 1000 times. |
| 71 | + setDirection(1); |
| 72 | + for(unsigned int x = 0; x < 1000; x++) |
| 73 | + { |
| 74 | + step(); |
| 75 | + } |
| 76 | + |
| 77 | + // Wait for 300 ms. |
| 78 | + delay(300); |
| 79 | +} |
| 80 | + |
| 81 | +// Sends a pulse on the STEP pin to tell the driver to take |
| 82 | +// one step, and also delays to control the speed of the motor. |
| 83 | +void step() |
| 84 | +{ |
| 85 | + // The NXT/STEP minimum high pulse width is 2 microseconds. |
| 86 | + digitalWrite(stepPin, HIGH); |
| 87 | + delayMicroseconds(3); |
| 88 | + digitalWrite(stepPin, LOW); |
| 89 | + delayMicroseconds(3); |
| 90 | + |
| 91 | + // The delay here controls the stepper motor's speed. You can |
| 92 | + // increase the delay to make the stepper motor go slower. If |
| 93 | + // you decrease the delay, the stepper motor will go fast, but |
| 94 | + // there is a limit to how fast it can go before it starts |
| 95 | + // missing steps. |
| 96 | + delayMicroseconds(2000); |
| 97 | +} |
| 98 | + |
| 99 | +void setDirection(bool dir) |
| 100 | +{ |
| 101 | + // The NXT/STEP pin must not change for at least 0.5 |
| 102 | + // microseconds before and after changing the DIR pin. |
| 103 | + delayMicroseconds(1); |
| 104 | + digitalWrite(dirPin, dir); |
| 105 | + delayMicroseconds(1); |
| 106 | +} |
0 commit comments