#include <p18f452.h>
#include <delays.h>
void main(void)
{
int count=0;

TRISA = 0x00; //Initialize these ports as digital outputs
TRISC = 0x00;
TRISD = 0x00;

PORTA = 0x01; //Turn the LED On
PORTD = 0x0F; //Full Speed

PORTCbits.RC0 = 0;	//Brake 	LMD18245 #1
PORTCbits.RC1 = 0;	//Direction LMD18245 #1
PORTCbits.RC2 = 0;	//Brake 	LMD18245 #2
PORTCbits.RC3 = 0;	//Direction 	LMD18245 #2

	while(1)
	{

	PORTD = 0x0F;	//M1-M4 Always On
	PORTA = 0x00;	//Turn LED Off
	/*
	The next for loop turns the stepper motor at full speed forward.
	Notice that the only thing we change are the direction pins on the LMD18245's.
	
	Each step consists of the following cycle:
	AB -> A*B -> A*B* -> AB* -> AB
	
	Where A is the direction for LMD18245 #1 
	and B is the direction for LMD18245 #2
	
	A = 1, A* = 0, B = 1, B* = 0
	*/
		for(count=0;count<=250;count++)
		{
			PORTCbits.RC1 = 1;		
			PORTCbits.RC3 = 1;
			Delay100TCYx(100);

			PORTCbits.RC1 = 0;
			PORTCbits.RC3 = 1;
			Delay100TCYx(100);

			PORTCbits.RC1 = 0;
			PORTCbits.RC3 = 0;
			Delay100TCYx(100);

			PORTCbits.RC1 = 1;
			PORTCbits.RC3 = 0;
			Delay100TCYx(100);

			PORTCbits.RC1 = 1;		
			PORTCbits.RC3 = 1;
		}

	PORTA = 0x01;	//Turn LED On
	/*
	The next for loop turns the stepper motor forward as well but....
	Now we will put a pause at the end of each step to slow the stepper motor down.
	Notice again that the only thing we change are the direction pins on the LMD18245's.
	
	Each step consists of the following cycle:
	AB -> A*B -> A*B* -> AB* -> AB (pause for 10,000 instruction cycles)
	
	Where A is the direction for LMD18245 #1 
	and B is the direction for LMD18245 #2
	
	A = 1, A* = 0, B = 1, B* = 0
	*/	
		for(count=0;count<=250;count++)
		{
			PORTCbits.RC1 = 1;		
			PORTCbits.RC3 = 1;
			Delay100TCYx(100);

			PORTCbits.RC1 = 0;
			PORTCbits.RC3 = 1;
			Delay100TCYx(100);

			PORTCbits.RC1 = 0;
			PORTCbits.RC3 = 0;
			Delay100TCYx(100);

			PORTCbits.RC1 = 1;
			PORTCbits.RC3 = 0;
			Delay100TCYx(100);

			PORTCbits.RC1 = 1;		
			PORTCbits.RC3 = 1;

			Delay100TCYx(100);
		}

	PORTA = 0x00;	//Turn LED Off
	/*
	The next for loop turns the stepper motor at full speed in reverse.
	Notice again that the only thing we change are the direction pins on the LMD18245's.
	
	Each step consists of the following cycle:
	AB -> AB* -> A*B* -> A*B -> AB
	         |                     |
	         V                    V
	These two elements are the only things we need to alter
	to make the	stepper move in reverse
	
	Where A is the direction for LMD18245 #1 
	and B is the direction for LMD18245 #2
	
	A = 1, A* = 0, B = 1, B* = 0
	*/	
		for(count=0;count<=250;count++)
		{
			PORTCbits.RC1 = 1;		
			PORTCbits.RC3 = 1;
			Delay100TCYx(100);

			PORTCbits.RC1 = 1;
			PORTCbits.RC3 = 0;
			Delay100TCYx(100);

			PORTCbits.RC1 = 0;
			PORTCbits.RC3 = 0;
			Delay100TCYx(100);

			PORTCbits.RC1 = 0;
			PORTCbits.RC3 = 1;
			Delay100TCYx(100);

			PORTCbits.RC1 = 1;		
			PORTCbits.RC3 = 1;
		}

	PORTA = 0x01;	//Turn LED On
	/*
	The next for loop turns the stepper motor reverse as well but....
	Now we will put a pause at the end of each step to slow the stepper motor down.
	Notice again that the only thing we change are the direction pins on the LMD18245's.
	
	Each step consists of the following cycle:
	AB -> AB* -> A*B* -> A*B -> AB (pause for 10,000 instruction cycles)
	         |                     |
	         V                    V
	These two elements are the only things we need to alter
	to make the	stepper move in reverse
	
	Where A is the direction for LMD18245 #1 
	and B is the direction for LMD18245 #2
	
	A = 1, A* = 0, B = 1, B* = 0
	*/	
		for(count=0;count<=250;count++)
		{
			PORTCbits.RC1 = 1;		
			PORTCbits.RC3 = 1;
			Delay100TCYx(100);

			PORTCbits.RC1 = 1;
			PORTCbits.RC3 = 0;
			Delay100TCYx(100);

			PORTCbits.RC1 = 0;
			PORTCbits.RC3 = 0;
			Delay100TCYx(100);

			PORTCbits.RC1 = 0;
			PORTCbits.RC3 = 1;
			Delay100TCYx(100);

			PORTCbits.RC1 = 1;		
			PORTCbits.RC3 = 1;

			Delay100TCYx(100);
		}
	}
}
