; STEP_3.ASM
;
; Controls a stepping motor driver on lower four bits of Port B.  
;
; The direction is controlled by a switch on Port B, Bit 7
;
; (Typical drivers might include a ULN2803 Octal Driver, 2N2222 or TIP122
; transistors or power FETs.  See Parallel Port Manual - Vol 1).
; 
; Outputs patterns 0x01, 0x03, 0x02, 0x06, 0x04, 0x0c, 0x08, 0x09, 0x01,
;  .. in sequence to advance stepping motor in one direction.  This is 
;  achieved by causing INDEX to advance from 0 to 7, to 0, etc and 
; mapping the INDEX into a pattern which is then output on the lower 
; nibble of PORTB.
;
; Motor is advanced in the other direction by causing INDEX to move in 
; the opposite direction; 7 to 0 to 7, etc.
;
; Switch on Port B, Bit 7 is read between each output to the stepping 
; motor to determine whether INDEX is to be incremented (0-7, 0) or 
; decremented.
;
; The delay is between steps determines the speed of the stepper.  In 
; this program it is 25 msecs.
;
; copyright,  MSU, June '97
;

	LIST P=PIC16C84

	__CONFIG 11h
#include <c:\mplab\P16C84.INC>

INDEX	EQU 0CH

LOOP1	EQU 10H		; for timing loop
LOOP2	EQU 11H

	ORG 000H

	BCF STATUS, RP1		; bank 1
	BSF STATUS, RP0
	
	MOVLW 0F0
	MOVWF TRISB		; lower four bits ouputs
				; others are inputs
	BCF STATUS, RP0

	CLRF INDEX		; start INDEX at zero
READ_SW:	
	BTFSC PORTB, 7		; read direction switch
	GOTO FORWARD
	GOTO REVERSE

FORWARD:
	INCF INDEX, F		; increment the index
	MOVLW .8
	SUBWF INDEX, W		; and test if equal to 8
	BTFSC STATUS, Z
	CLRF INDEX		; if at 8, set to 0
	GOTO ONE_STEP

REVERSE:
	DECF INDEX, F		; decrement the index
	MOVLW 0FFH
	SUBWF INDEX, W		; test if at 0xFF
	BTFSC STATUS, Z
	GOTO REVERSE_1		; if at FF, then reset to 7
	GOTO ONE_STEP

REVERSE_1:

	MOVLW .7
	MOVWF INDEX
	GOTO ONE_STEP

ONE_STEP:
     	MOVF INDEX, W		; copy count into w
	CALL PATT		; returns the correct pattern in w
	MOVWF PORTB		; output PATT to PORTB
	
	CALL DELAY		; 25 msecs 
	
	GOTO READ_SW
;;;;;;

PATT:	; maps INDEX into appropriate stepping motor pattern
	MOVF INDEX, W		
	ADDWF PCL, F		; add w to the program counter
	
	RETLW	01H		; stepping motor patterns
	RETLW	03H
	RETLW	02H

	RETLW	06H
	RETLW	04H
	RETLW	0CH
	RETLW	08H	
	
DELAY:				; provides nominal 25 msec delay
	MOVLW	.25
	MOVWF 	LOOP1
OUTTER:
	MOVLW	.110
	MOVWF 	LOOP2
INNER:
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	DECFSZ	LOOP2, F
	GOTO INNER
	DECFSZ 	LOOP1, F
	GOTO OUTTER
	RETURN

	END