A simple stepper motor driver library, supporting micro-stepping drivers such as the Pololu A4988 stepper driver carrier or the Sparkfun EasyDriver.

Dependents:   HangLiu_copy

Fork of StepperMotor by Matthew Else

Committer:
melse
Date:
Sun Feb 24 16:23:02 2013 +0000
Revision:
0:52fb09e87581
Child:
1:6fbe104c6f37
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
melse 0:52fb09e87581 1 #include "Stepper.h"
melse 0:52fb09e87581 2 #include "mbed.h"
melse 0:52fb09e87581 3
melse 0:52fb09e87581 4 stepper::stepper(PinName _en, PinName ms1, PinName ms2, PinName ms3, PinName _stepPin, PinName dir):en(_en),
melse 0:52fb09e87581 5 microstepping(ms1, ms2, ms3),
melse 0:52fb09e87581 6 stepPin(_stepPin),
melse 0:52fb09e87581 7 direction(dir)
melse 0:52fb09e87581 8 {
melse 0:52fb09e87581 9 }
melse 0:52fb09e87581 10
melse 0:52fb09e87581 11 void stepper::step(int microstep, int dir, float speed)
melse 0:52fb09e87581 12 {
melse 0:52fb09e87581 13 if (microstep == 1) {
melse 0:52fb09e87581 14 microstepping = 0;
melse 0:52fb09e87581 15 } else if (microstep <= 4) {
melse 0:52fb09e87581 16 microstepping = microstep / 2;
melse 0:52fb09e87581 17 } else if (microstep > 4) {
melse 0:52fb09e87581 18 microstepping = (microstep / 2) - 1;
melse 0:52fb09e87581 19 }
melse 0:52fb09e87581 20 if (dir == 1) {
melse 0:52fb09e87581 21 direction = 0;
melse 0:52fb09e87581 22 } else if (dir == 0) {
melse 0:52fb09e87581 23 direction = 1;
melse 0:52fb09e87581 24 }
melse 0:52fb09e87581 25
melse 0:52fb09e87581 26 // Step...
melse 0:52fb09e87581 27 stepPin = 1;
melse 0:52fb09e87581 28 wait(1/speed);
melse 0:52fb09e87581 29 stepPin = 0;
melse 0:52fb09e87581 30 wait(1/speed);
melse 0:52fb09e87581 31 }
melse 0:52fb09e87581 32
melse 0:52fb09e87581 33 void stepper::enable()
melse 0:52fb09e87581 34 {
melse 0:52fb09e87581 35 en = 0;
melse 0:52fb09e87581 36 }
melse 0:52fb09e87581 37
melse 0:52fb09e87581 38 void stepper::disable()
melse 0:52fb09e87581 39 {
melse 0:52fb09e87581 40 en = 1;
melse 0:52fb09e87581 41 }