Fully functional driver for the DRV88255 stepper motor driver. Includes microstepping, speed, disable and enable.
Dependents: Lidar_2D_Mapping Water Play Water_pump Lidar_2D_Mapping
DRV8825.cpp@1:c8f423bfe891, 2015-12-11 (annotated)
- Committer:
- sventura3
- Date:
- Fri Dec 11 15:50:02 2015 +0000
- Revision:
- 1:c8f423bfe891
- Parent:
- 0:a2a53dc49e0d
updated spelling error 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sventura3 | 0:a2a53dc49e0d | 1 | #include "DRV8825.h" |
sventura3 | 0:a2a53dc49e0d | 2 | #include "mbed.h" |
sventura3 | 0:a2a53dc49e0d | 3 | |
sventura3 | 0:a2a53dc49e0d | 4 | DigitalOut myled(LED1); |
sventura3 | 0:a2a53dc49e0d | 5 | |
sventura3 | 1:c8f423bfe891 | 6 | DRV8825::DRV8825(PinName _en, PinName m0, PinName m1, PinName m2, PinName _stepPin, PinName dir):en(_en), |
sventura3 | 0:a2a53dc49e0d | 7 | microstepping(m0, m1, m2), |
sventura3 | 0:a2a53dc49e0d | 8 | stepPin(_stepPin), |
sventura3 | 0:a2a53dc49e0d | 9 | direction(dir) |
sventura3 | 0:a2a53dc49e0d | 10 | { |
sventura3 | 0:a2a53dc49e0d | 11 | } |
sventura3 | 0:a2a53dc49e0d | 12 | |
sventura3 | 0:a2a53dc49e0d | 13 | void DRV8825::settings(float microstep, int dir, float speed) |
sventura3 | 0:a2a53dc49e0d | 14 | { |
sventura3 | 0:a2a53dc49e0d | 15 | //Microsteppiing settings |
sventura3 | 0:a2a53dc49e0d | 16 | if (microstep == 1) microstepping = 0; |
sventura3 | 0:a2a53dc49e0d | 17 | else if (microstep == 1/2) microstepping = 1; |
sventura3 | 0:a2a53dc49e0d | 18 | else if (microstep == 1/4) microstepping = 2; |
sventura3 | 0:a2a53dc49e0d | 19 | else if (microstep == 1/8) microstepping = 3; |
sventura3 | 0:a2a53dc49e0d | 20 | else if (microstep == 1/16) microstepping = 4; |
sventura3 | 0:a2a53dc49e0d | 21 | else if (microstep == 1/32) microstepping = 5; |
sventura3 | 0:a2a53dc49e0d | 22 | |
sventura3 | 0:a2a53dc49e0d | 23 | if (dir == 1) { |
sventura3 | 0:a2a53dc49e0d | 24 | direction = 0; |
sventura3 | 0:a2a53dc49e0d | 25 | } else if (dir == 0) { |
sventura3 | 0:a2a53dc49e0d | 26 | direction = 1; |
sventura3 | 0:a2a53dc49e0d | 27 | } |
sventura3 | 0:a2a53dc49e0d | 28 | |
sventura3 | 0:a2a53dc49e0d | 29 | // Speeed or times per second |
sventura3 | 0:a2a53dc49e0d | 30 | if(stepPin == 1){ |
sventura3 | 0:a2a53dc49e0d | 31 | stepPin = 0; |
sventura3 | 0:a2a53dc49e0d | 32 | wait(1/speed); |
sventura3 | 0:a2a53dc49e0d | 33 | } |
sventura3 | 0:a2a53dc49e0d | 34 | else{ |
sventura3 | 0:a2a53dc49e0d | 35 | stepPin = 1; |
sventura3 | 0:a2a53dc49e0d | 36 | wait(1/speed); |
sventura3 | 0:a2a53dc49e0d | 37 | } |
sventura3 | 0:a2a53dc49e0d | 38 | myled = stepPin; |
sventura3 | 0:a2a53dc49e0d | 39 | |
sventura3 | 0:a2a53dc49e0d | 40 | |
sventura3 | 0:a2a53dc49e0d | 41 | } |
sventura3 | 0:a2a53dc49e0d | 42 | |
sventura3 | 0:a2a53dc49e0d | 43 | void DRV8825::enable() |
sventura3 | 0:a2a53dc49e0d | 44 | { |
sventura3 | 0:a2a53dc49e0d | 45 | en = 0; |
sventura3 | 0:a2a53dc49e0d | 46 | } |
sventura3 | 0:a2a53dc49e0d | 47 | |
sventura3 | 0:a2a53dc49e0d | 48 | void DRV8825::disable() |
sventura3 | 0:a2a53dc49e0d | 49 | { |
sventura3 | 0:a2a53dc49e0d | 50 | en = 1; |
sventura3 | 0:a2a53dc49e0d | 51 | } |