	LIST 	P=16F877A
		#INCLUDE<P16F877A.INC>

RESULT_HIG   	EQU	20
RESULT_LOW		EQU	21
UNIDADES		EQU	22  
DECENAS			EQU	23	
R3272_HIG		EQU	24
R3272_LOW 		EQU	25
mA			EQU	26
PDel0 			EQU	27
PDel1			EQU	28
PDel2			EQU	29
R_4mA	  		EQU	2A
REF			EQU	2B
MEMORIA			EQU	2C
COMP			EQU	2D
mA_NEG      	        EQU	2E
LUC			EQU	2F

;BITS CONVERSOR
ADIF			EQU	6	
GODONE			EQU	2
ADON			EQU	0
ADFM			EQU	7
C			EQU	0


                                                                      
                                                                                                                                
	ORG	0
	GOTO INICIO
	ORG 05



RETARDO 
; Code generated by PDEL  ver 1.0  on 05/08/2007 at 11:57:49 p.m.
; Description: Waits 1000000 cycles
;-------------------------------------------------------------
PDelay  movlw     .14       ; 1 set number of repetitions (C)
        movwf     PDel0     ; 1 |
PLoop0  movlw     .72       ; 1 set number of repetitions (B)
        movwf     PDel1     ; 1 |
PLoop1  movlw     .247      ; 1 set number of repetitions (A)
        movwf     PDel2     ; 1 |
PLoop2  clrwdt              ; 1 clear watchdog
        decfsz    PDel2, 1  ; 1 + (1) is the time over? (A)
        goto      PLoop2    ; 2 no, loop
        decfsz    PDel1,  1 ; 1 + (1) is the time over? (B)
        goto      PLoop1    ; 2 no, loop
        decfsz    PDel0,  1 ; 1 + (1) is the time over? (C)
        goto      PLoop0    ; 2 no, loop
PDelL1  goto PDelL2         ; 2 cycles delay
PDelL2  clrwdt              ; 1 cycle delay
        return              ; 2+2 Done
;-----------------------------------------
;-------------------------------------------------------------
PDelayi 
	 movlw     .50    ; 1 set number of repetitions
        movwf     PDel0     ; 1 |
PLoopR  clrwdt              ; 1 clear watchdog
        decfsz    PDel0, 1  ; 1 + (1) is the time over?
        goto      PLoopR    ; 2 no, loop
PDelLA  goto PDelLB         ; 2 cycles delay
PDelLB  clrwdt              ; 1 cycle delay
        return              ; 2+2 Done	





INICIO
		BSF	STATUS,5
		BCF	STATUS,6
		MOVLW	B'10101001'
		MOVWF	PORTB
		CLRF	PORTC
		CLRF	PORTD
		MOVLW	B'00000001'
		MOVWF	PORTA
		BCF	STATUS,5
		CLRF	mA
		CLRF	R_4mA
		CLRF	MEMORIA
		CLRF	PORTB
		CLRF	PORTD
		BSF		LUC,0
;AT		BTFSC	PORTB,0
;		GOTO	AT
;		BSF		PORTB,1
;BA		BTFSC	PORTB,3
;		GOTO	BA
;		BSF		PORTB,4
;DS		BTFSC	PORTB,5
;		GOTO	DS
;		BSF		PORTB,6
;		BSF	STATUS,5
;		BCF	STATUS,6
		
		
COROSO	
		CALL	CONVERSIONANALOGODIGITAL
		BCF	STATUS,5
		CLRF	mA
		CLRF	R_4mA
		CALL	DECODIFICACION_BCD
		CALL	PROCESO
		GOTO	COROSO
CONVERSIONANALOGODIGITAL
		BSF	STATUS,5
		MOVLW	B'00001110'		;RA0 ANALOGO JUSTIFICACION LOS OCHO S BITS 
;DE ADRESH Y DOS BITS DE ADRESl
		MOVWF	ADCON1
		BCF	STATUS,5
EEE		MOVLW   B'10000001'
		MOVWF   ADCON0
		BCF	PIR1,ADIF
		BSF	ADCON0,GODONE
		CALL	PDelayi
EL		BTFSS	PIR1,ADIF
		GOTO	EL
		BSF	STATUS,5
		BCF	STATUS,6
		MOVF	ADRESL,0	;PARTE BAJA DE LA CONVERSION
		BCF	STATUS,5
		MOVWF RESULT_LOW
		MOVF	ADRESH,0	;PARTE ALTA DE LA CONVERSION
		MOVWF	RESULT_HIG		
		RETURN

DECODIFICACION_BCD

GE
	;	MOVLW	B'10101001'
	;	MOVWF	RESULT_LOW
	;	MOVLW	B'10001100'
	;	MOVWF	RESULT_HIG
	
		MOVLW	B'00001100'	;3272 PARTE ALTA
		MOVWF	R3272_HIG
		MOVLW	B'11001000'	
		MOVWF	R3272_LOW         ;3272 PARTE BAJA

INIC_F	MOVF	R3272_LOW,0    ;RESUT_LOW - R3272_LOW  
		SUBWF	RESULT_LOW,1
		BTFSS	STATUS,0
		GOTO	AAA
		MOVF	R3272_HIG,0 
		SUBWF	RESULT_HIG,1  ;RESUT_HIG - R3272_HIG
		BTFSS	STATUS,0
		GOTO	BBB

		INCF	mA,1
		GOTO	INIC_F

AAA		MOVLW	.1
		SUBWF	RESULT_HIG,1
		BTFSS	STATUS,0
		GOTO	BBB
		MOVF	R3272_HIG,0 
		SUBWF	RESULT_HIG,1
		BTFSS	STATUS,0
		GOTO	BBB

		INCF	mA,1
		GOTO	INIC_F


BBB		MOVF	mA,0
		MOVWF	mA_NEG
		RETURN

PROCESO		BTFSC	LUC ,0
		GOTO	SEGUIR
		MOVF	MEMORIA,0	
		SUBWF	mA,1
		BTFSS	STATUS,0
		GOTO	BEN
		BTFSC	STATUS,2
		RETURN
		GOTO	BEN_R

BEN		COMF	mA,1
		INCF	mA,1
		MOVF	mA_NEG,0
		MOVWF	MEMORIA
		BSF		PORTD,1
BEN_TR	CALL	RETARDO		 ;GAS IZQ	
		DECFSZ	mA,1
		GOTO	BEN_TR
		BCF		PORTD,0
		BCF		PORTD,1
		BCF		LUC,0
		RETURN

SEGUIR	MOVLW	.4
		SUBWF	mA,1
		BTFSC	STATUS,2
		GOTO	RET
BEN_R	BSF		PORTD,0  ;DER
BEMM	CALL	RETARDO
		DECFSZ  mA,1
		GOTO	BEMM
		MOVF	mA_NEG,0
		MOVWF	MEMORIA
		BCF		PORTD,0
		BCF		PORTD,1
		BCF		LUC,0
		RETURN

RET    	MOVF	mA_NEG,0
		MOVWF	MEMORIA
		BCF	LUC,0
		RETURN
		END
