;Progam STEP_2.BAS

;This program continuosly turns a stepper motor in a direction depending
;on the state of pin_D7. 

;copyright Locksley Haynes, Morgan State University, Oct. 7, 1997

;****************** Circuit Diagram *************************
;TICKit ------------------>  DRIVER (ULN 2803A)
;DATA_0 (term 31) --------> D0 (term 1) 
;DATA_1 (term 32) --------> D1 (term 2)
;DATA_2 (term 33) --------> D2 (term 3)
;DATA_3 (term 34) --------> D3 (term 4)

;DRIVER ------------------> MOTOR
;Q0 (term 18) --------> PHI_0
;Q1 (term 17) --------> PHI_1 
;Q2 (term 16) --------> PHI_2 
;Q3 (term 15) --------> PHI_3 

;***************************************************************

DEF tic62_b       ;define TICkit device
LIB fbasic.lib    ;library for FBASIC  functions
LIB constrin.lib  ;library for console writing functions

DEF DIREC pin_D7

;Initialize step pattern values
GLOBAL byte step_pattern [8]  1b 3b 2b 6b 4b 12b 8b 9b

;********************Main function****************************
FUNC none main
  LOCAL byte step_index 0b
  LOCAL byte direction 0b
BEGIN

   REP
     =(direction, and(pin_in( DIREC ), 1b)) ;read pin_D7 for direction
     
     dtris_set(0b)   ;set data port direction register for output.
     dport_set(step_pattern[step_index])   ;output pattern to stepper
motor

     IF ==(direction, 1b)      ;if direction = 1, turn one way  
        IF ==(step_index, 7b)       ;if at end of pattern
            =(step_index, 0b)       ;start over
        ELSE
          ++(step_index)            ;increment to next pattern
        ENDIF
     ELSE                      ;else turn other way
        IF ==(step_index, 0b)       ;if at end of pattern
            =(step_index, 7b)       ;startover
        ELSE
          --(step_index)                    ;decrement to next pattern
        ENDIF
     ENDIF

     delay(50)
   LOOP
ENDFUN

;*********************************************************************