# Help with C+ program to drive Bipolar Stepper motor using PIC18F1320 Microcontroller

#### zabumafu

Joined Nov 20, 2013
2
Hello I have very little experience and or instructions on how to program this however here is what I have. I am using a pic 18f1320 microcontroller and 2 H-bridges to drive a Mercury Stepper Motor (SM-42BYG011-25) in order for it to rotate one way with the one of the LED's lit on the microcontroller, stop and light a different LED, and then reverse its direction (clockwise to counterclock wise or v.v.) and keep it in a loop. If someone could help me through this that'd be amazing, ill probably be sticking around this forum now that I'm getting into electrical engineering.

The datasheet for the pic18f1320 is below:
PIC18F1220/1320 Data Sheet 18/20/28-Pin High ... - Microchip

I know its suppose to be in 4 parts with the Coil Energization Sequence as follows
(I believe this is the correctly matched pins:
RA0=B3
RA1=B2
RA2=A7
RA3=A6

Coil Energization Sequence
RA0: H-H-L-L-L
RA1: L-L-H-H-L
RA2: H-L-L-H-L
RA3: L-H-H-L-L
with H being high output and L being low and the last L for RA0,1,2,3 being L meaning off. (My professor gave me a circuit diagram for a PIC16F84A and it had this written on it and I am trying to match these RA0 pins to B2, B3 etc. on my Pic 18f1320. A picture of what he gave me and the microcontroller and 2 H bridges are below:

http://i43.tinypic.com/smy7t4.jpg

I am using a PCW compiler program to compile the code in C+ then MPLAB to program the microcontroller. This is what I have thus far for one step but how do I stop it, reverse it, and loop it.
*PLEASE place comments along the code so I know whats going on so I can edit delays etc.

Here is the code I made thusfar and it compiled fine in PCW compiler program:

CODE:

Rich (BB code):
#include <18f1320.h> /*tell compiler we are using a 18f1320.h microcontroller */
#fuses INTRC_IO,NOLVP,NOWDT,PUT
#use delay (clock=4000000)
//LED lights
#define GREEN_LED PIN_A3
#define YELLOW_LED PIN_B0
#define RED_LED PIN_B1
//methods
Main(){
void part_1(void){
Output_low(PIN_A3); //led green
Output_low(PIN_B2);
Output_high(PIN_B3);
Output_high(PIN_A7);
Output_low(PIN_A6);
Delay_ms(2500);
Output_high(PIN_A3);
//turn off led
}
void part_2(void) {
Output_low(PIN_B1); //led yellow
Output_high(PIN_B3);
Output_low(PIN_B2);
Output_low(PIN_A7);
Output_high(PIN_A6);
Delay_ms(2500);
Output_high(PIN_B1);
}
void part_3(void) {
Output_low(PIN_B0); //led red
Output_low(PIN_B3);
Output_high(PIN_B2);
Output_low(PIN_A7);
Output_high(PIN_A6);
Delay_ms(2500);
Output_high(PIN_B0);
}

void part_4(void) {
Output_low(PIN_B0); //led red
Output_low(PIN_B3);
Output_high(PIN_B2);
Output_high(PIN_A7);
Output_low(PIN_A6);
Delay_ms(2500);
Output_high(PIN_B0);
}
}

#### THE_RB

Joined Feb 11, 2008
5,438
To reverse the motor you need to keep separate control of the "count".

That count is the postion of the motor, and turning forward would be;
012301230123
obviously turning in reverse would be;
321032103210

I would split the code into two areas;
1. count sequencing, ie count up 0123 or count down 3210
2. motor control, this sets the pins for each possible state; 0,1,2,3