; STEP_4.ASM (Variable Direction and Speed) - 16C84
;
; Controls a stepping motor driver on lower four bits of Port B.  
;
; The direction is controlled by a switch on Port B, Bit 7.
; Speed is controlled by settings of three switcches on Port B, Bits 4, 
; 5, 6.
;
; (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.  Speed 
; bits; (PORTB, Bits 6, 5 and 4) are read and mapped into a delay time 
; using indirect table lookup CALC_TIME.
;
; 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		
	
	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 delay determined by PORTB, bits 6, 5 and 4
	SWAPF PORTB, W	; bits in W are now 3, 2, 1, 0, 7, 6, 5, 4
	ANDLW 07H	; consider only bits 6, 5, 4	
	CALL CALC_TIME	; map this into a delay in msecs
	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

CALC_TIME:		; maps 0-7 into a delay constant
	ADDWF PCL, F
	DT .1, .2, .4, .8, .16, .32, .64, .128
	; note that the define table (DT) directive generates the 
	; RETLW instructions.  It saves a lot of tedious typing.

	END