; Remember that, for the microswitches (SW4..SW3), SW4 is the most significant bit
; and the ON position=0, the OFF position=1.
; Make a program in assembler
; a table by retlw instructions, with the following 8 hexadecimal data:
; AA 20 D9 3C 16 0B 9F 5E
; Show on the LEDs, LED7 .. LED0 in binary code, consecutively, each of these
; values for 1 second. If the 4 least significant bits of the data displayed on the LEDs is equal
; to the value of the microswitches SW1.. SW4 .. (n = 0 ... F), the LEDs should be kept showing the same data.
; Program concludes with sleep instruction.
; If none of the data coincides with the value of the microswitches, the program displays data
; and ends the table showing the latest data.

	processor 18F2550
	include	  <p18F2550.inc>
	include	 "KEYBOLT2.inc"	;Auxiliary file and macro

J	equ	H'80'	  
K	equ	H'81'
L	equ 	H'82'
CLAVE100 equ H'83'
CONT	equ	H'84'
DUMMY	equ	H'85'
	 
		org 	0x800	;modify to org=0 to simulate in MPLAB
		PUERTOS
		BOLTSWITCH
		movlw	H'8'
		movwf	PCLATH
		CLRF	PORTB
		CLRF	CONT
malla	movf	CONT,w
		call	tabla
		movwf	PORTB
		andlw	H'0F'
		subwf	CLAVE100,w
		btfsc	STATUS,Z
		sleep
		call	retrobolt
		incf	CONT,f
		incf	CONT,f
		movf	CONT,w
		sublw	D'16'
		btfss	STATUS,Z
		goto	malla
		sleep
		
		org 	0x880		

tabla	addwf	PCL,f
		retlw	H'AA'
		retlw	H'20'
		retlw	H'3C'
		retlw	H'5E'
		retlw	H'D9'
		retlw	H'16'
		retlw	H'0B'
		retlw	H'9F'

;delay subroutine, aprox 1 second. modify to (16.4 x L) MILISECONDS

retrobolt:	movlw	D'61'	;valor de L. Modifique para cambiar el valor del retraso.
		movwf	L
lloop		movlw	D'255'	; w := 200 decimal
		movwf	J	; J := w
jloop:		movwf	K	; K := w
kloop:		decfsz	K,f	; K = K-1, skip next if zero
		goto 	kloop
		decfsz	J,f	; J = J-1, skip next if zero
		goto	jloop
		decfsz	L,f
		goto	lloop
		return


		end	

