;*********************************************************************
;*********************************************************************
;**                          CONTROL PC					            **
;**															       	**
;**																	**
;*********************************************************************
;*********************************************************************
		
		TITLE CONTROL_PC
		LIST P=16F84A
        #INCLUDE "P16F84A.INC"

		__CONFIG _XT_OSC & _PWRTE_ON & _CP_OFF & _WDT_OFF
;*********************************************************************
;**				MEMORIA PARA CODIGOS DE ENTRADA Y VARIABLES			**
;*********************************************************************


digit			equ	0x13
var_4			equ 0x14
mem_dir			equ 0x15
cont_4			equ 0x16
cont_5			equ 0x17
cont_6			equ 0x18
status_w		equ 0x19
status_o		equ 0x20
v_eep			equ 0x21
;*********************************************************************
;**			DECLARACION DE CLAVES DE ENCENDIDO-APAGADO MANUAL		**
;*********************************************************************


;*********************************************************************
;**				    	INICIO DEL PROGRAMA							**
;*********************************************************************

		org		0
		goto	inicio

;*********************************************************************
;**			 	INICIALIZACIÓN DE PUERTOS Y VARIABLES				**
;*********************************************************************

inicio
		bsf		STATUS,5
        movlw   0xfe
        movwf   TRISA
        movlw   0x00
		movwf   TRISB
		bcf		STATUS,5

		clrf	PORTA
		clrf	PORTB
		clrf	v_eep
		clrw

;*********************************************************************
;**				    		PROGRAMA PRINCIPAL						**
;*********************************************************************

principal
		call	le_estado
		bsf		PORTB,0
loop	call	leer_codigo
		call	ej_estado
		goto	loop

;*********************************************************************
;**				    	SUBRUTINA LE_ESTADO							**
;** Descripción: Ejecuta el estado anterior de las salidas grabado	**
;**				 en la eeprom.										**
;*********************************************************************
le_estado
		movlw	00h
		movwf	mem_dir
		call	read_eeprom
		call	delay_2
		movlw	02fh
		subwf	status_w,w
		btfss	STATUS,Z
		goto	h_1
		goto	h_2

h_1		bsf		PORTB,5
		return
h_2		bcf		PORTB,5
		return

;*********************************************************************
;**				    	SUBRUTINA LEER_CODIGO						**
;** Descripción: Recibe 4 dígitos BCD correspondientes a una clave	**
;**				 ingresada y los almacena en memoria.				**
;*********************************************************************

leer_codigo
		rrf		PORTA,w
		andlw	0fh
		movwf	var_4

test_1	rrf		PORTA,w
		andlw	0fh
		subwf	var_4,w
		btfsc	STATUS,Z
		goto	test_1
		rrf		PORTA,w
		andlw	0fh		
		movwf	digit
		bcf		PORTB,0
		call	delay_2
		rrf		PORTA,w
		andlw	0fh
		movlw	var_4
		bsf		PORTB,0
		return

;*********************************************************************
;**				    	SUBRUTINA EJEC_ESTADO						**
;** Descripción: Compara los tres digitos en memoria y los compara	**
;**				 con todas las claves guardadas para definir una	**
;**				 tarea a realizar.									**
;*********************************************************************

ej_estado
		movf	digit,w
		xorlw	05h
		btfsc	STATUS,Z
		goto	task_1
		movf	digit,w
		xorlw	06h
		btfsc	STATUS,Z
		goto	task_2
		return

task_1	bsf		PORTB,5
re_wr	movlw	00h
		movwf	mem_dir
		movlw	7fh
		movwf	status_o
		call	write_eeprom
		call	verify_write
		movf	v_eep,w
		xorlw	01h
		btfss	STATUS,Z
		goto	re_wr
		return

task_2	bcf		PORTB,5
re_wr2	movlw	00h
		movwf	mem_dir
		movlw	02fh
		movwf	status_o
		call	write_eeprom	
		call	verify_write
		movf	v_eep,w
		xorlw	01h
		btfss	STATUS,Z
		goto	re_wr2
		return

;*********************************************************************
;**				    	SUBRUTINA WRITE_EEPROM						**
;** Descripción: Escribe en la memoria no volatil, el estado en el	**
;**				 que se encuentran las salidas.						**
;*********************************************************************

write_eeprom
		bcf		STATUS,5
		movf	status_o,w		
		movwf	EEDATA
		movf	mem_dir,w
		movwf	EEADR

		bsf		STATUS,5
		bcf		INTCON,GIE
		bsf		EECON1,WREN
		movlw	55h
		movwf	EECON2
		movlw	0aah
		movwf	EECON2
		bsf		EECON1,WR
		bsf		INTCON,GIE
		bcf		STATUS,5
		return

;*********************************************************************
;**				    	SUBRUTINA VERIFY_WRITE						**
;** Descripción: Verifica que el dato en la eeprom ha sido grabado	**
;*********************************************************************
verify_write
		call	read_eeprom
		movf	status_w,w
		subwf	status_o,w
		btfss	STATUS,Z
		goto	g_1
		goto	g_2
g_1		movlw	00h
		movwf	v_eep
		return
g_2		movlw	01h
		movwf	v_eep
		return
;*********************************************************************
;**				    	SUBRUTINA READ_EEPROM						**
;** Descripción: Recupera de la memoria no volatil el estado en el  **
;**				 que se encontraban las salidas.					**
;*********************************************************************

read_eeprom
		bcf		STATUS,5
		movf	mem_dir,w
		movwf	EEADR
		bcf		INTCON,GIE
		bsf		STATUS,5
		bsf		EECON1,RD
		bcf		STATUS,5
		bsf		INTCON,GIE
		movf	EEDATA,w
		movwf	status_w
		return


;*********************************************************************
;**				    	SUBRUTINA DELAY_2							**
;** Descripción: Introduce un retardo de aprox. 1s.	    			**
;*********************************************************************
delay_2
		movlw	0
		movwf	cont_4
		movlw	0
		movwf	cont_5
		movlw	5
		movwf	cont_6
loop_5	decfsz	cont_4,f
		goto	loop_5
		decfsz	cont_5,f
		goto	loop_5
		decfsz	cont_6,f
		goto	loop_5
		return
		end