Very simple operation samples of unipolar stepper motor. This code has been made to help beginners to learn the stepper motor. The history of the code shows the how to generate pulses from very basic level.

Dependencies:   mbed

Committer:
okano
Date:
Sat Jan 11 02:37:00 2014 +0000
Revision:
11:436e74a3ec7a
Parent:
10:f00d0b8775d4
version 8 : motor control by function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 4:3cb4e78e0846 1 // stepper motor operation sample (learn it step by step).
okano 0:6e775c640f78 2 // showing how to control a unipolar stepper motor by mbed digital output ports.
okano 4:3cb4e78e0846 3 //
okano 11:436e74a3ec7a 4 // version 8 : motor control by function
okano 0:6e775c640f78 5
okano 0:6e775c640f78 6 #include "mbed.h"
okano 0:6e775c640f78 7
okano 6:11b35048d384 8 BusOut motor_out( p26, p25, p24, p23 );
okano 0:6e775c640f78 9
okano 11:436e74a3ec7a 10 #define CW 0 // CLOCKWISE
okano 11:436e74a3ec7a 11 #define CCW 1 // COUNTERCLOCKWISE
okano 1:e49f906a4dbd 12
okano 11:436e74a3ec7a 13 void rotate( int steps, float speed, char direction );
okano 7:032ae28fae2e 14
okano 4:3cb4e78e0846 15 int main()
okano 4:3cb4e78e0846 16 {
okano 0:6e775c640f78 17 while(1) {
okano 11:436e74a3ec7a 18 rotate( 20, 50, CW );
okano 11:436e74a3ec7a 19 rotate( 20, 50, CCW );
okano 11:436e74a3ec7a 20 rotate( 40, 100, CW );
okano 11:436e74a3ec7a 21 rotate( 40, 100, CCW );
okano 0:6e775c640f78 22 }
okano 0:6e775c640f78 23 }
okano 11:436e74a3ec7a 24
okano 11:436e74a3ec7a 25 void rotate( int steps, float speed, char direction )
okano 11:436e74a3ec7a 26 {
okano 11:436e74a3ec7a 27 static char pattern[] = { 0x3, 0x6, 0xC, 0x9 };
okano 11:436e74a3ec7a 28 static int state = 0;
okano 11:436e74a3ec7a 29
okano 11:436e74a3ec7a 30 for ( int i = 0; i < steps; i++ ) {
okano 11:436e74a3ec7a 31 motor_out = pattern[ state & 0x3 ];
okano 11:436e74a3ec7a 32 state += direction ? -1 : 1;
okano 11:436e74a3ec7a 33 wait( 1.0 / speed );
okano 11:436e74a3ec7a 34 }
okano 11:436e74a3ec7a 35 }
okano 11:436e74a3ec7a 36