# Stepper Motor Control: Software

The Software
The software for this tutorial is the key to controlling the stepper motor. The entire program is too much to go through in this one page so we'll just look at once chunk of the code. I've commented pretty thoroughly so any questions you might have should be answered.

« Begin Code »
```/*
Written by: Chris
Date: 12/3/2007
Purpose & Description: This program controls a stepper motor
It is just a snippet of the full program file that you can
*/
//Initializations
PORTA = 0x01; //Turn LED On
PORTCbits.RC1 = 0; //Direction Pin LMD18245 #1
PORTCbits.RC3 = 0; //Direction Pin LMD18245 #2

//The for loop below completes 251 full steps.
for(count=0;count<=250;count++)
{
PORTCbits.RC1 = 1; //Pull The Motor
PORTCbits.RC3 = 1; //Pull The Motor
Delay100TCYx(100);

PORTCbits.RC1 = 0; //Push The Motor
PORTCbits.RC3 = 1; //Pull The Motor
Delay100TCYx(100);

PORTCbits.RC1 = 0; //Push The Motor
PORTCbits.RC3 = 0; //Push The Motor
Delay100TCYx(100);

PORTCbits.RC1 = 1; //Pull The Motor
PORTCbits.RC3 = 0; //Push The Motor
Delay100TCYx(100);

PORTCbits.RC1 = 1; //Pull The Motor
PORTCbits.RC3 = 1; //Pull The Motor

Delay100TCYx(100); //Delay For Speed Variation
}```

« End Code »

The Push/Pull idea seen in the comments is the best way I could name what is happening for the purpose of understanding. The pushing and pulling is happening by way of induction so nothing is actually touching the motor.

The Direction of Current Flow
To better explain what is happening with the current flow direction here's another little snippet from the comments in the program:

Each step consists of the following cycle:
AB -> A*B -> A*B* -> AB* -> AB (delay here for speed control)

Where A is the direction for LMD18245 #1
and B is the direction for LMD18245 #2

Where: A = 1, A* = 0, B = 1, B* = 0

The Pulse
Another thing to notice is that after each direction change we see this delay: Delay100TCYx(100); Inorder for the stepper motors to work properly, the motor controller defines that the each step 'pulse' (as described above) must occur within the range of 20uS to 20mS. For this example we use a 20mS delay.

Now that we know how the hardware is put together & how the software will control everything. It is time to take a look at the finished product.

;