
;**********************************************************************

	list      p=16f628A           ; list directive to define processor
	#include <p16F628A.inc>       ; processor specific variable definitions

	;errorlevel  -302              ; suppress message 302 from list file

	__CONFIG   _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _INTOSC_OSC_NOCLKOUT 

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.




;***** VARIABLE DEFINITIONS
w_temp        EQU     0x71        ; variable used for context saving 
status_temp   EQU     0x72        ; variable used for context saving





;**********************************************************************
	ORG     0x000             ; processor reset vector
	goto    main              ; go to beginning of program
	

	ORG     0x005             ; interrupt vector location
	movwf   w_temp            ; save off current W register contents
	movf	STATUS,w          ; move status register into W register
	movwf	status_temp       ; save off contents of STATUS register

; isr code can go here or be located as a call subroutine elsewhere


	movf    status_temp,w     ; retrieve copy of STATUS register
	movwf	STATUS            ; restore pre-isr STATUS register contents
	swapf   w_temp,f
	swapf   w_temp,w          ; restore pre-isr W register contents
	retfie                    ; return from interrupt


BANK0	MACRO 
		BCF		STATUS,5
		ENDM

BANK1	MACRO 
		BSF		STATUS,5
		ENDM

CM_TEMP	EQU	0X20				;	ALMACENA EL ESTADO DE LOS SENSORES

		RADIX	DEC


main


		MOVLW	2
		MOVWF	CMCON			; COMPARADOR MODO 2
								;RA0-RA1 = INPUTS
								

		BANK1

		MOVLW	0X03
		MOVWF	TRISA
		CLRF	TRISB
		
		BANKSEL VRCON

		MOVLW	B'10100100'		;ANALOG Vref ENABLE, NO CONECT TO RA2,
		MOVWF	VRCON			;LOW RANGE;  9 --> VR VALUES = 1.875v

		BANKSEL CMCON

		CLRF	PORTB

COMPARA
		BCF	STATUS,C

		MOVF	CMCON,W
		ANDLW	0XC0	

		MOVWF	CM_TEMP
		RRF		CM_TEMP,F
		RRF		CM_TEMP,F
		SWAPF	CM_TEMP,W
		
		
SENSORES

		ADDWF	PCL,F

		GOTO	ADELANTE
		GOTO	IZQUIERDA
		GOTO	DERECHA
		GOTO	ALTO	
	


ADELANTE
		clrf	PORTB
		MOVLW	B'1010'			;	< M1+- M2+- > 
		MOVWF	PORTB
		GOTO	COMPARA

IZQUIERDA
		
		

		clrf	PORTB
		BSF		PORTB,3
;		BCF		PORTB,1			;	M1++  < M2+- >
		
		NOP
		GOTO	COMPARA
		
		
DERECHA
		
		CLRF	PORTB
		BSF		PORTB,1
;		BCF		PORTB,3			;	< M1++ >  M2+-
		
		NOP
		GOTO	COMPARA

ALTO

		CLRF	PORTB
		GOTO	COMPARA
		

		END                	     

