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

main.cpp.orig

Committer:
okano
Date:
2014-01-11
Revision:
11:436e74a3ec7a
Parent:
6:11b35048d384

File content as of revision 11:436e74a3ec7a:

//  stepper motor operation sample (learn it step by step).
//  showing how to control a unipolar stepper motor by mbed digital output ports.
//
//  version 2 : 360 steps clockwise and counterclockwise rotations

#include "mbed.h"

DigitalOut  motor_out0( p26 );
DigitalOut  motor_out1( p25 );
DigitalOut  motor_out2( p24 );
DigitalOut  motor_out3( p23 );

#define     INTERVAL    0.01

int main()
{
    while(1) {

        //  pulse orser : "p26 -> p25 -> p24 -> p23" for 4*90(=360) steps

        for ( int i = 0; i < 90; i++ ) {
            motor_out0  = 1;
            motor_out1  = 0;
            motor_out2  = 0;
            motor_out3  = 0;
            wait( INTERVAL );

            motor_out0  = 0;
            motor_out1  = 1;
            motor_out2  = 0;
            motor_out3  = 0;
            wait( INTERVAL );

            motor_out0  = 0;
            motor_out1  = 0;
            motor_out2  = 1;
            motor_out3  = 0;
            wait( INTERVAL );

            motor_out0  = 0;
            motor_out1  = 0;
            motor_out2  = 0;
            motor_out3  = 1;
            wait( INTERVAL );
        }

        //  pulse orser : "p23 -> p24 -> p25 -> p26" for 4*90(=360) steps

        for ( int i = 0; i < 90; i++ ) {
            motor_out0  = 0;
            motor_out1  = 0;
            motor_out2  = 0;
            motor_out3  = 1;
            wait( INTERVAL );

            motor_out0  = 0;
            motor_out1  = 0;
            motor_out2  = 1;
            motor_out3  = 0;
            wait( INTERVAL );

            motor_out0  = 0;
            motor_out1  = 1;
            motor_out2  = 0;
            motor_out3  = 0;
            wait( INTERVAL );

            motor_out0  = 1;
            motor_out1  = 0;
            motor_out2  = 0;
            motor_out3  = 0;
            wait( INTERVAL );
        }
    }
}