A simple sample to try to control stepper with Allegro A4988 stepper driver

Dependencies:   mbed

Committer:
ytsuboi
Date:
Sat Mar 22 15:41:45 2014 +0000
Revision:
0:b9c1a7c906f5
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ytsuboi 0:b9c1a7c906f5 1 // simple A4988 sample
ytsuboi 0:b9c1a7c906f5 2 // jumper reset and sleep together
ytsuboi 0:b9c1a7c906f5 3 // connect VDD to mbed VOUT
ytsuboi 0:b9c1a7c906f5 4 // connect GND to mbed GND
ytsuboi 0:b9c1a7c906f5 5 // connect 1A and 1B to stepper coil 1 (GREEN and BLACK wire)
ytsuboi 0:b9c1a7c906f5 6 // connect 2A and 2B to stepper coil 2 (BLUE and RED wire)
ytsuboi 0:b9c1a7c906f5 7 // connect VMOT to power source (to AC adapter)
ytsuboi 0:b9c1a7c906f5 8 // connect GRD to power source (to AC adapter)
ytsuboi 0:b9c1a7c906f5 9 // connect STEP to mbed p22
ytsuboi 0:b9c1a7c906f5 10 // connect DIR to mbed p21
ytsuboi 0:b9c1a7c906f5 11
ytsuboi 0:b9c1a7c906f5 12 #include "mbed.h"
ytsuboi 0:b9c1a7c906f5 13
ytsuboi 0:b9c1a7c906f5 14 DigitalOut A4988STEP(p22);
ytsuboi 0:b9c1a7c906f5 15 DigitalOut A4988DIR(p21);
ytsuboi 0:b9c1a7c906f5 16 int a=0;
ytsuboi 0:b9c1a7c906f5 17
ytsuboi 0:b9c1a7c906f5 18 int main() {
ytsuboi 0:b9c1a7c906f5 19 A4988DIR = 0;
ytsuboi 0:b9c1a7c906f5 20 A4988STEP = 0;
ytsuboi 0:b9c1a7c906f5 21
ytsuboi 0:b9c1a7c906f5 22 while(1) {
ytsuboi 0:b9c1a7c906f5 23 if (a < 200) //sweep 200 step in dir 1
ytsuboi 0:b9c1a7c906f5 24 {
ytsuboi 0:b9c1a7c906f5 25 a++;
ytsuboi 0:b9c1a7c906f5 26 A4988STEP = 1;
ytsuboi 0:b9c1a7c906f5 27 wait(0.008);
ytsuboi 0:b9c1a7c906f5 28 A4988STEP = 0;
ytsuboi 0:b9c1a7c906f5 29 wait(0.008);
ytsuboi 0:b9c1a7c906f5 30 }
ytsuboi 0:b9c1a7c906f5 31 else
ytsuboi 0:b9c1a7c906f5 32 {
ytsuboi 0:b9c1a7c906f5 33 A4988DIR = 1;
ytsuboi 0:b9c1a7c906f5 34 a++;
ytsuboi 0:b9c1a7c906f5 35 A4988STEP = 1;
ytsuboi 0:b9c1a7c906f5 36 wait(0.008);
ytsuboi 0:b9c1a7c906f5 37 A4988STEP = 0;
ytsuboi 0:b9c1a7c906f5 38 wait(0.008);
ytsuboi 0:b9c1a7c906f5 39
ytsuboi 0:b9c1a7c906f5 40 if (a>400) //sweep 200 in dir 2
ytsuboi 0:b9c1a7c906f5 41 {
ytsuboi 0:b9c1a7c906f5 42 a = 0;
ytsuboi 0:b9c1a7c906f5 43 A4988DIR = 0;
ytsuboi 0:b9c1a7c906f5 44 }
ytsuboi 0:b9c1a7c906f5 45 }
ytsuboi 0:b9c1a7c906f5 46 }
ytsuboi 0:b9c1a7c906f5 47 }