; LOOK_1.ASM
;
; Reads input RA.2.  Based on value, the program branches to one of two
; places.
;
; At PLACE_0, four LEDs on lower four bits of PORTB are rolled in an
; arrow form in one direction.  At PLACE_1, in the other direction.
;
; Peter H. Anderson, MSU, Feb 16, '98  (Not checked)
;

	LIST p=16c84	
#include <:\mplab\p16c84.inc>
	__CONFIG 11h

#define BASE 0CH

LOOP1	EQU BASE+0
LOOP2	EQU BASE+1
N_MSECS	EQU BASE+2

INDEX	EQU BASE+3
N	EQU BASE+4

	ORG 000H

	CALL DIRS		; set PORTB to all outputs
				; RA.0 and RA.1 outputs
				; RA.4, RA.3, RA.2 inputs

TOP:
	MOVF 	PORTA, W	; fetch from PORTA
	MOVWF	INDEX
	RRF	INDEX, F	
	RRF	INDEX, F
	MOVLW	01H
	ANDWF	INDEX, W	; INDEX is now RA.2

	ADDWF	PCL, F		; this is the equiv of Basic Stamp BRANCH
	GOTO	PLACE_0
	GOTO	PLACE_1
		
PLACE_0:		; arrow in one direction
	MOVLW 	.4
	MOVWF	N

PLACE_0_0:
	MOVF	N, W
	SUBLW	.4		; W = 4-W	
	CALL	LOOK_0
	MOVWF	PORTB
	MOVLW	.100
	CALL	DELAY
	DECFSZ	N, F
	GOTO 	PLACE_0_0

	GOTO 	TOP

LOOK_0:		; this is the equivalent of the Basic Stamp's LOOKUP
command
	ADDWF	PCL, F
	RETLW	1H	; %0001
	RETLW	3H	; %0011
	RETLW	7H	; %0111
	RETLW	0FH	; %1111

PLACE_1:		; arrow in other direction

	MOVLW 	.4
	MOVWF	N

PLACE_1_0:
	MOVF	N, W	
	SUBLW	.4	; w = 4 -w
	CALL	LOOK_1
	MOVWF	PORTB
	MOVLW	.100
	CALL	DELAY
	DECFSZ	N, F
	GOTO 	PLACE_1_0

	GOTO 	TOP

LOOK_1:	ADDWF	PCL, F
	RETLW	08H	; %1000
	RETLW	0CH	; %1100
	RETLW	0EH	; %1110
	RETLW	0FH	; %1111

DELAY:	; delays N_MSECS
	; note that N_MSECS is set in main

	MOVF	N_MSECS, W	
	MOVWF	LOOP1		; LOOP1 = N_MSECS
OUTTER:
	MOVLW	.110	; close to 1.0 msec delay when set to .110
	MOVWF 	LOOP2
INNER:
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	DECFSZ	LOOP2, F
	GOTO INNER
	DECFSZ 	LOOP1, F
	GOTO OUTTER
	RETURN

DIRS:

	BSF STATUS, RP0		; switch to bank 1 and
				
	CLRF TRISB		; 0 indicates an output

				; now PORTA
	BCF TRISA, 0		; make lower two bits of Porta outputs
	BCF TRISA, 1		;

	BSF TRISA, 2		; make bits 4, 3 and 2 inputs
	BSF TRISA, 3
	BSF TRISA, 4 

	BCF STATUS, RP0		; back to data bank 0
	RETURN

	END