; Progam INTER.BAS

; This program continuosly turns a stepper motor in a direction until it
; receives a low on the IRQ pin, then it momentarily lights an LED, and
; reverses the direction of the stepper motor. 

; copyright Locksley Haynes, Morgan State University, Oct. 21, 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 LED pin_D7    ;pin for LED

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

FUNC none irq
BEGIN

   pin_high( LED )     ;light LED for
   delay(2000)         ;2 seconds
   pin_low( LED )

   IF and(DIREC, 1b)      ;change direction of
      =(DIREC, 0b)        ;stepper motor
   ELSE
      =(DIREC, 1b)
   ENDIF

   irq_on()        ;turn irq back on before exiting function
ENDFUN

;********************Main function****************************
FUNC none main
  LOCAL byte step_index 0b
  
BEGIN
   rs_param_set(debug_pin)
   irq_on()            ;turn on irq
   =(DIREC, 1b)        ;initial stepper direction direction
   REP
     
     dtris_set(0b)   ;set data port direction register for output.
     dport_set(step_pattern[step_index])   ;output pattern to stepper
motor

     IF and(DIREC, 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(25)
   LOOP
ENDFUN

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