;Esquema 2 ( display K comun )
   ;10/09/2003		CAPACIMETRO 5n a 9999 microF
   ; COLOCAR RESISTOR DE 1K2 NO PINO 3 ( RA4 ) PARA +5V
   ; Ligar resistor do ponto decimal ao +5 

				;Segmentos no portb
	errorlevel	-302
	list p=16F84A
	radix	dec
	include	
	__config	_XT_OSC & _CP_OFF & _WDT_OFF & _PWRTE_ON


W2	equ	12
ST2	equ	13
milhar	equ	14
centena	equ	15
dezena	equ	16
unidade	equ	17
qual	equ	18
tempo	equ	19
dt1	equ	20
espera	equ	21	

	org	0

	goto	inicio


	org	4

SalvaWeST:
	movwf	W2
	movf	STATUS,W
	movwf	ST2

TestaFlags:
	btfsc	INTCON,T0IF
	goto	INT_TIMER
	btfsc	INTCON,RBIF
	goto	INT_EXTERNA

Fim_Int:
	movf	ST2,W
	movwf	STATUS
	movf	W2,W
	retfie


INT_TIMER:
	bcf 	STATUS,RP0
	incf	qual,f
	movf	qual,w
indexa:
	addwf	PCL,f
	nop
	goto	dispM
	goto	dispC
	goto	dispD
	goto	dispU
	movlw	1
	movwf	qual

dispM:
	bsf	PORTA,1
	bsf	PORTA,2
	bsf	PORTA,3
	bsf	PORTA,4
	movf 	milhar,w
	call	segmento
	movwf	PORTB
	bcf	PORTA,2
	bcf	INTCON,T0IF
	goto	Fim_Int

dispC:

	bsf	PORTA,1
	bsf	PORTA,2
	bsf	PORTA,3
	bsf	PORTA,4
	movf 	centena,w
	call	segmento
	movwf	PORTB
	bcf	PORTA,1
	bcf	INTCON,T0IF
	goto	Fim_Int

dispD:
	bsf	PORTA,1
	bsf	PORTA,2
	bsf	PORTA,3
	bsf	PORTA,4
	movf 	dezena,w
	call	segmento
	movwf	PORTB
	bcf	PORTA,4
	bcf	INTCON,T0IF
	goto	Fim_Int

dispU:
	bsf	PORTA,1
	bsf	PORTA,2
	bsf	PORTA,3
	bsf	PORTA,4
	movf 	unidade,w
	call	segmento
	movwf	PORTB
	bcf	PORTA,3
	bcf	INTCON,T0IF
	goto	Fim_Int

segmento:

	addwf	PCL,f
	retlw	B'01111111'	;0
	retlw	B'00001101'	;1
	retlw	B'10110111'	;2
	retlw	B'10011111'	;3
	retlw	B'11001101'	;4
	retlw	B'11011011'	;5
	retlw	B'11111011'	;6
	retlw	B'00001111'	;7
	retlw	B'11111111'	;8
	retlw	B'11011111'	;9
	retlw	B'11110010'	;
	retlw	B'10100000'	;
	retlw	B'10111000'	;
 	retlw	B'10101000'	;
	retlw	B'00000000'	; apaga segmentos
	call	erro

INT_EXTERNA:
	incf	unidade,f
	movlw	10
	subwf	unidade,w
	btfss	STATUS,Z
	goto	fim_ext

	clrf	unidade
	incf	dezena,f
	movlw	10
	subwf	dezena,w
	btfss	STATUS,Z
	goto	fim_ext

	clrf	dezena
	incf	centena,f
	movlw	10
	subwf	centena,w
	btfss	STATUS,Z
	goto	fim_ext

	clrf	centena
	incf	milhar,f
	movlw	10
	subwf	milhar,w
	btfss	STATUS,Z
	goto	fim_ext
	clrf	milhar

fim_ext:
	bcf	INTCON,RBIF	
	goto	Fim_Int

inicio:
	clrf	PORTA
	clrf	PORTB
	bcf	PORTA,1
	bcf	PORTA,2
	bcf	PORTA,3
	bcf	PORTA,4

	clrf	INTCON
	clrf	TMR0
	bsf	STATUS,RP0
	movlw	B'00000001'	
	movwf	TRISB
	movlw	B'11100000'
	movwf	TRISA
	movlw	B'10010011'
	movwf	OPTION_REG
	bcf	STATUS,RP0
	
Reset:

	clrf	milhar
	clrf	centena
	clrf	dezena
	clrf	unidade
	clrf	qual


	bcf	INTCON,T0IF
	bcf	INTCON,RBIF
	bsf	INTCON,T0IE
	bsf	INTCON,RBIE
	bsf	INTCON,GIE
;---------	
pulso
	bsf	PORTA,0		; descarga do capacitor
	call	ms250		; espera
	call	ms250
	bcf	PORTA,0		; inicia carga do capacitor

loop:	btfss	PORTB,0		; 
	bsf	INTCON,RBIF	; conta durante tempo de carga do capacitor

;------------------OVERFLOW  (9999)--------------------------
	btfsc	PORTB,0		;*** testa fim da medição do capacitor
	goto	off		;***
	movf	milhar,w
	addwf	centena,w	; SOMA
	addwf	dezena,w	;  "
	addwf	unidade,w	;  "
	xorlw	36		; 9+9+9+9
	btfss	STATUS,2
	goto	loop

erro	movlw	10
	movwf	milhar
	movlw	11
	movwf	centena
	movlw	11
	movwf	dezena
	movlw	12
	movwf	unidade	

off	movlw	1	;(+- 1 min.) 
	movwf	espera

loop2	call	ms250
	incfsz	espera,1
	goto	loop2

	movlw	10
	movwf	milhar
	movlw	11
	movwf	centena
	movlw	12
	movwf	dezena
	movlw	13
	movwf	unidade

	movlw	240	;   (+- 4 seg.)
	movwf	espera
loop3	call	ms250
	incfsz	espera,1	
	goto	loop3
;------------------------------------------------	
	;Preparando-se para dormir


	bsf	STATUS,RP0
	movlw	B'11111111'
	movwf	TRISA
	movwf	TRISB
	bcf	STATUS,RP0
	bcf	INTCON,GIE	
	sleep


;--------------------------
ms250:
	movlw	250
	movwf	tempo

ms250a:
	movlw 	248
	movwf	dt1

ms250b:
	nop               	
	decfsz 	dt1,1
	goto  	ms250b

	decfsz	tempo,1
	goto 	ms250a

	return

	END

CAPACIMETRO