## 1 Basic Control Methods

Basic Control Methods

First of all it is easier to define the pins you will be using to control your stepper motor:

#define mout1 PORTDbits.RD0

#define mout2 PORTDbits.RD1

#define mout3 PORTDbits.RD2

#define mout4 PORTDbits.RD3

If you have read the other tutorials, you will no doubt have seen the different methods of control. Wave, Full Step and Half Step, I like to implement these as a 3 dimensional array:-

//phaseArray[phasetype][phaseposition][outputnumber]

//phasetype:- 0 = wave ; 1 = full step ; 2 = half step

char phaseArray[3][8][4]={

{{1,0,0,0},{1,0,0,0},

{0,0,1,0},{0,0,1,0},

{0,1,0,0},{0,1,0,0},

{0,0,0,1},{0,0,0,1}},

{{1,0,1,0},{1,0,1,0},

{0,1,1,0},{0,1,1,0},

{0,1,0,1},{0,1,0,1},

{1,0,0,1},{1,0,0,1}},

{

{1,0,0,0},

{1,0,1,0},

{0,0,1,0},

{0,1,1,0},

{0,1,0,0},

{0,1,0,1},

{0,0,0,1},

{1,0,0,1}

}

};

For wave and full step mode, we should only need to supply 4 elements for the 2nd array list but to integrate half step mode into the array we need to double the sequences up for wave and full step modes. This means that we need to increment these two modes by 2 for each step.

so in order to use the array you will need to do something like this:

mout1 = phaseArray[*phasetype*][*phaseposition*][0];

mout2 = phaseArray[*phasetype*][*phaseposition*][1];

mout3 = phaseArray[*phasetype*][*phaseposition*][2];

mout4 = phaseArray[*phasetype*][*phaseposition*][3];

mout refers to the pins we have defined earlier

*phasetype* refers to the 1st array dimension, if we set this as 1 then we will be operarting with full step mode

*phaseposition* refers to what step cycle we are currently on. Incrementing this by 2 will send us to the next step cycle in the array.

the number in the 3rd array dimension just refers to the port state for the current cycle.

so looking at the array defined above:

mout1 = phaseArray[1][0][0];

mout2 = phaseArray[1][0][1];

mout3 = phaseArray[1][0][2];

mout4 = phaseArray[1][0][3];

would produce the following:

mout1 = 1

mout2 = 0

mout3 = 1

mout4 = 0

Here is a full program that uses the array above to control a stepper motor with continuous forward motion

The program controls the steps at 5000us Lets try it out

Its working, but its a bit slow. Lets try with a 1500us step delay intead

Its better but still a bit slow. Lets try it with a 500us step delay

Whoops, it stalls. It wont move at all at 500us. Is 1500us the fastest we can go then?

Well the answer is no. We can move a lot faster.

Stepper motors have to start slow in order for the coils to catch. Once its spinning, you can slowly ramp up the speed.

Notice in the videos how the motors are moving bit they are really jittery, they are suffering from mechanical resonance at a slow speed.(more on this later).

Follow to the next page to see how to drive your motor faster.