;---------------Encabezado-----------

	__CONFIG   _CP_OFF &  _WDT_OFF & _PWRTE_ON & _XT_OSC
	LIST	   P=16F84A
	INCLUDE  <P16F84A.INC>

;-------Configuración de puertos-------

    cblock  0x0c
	PDel0
	PDel1
	ContadorD
    Activo
    Alto  
    Bajo  
    DIRECCION
    endc
 		
	ORG	0x00
	GOTO	Inicio
	ORG	0X04		; Atiendo la interrupción
    GOTO    Pulsadores

Inicio	
    BSF	    STATUS,5     ; configurando puertos
	MOVLW   B'00011111'
    MOVWF   TRISA
    MOVLW   B'00000001'
    MOVWF   TRISB	       ; puerto B es salida
    MOVLW   B'11000000'
    MOVWF   OPTION_REG
    BCF	    STATUS,5
    MOVLW   b'10010000' ;Interrupcion por PORTB,0
	MOVWF   INTCON
    CLRF    PORTB		; limpiamos PORTB
    CLRF    PORTA
    BCF     Activo,0
Cargar  
    MOVLW   d'3'
    MOVWF   Alto
    MOVLW   d'7'
    MOVWF   Bajo
    CLRF   DIRECCION
    BCF     Activo,0  ;Verifica si el carro esta parado
Periodo  
    MOVF    Activo,W  ; sale de este loop cuando se active con
    SUBLW	D'1'      ; Adelante, Atras; Izquierda, Derecha
    BTFSS   STATUS,Z 
    GOTO    Periodo
    
    MOVF    Bajo,W
    SUBLW	D'10'
    BTFSS   STATUS,Z 
    CALL    Periodo_Alto
    
    MOVF    Alto,W
    SUBLW	D'10'
    BTFSS  STATUS,Z 
    CALL    Periodo_Bajo   
    
    Goto    Periodo
    

Periodo_Alto
    MOVF    DIRECCION,W
    MOVWF   PORTB
    MOVF    Alto,W
    MOVWF   ContadorD
    CALL    DEMORA1000X?
    RETURN
Periodo_Bajo
    MOVLW   B'00000000'
    MOVWF   PORTB
    MOVF    Bajo,W
    MOVWF   ContadorD
    CALL    DEMORA1000X?
    RETURN

DEMORA1000X?	
        MOVLW	.74
	    MOVWF	PDel0
	    MOVLW	.2
        MOVWF	PDel1
LOOP	
        DECFSZ	PDel0,F
	    GOTO	LOOP
	    DECFSZ	PDel1,F
	    GOTO	LOOP
	    decfsz	ContadorD		; decrementa cantidad de veces
        goto	DEMORA1000X?
	    Return  
    
.
    CBLOCK	
	Guarda_W
	Guarda_STATUS
	Guarda_ContadorD
	ENDC
    
Pulsadores
    movwf	Guarda_W		; Guarda W y STATUS.
	swapf	STATUS,W		; Ya que "movf STATUS,W" corrompe el bit Z.
	movwf	Guarda_STATUS
	bcf	    STATUS,RP0		; Para asegurarsede que trabaja con el Banco 0.
	movf	ContadorD,W		; Guarda los registros utilizados en esta 
	movwf	Guarda_ContadorD		; subrutina y también en la principal.
	
	
    SUBLW	D'20'  
    MOVWF   ContadorD
    CALL	DEMORA1000X?                 
    
    MOVF   PORTA,W            
    XORLW  0x05          ;   b'00000101'  d'05'
    BTFSC  STATUS,Z
    GOTO   ADELANTE
    MOVF   PORTA,W       
    XORLW  0x0A          ;   b'00001010'  d'10'
    BTFSC  STATUS,Z
    GOTO   ATRAS
    MOVF   PORTA,W       
    XORLW  0x06          ;   b'00000110'  d'06'
    BTFSC  STATUS,Z
    GOTO   DERECHA
    MOVF   PORTA,W       
    XORLW  0x09          ;   b'00001001'  d'09'
    BTFSC  STATUS,Z
    GOTO   IZQUIERDA
    MOVF   PORTA,W       
    XORLW  0x03          ;   b'00000011'  d'03'
    BTFSC  STATUS,Z
    GOTO   PARAR
    MOVF   PORTA,W       
    XORLW  0x02          ;   b'00000010'  d'02'
    BTFSC  STATUS,Z
    GOTO   Vel_1
    MOVF   PORTA,W       
    XORLW  0x0E          ;   b'00001110'  d'14'
    BTFSC  STATUS,Z
    GOTO   Vel_2 
    MOVF   PORTA,W       
    XORLW  0x07          ;   b'00000111'  d'07'
    BTFSC  STATUS,Z
    GOTO   Vel_3   
    GOTO   FinInterrupcion

ADELANTE
    BSF    Activo,0
    MOVLW  b'10101100'
	MOVWF  DIRECCION
    GOTO   FinInterrupcion

ATRAS
    BSF    Activo,0
    MOVLW  b'01011100'   
	MOVWF  DIRECCION
    GOTO   FinInterrupcion
    
DERECHA
    BSF    Activo,0
    MOVLW  b'10011100'
	MOVWF  DIRECCION
    GOTO   FinInterrupcion
    
IZQUIERDA
    BSF    Activo,0
    MOVLW  b'01101100'
	MOVWF  DIRECCION
    GOTO   FinInterrupcion

PARAR
    MOVLW   d'3'
    MOVWF   Alto
    MOVLW   d'7'
    BCF    Activo,0
    MOVLW  b'00000000'
    MOVWF  DIRECCION
    GOTO   FinInterrupcion
    
Vel_1
    MOVF    Activo,W   ; Si esta en movimiento puedo
    SUBLW	D'0'       ; afectar la velocidad
    BTFSC   STATUS,Z 
    GOTO    FinInterrupcion
    MOVLW   d'3'
    MOVWF   Alto
    MOVLW   d'7'
    MOVWF   Bajo
    GOTO   FinInterrupcion

Vel_2
    MOVF    Activo,W   ; Si esta en movimiento puedo
    SUBLW	D'0'       ; afectar la velocidad
    BTFSC   STATUS,Z 
    GOTO    FinInterrupcion
    MOVLW   d'6'
    MOVWF   Alto
    MOVLW   d'4'
    MOVWF   Bajo
    GOTO   FinInterrupcion

Vel_3
    MOVF    Activo,W   ; Si esta en movimiento puedo
    SUBLW	D'0'       ; afectar la velocidad
    BTFSC   STATUS,Z 
    GOTO    FinInterrupcion
    MOVLW   d'10'
    MOVWF   Alto
    MOVLW   d'0'
    MOVWF   Bajo
    GOTO   FinInterrupcion

FinInterrupcion
	movf	Guarda_ContadorD,W	; Restaura los registros utilizados en esta 
	movwf	ContadorD			; subrutina y también en la principal.
	swapf	Guarda_STATUS,W		; Restaura el STATUS.
	movwf	STATUS
	swapf	Guarda_W,F		; Restaura W como estaba antes de producirse
	swapf	Guarda_W,W		; interrupción.
	Bcf	   INTCON,INTF
	retfie
    
;==========================================================================
    END
;==========================================================================

