;**********************************************************************
; 
;**********************************************************************
		list      p=12f675   
  		 #include "p12f675.inc"  

		errorlevel -302 	

	__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
; *********************************
; 	Definiciones
; *********************************
   	#define LEDR 5
	#define LEDA 4
	#define	LEDV 2
; *********************************
; 	Variables
; *********************************
   	cblock	0x20
   	ADC_ba
	ADC_al
	Bit
	Vs_al
	Vs_ba
	Vi_al
	Vi_ba
	Vt_al
	Vt_ba
	Sup
	Inf
	Tem			
	Tem_sis
	Tem_sis2
   	endc
; *********************************
; 	Initialisacion
; *********************************
	ORG 	0x00
	goto 	Inicio 				; 
	ORG 	0x04 				; 
	bcf		Bit,	4 
	bcf		INTCON,	T0IF
	goto	Inter

Inicio:
	bsf		STATUS,	RP0	
   	movlw	11h         		;
   	movwf  	ANSEL
	movlw  	B'00000111'			;
   	movwf   VRCON
   	movlw   07h         		;
   	movwf   CMCON
   	movlw   B'00000011'     	;
   	movwf   TRISIO
	movlw 	B'00000010'
	movwf 	WPU
	movlw 	B'00000111'
	movwf 	OPTION_REG
	movlw 	B'10100000'
	movwf	INTCON
	call 	0x3FF 				;  			
	movwf 	OSCCAL 				; 
	bcf		STATUS,	RP0
   	movlw   0x81      			;
   	movwf   ADCON0

	movlw   0x02         
   	movwf   Vs_al
	movlw   0xBC        
   	movwf   Vs_ba

	movlw   0x01         
   	movwf   Vi_al
	movlw   0x2C        
   	movwf   Vi_ba

	movlw   0x01         
   	movwf   Vt_al
	movlw   0x4C        
   	movwf   Vt_ba

	clrf	Bit
	Banksel	GPIO
	clrf	GPIO
	Call	Te_va
	Call	Te_va2
	goto	Ciclo
; ---------------------------------
;	Interrupcion que controla el ciclo de espera y el parpadeo
; ---------------------------------
Inter: 
	DECFSZ		Tem_sis,F
	Retfie
	call		Te_va
	btfsc 		Bit,	2 		;
	goto 		LED1 			; 
	goto 		LED2

LED1:		
	bcf 		Bit,	2
	bsf			GPIO,	LEDV
	goto		$+3

LED2:
	bsf 		Bit,	2
	bcf			GPIO,	LEDV

	DECFSZ		Tem_sis2,F
	Retfie
	bcf			INTCON,	T0IE
	bsf 		Bit,	3
	call		Te_va2
	bsf			GPIO,	LEDV
	bsf			GPIO,   LEDR
	Retfie
; --------------------------------
; 	Prog. principal
; --------------------------------
Ciclo:
Ciclo2:
	btfsc	Bit,	3
	goto	$+4
	btfsc	Bit,	4
	goto	Ciclo
	bsf		Bit,	4
   	call   	Leer_ADC   			;read ADC

   	banksel GPIO
	movf	Vt_al,	W
	movwf   Sup
	movf	Vt_ba,	W
	movwf   Inf
	call	Comp

	btfss	Bit,	1
	goto	$+3
	bsf		GPIO,	LEDA
	goto	$+2
	bcf		GPIO,	LEDA
	
	movf	Vs_al,	W
	movwf   Sup
	movf	Vs_ba,	W
	movwf   Inf
	call	Comp
 
	btfsc	Bit,	1
	goto	$+3
	bsf		Bit,	0
	goto	$+2	
	bcf		Bit,	0

	movf	Vi_al,	W
	movwf   Sup
	movf	Vi_ba,	W
	movwf   Inf
	call	Comp

	btfss	Bit,	0
	goto	Led_a
	btfss	Bit,	1
	goto	Led_a

	goto	Ciclo2	

Led_a:
	bcf		GPIO,	LEDR
	bcf		Bit,	3
	bcf		Bit,	4
	bsf		INTCON,	T0IE
	call	Te_va2
	goto	Ciclo2
; ********************************
Te_va:
	movlw	.06
 	movwf 	Tem_sis
	return
; ********************************
Te_va2:
	movlw	.76					; 
	movwf	Tem_sis2
	return
; --------------------------------
; 	Captura de valores analogicos con el ADC de 10 BIT
; --------------------------------
Leer_ADC:
   banksel 	ADCON0
   bsf    	ADCON0,	1   		;start conversion
   nop
   nop
   nop	
   btfss    PIR1,	ADIF
   goto    	$-4      			;wait for ADC to finish
   movf    	ADRESH, W   		;get high byte
   movwf    ADC_al
   banksel  ADRESL
   movf    	ADRESL, W   		;get low byte
   movwf    ADC_ba
   banksel  PIR1
   bcf     	PIR1,	ADIF		;clear conversion flag
   return
; --------------------------------
; 	Comparador de 16 Bit
; --------------------------------
Comp:
 	movf	Sup,	W
   	subwf	ADC_al,	W

   	btfsc   STATUS,	Z
   	goto    Cont

   	btfsc   STATUS,	C	
	goto	$+3
	bcf		Bit,	1
	goto	Salir
	bsf		Bit,	1
	goto	Salir

Cont:
	movf	Inf,	W
   	subwf	ADC_ba,	W

   	btfsc   STATUS,	Z
	goto	$+2
	goto	$+3
   	bsf		Bit,	1
	goto	Salir

   	btfsc   STATUS,	C
	goto	$+3
	bcf		Bit,	1
	goto	Salir
	bsf		Bit,	1
Salir:
	return
	END