; LED_2B.ASM
;
; Shows how to flash an LED on PortB, Bit 0.  Logic 0 turns LED on.
; The speed of the flash is controlled by a switch at PortA, Bit 2
;
; Timing is done with a simple loop.  
;
; Same as LED_2A.ASM except slight difference in handling selection of
; flash rate.
;
; Peter H. Anderson, MSU, Feb 11, '98  (Not checked)
;

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

#define BASE 0CH

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

	ORG 000H

	BSF STATUS, RP0		; switch to bank 1 and

				; make Portb outputs

	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

TOP:
	BTFSS	PORTA, 2	; bit file skip set
	MOVLW 	.100		; 100 ms delay
	BTFSC	PORTA, 2	
	MOVLW	.250

	MOVWF	N_MSECS		; number of msecs in delay
	CALL	FLASH
	GOTO	TOP

FLASH:	; flashes LED one time with a delay of N_MSECS
	BCF	PORTB, 0	; bit 0 to logic 0
	CALL	DELAY
	BSF	PORTB, 0	; turn LED off
	CALL	DELAY
	RETURN

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

	END