/***************************************************************************/ /* Stepper Motor Driver */ /* */ /* Stepper.c */ /* */ /* Version : 1.0 */ /* */ /* By : Julian Winpenny */ /* */ /* Date : 25/6/2000 */ /* */ /* Purpose : Illustrate Driving a Stepper Motor */ /* Runs Clockwise slow then Anticlockwise fast */ /* */ /* Notes: Stepper motor driving transistors */ /* connected to 4 bits Port B */ /* */ /* Add a Photo switch to detect */ /* position. */ /* */ /***************************************************************************/ void StepIt( char Direction, char Speed ); char stepper[] = {5,9,10,6}; char cPos; #define Clockwise 0; #define AntiClockwise 3; void main(void) { int x; // Set up the PIC ports etc. set_bit(STATUS, RP0); set_tris_b(0); clear_bit(STATUS, RP0); while(1) { for ( x=0; x < 100 ; x++ ) { StepIt( Clockwise, 50 ); } delay_s(2); for ( x=0; x < 200 ; x++ ) { StepIt( AntiClockwise, 10 ); } } } /******************************/ /* Move the Motor */ /* */ /* */ /******************************/ void StepIt( char Direction, char Speed ) { // Initialise if needed if ( cPos > 3 ) cPos = 0; // Do each step sequence output_port_b(stepper[cPos]); // Set speed delay_ms( Speed ); switch ( Direction ) { case Clockwise: if( cPos++ == 4 ) cPos = 0; break; case AntiClockwise: if( cPos-- == 0 ) cPos = 3; break; } }