
;**Programa para controlar la carga de baterías NI-MH
;**mediante una placa solar

;**Por: Pedro Díaz Sánchez
;**3 de Agosto de 2006
;**FREC OSC.--> 4 Mhz
;***************************************************************
	List 	P=16F876
;	RADIX	HEX
;***************************************************************
	Include "P16F876.inc"
	__Config _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC
;***************************************************************

Canal		equ 0x26
PDel0		equ 0x27
PDel1		equ 0x28
PDel2		equ 0x29
Voltaje   	equ 0x2A
Aux			equ 0x2B
Temp		equ 0x2D
Hexa		equ 0x2E

	




#define 	banco0 bcf STATUS, RP0	     ;selecciono el banco 0
#define 	banco1 bsf STATUS, RP0	     ;selecciono el banco 1

		Org	0x00
		goto 	Inicio
		Org	0x05
;/////////////////////////////////Interrupción//////////////////////////////////////////////
	


		
;////////////////////////////////////////////////////////////////////////////////////////
	

Inicio			;PROGRAMA PRINCIPAL
			banco1
			movlw	b'00011111'
			movwf	TRISA		;Puerta A entradas
;---------------------Configuracion CONVERTIDOR A/D-------------------------
			banco1
			bcf	STATUS,RP1
		
			movlw	b'00000001'	;seleccion tipo puertos A/D. Todo entradas analógicas con Vref+ en an3=3.00v
			movwf	ADCON1
			movlw	b'11111111'
			movwf	TRISA		;PORTA como entrada
		
			banco0
			movlw	b'01000001'	;seleccion Tad, canales para
			movwf	ADCON0		;conversion y A/D=ON
			bcf	PIR1,ADIF	;limpia flag de conversion completa
			
;	FIN de configuracion de registros
;---------------------------------------------------------------------------
			
			banco1
			clrf	TRISB		;Puerta B se configura como salida
			clrf 	TRISC		;Puerta C salidas
			banco0	


		
;--------------------------Comprovador de voltaje y temperatura----------------
ini			bsf PORTB,0			;Intensidad por las baterías 0mA
			call DELAY1S		;Espero 1s para estabilizar voltaje de las baterías
			call OBT_VOLT
			sublw .128			;128---->1.5v
			btfss STATUS,C		;Salto si el voltaje es <1.5v
			goto temp
			goto ini
temp		call OBT_TEMP
			sublw .39			;39---->0.46v---->46º
			btfsc STATUS,0	;Salto si la temperatura es >46º
			call CARGA
			goto ini
			
					
;---------------------------------------------------------------------------

RUN_ADC			bcf	STATUS,RP0	;Banco 0
				bcf	STATUS,RP1	;Banco 0
		
				movlw	b'01000001'	;repone a cero para que iorlw pueda
				movwf	ADCON0
		;bcf	ADCON0,ADON	;apaga el convertidor
				rlf	Canal,1		;posiciona los bits
				rlf	Canal,1
				rlf	Canal,0	
				iorwf	ADCON0,1
		;bsf	ADCON0,ADON	;enciende el convertidor
				rrf	Canal,1
				rrf	Canal,1		;retorna a la anterior
				bcf	PIR1,ADIF	;limpia flag de conversion completa
		
				nop
				nop
				nop
				nop
				nop
GO_ADC			bsf	ADCON0,GO_DONE	;Inicio conversion

wait_ADC		btfsc	ADCON0,GO_DONE	;espera a que finalice la conversion
				goto	wait_ADC	;bucle

READ_OK		;La conversion esta en ADRESH:ADRESL	
				return
			
;///////////////////////////////////////////////////////////////////////		
			
CARGA		nop	
inicarga	bcf PORTB,0
			call OBT_TEMP
			sublw .39		
			btfsc STATUS,0	;Salto si la temperatura es >45º
			goto inicarga
			return
				

;/////////////////////////////////////////////////////////////////////



;-----------Subrutina para obtener el Voltaje(valor de retorno en W)----------------------
OBT_VOLT	movlw .2		;Canal 0 para el voltaje
			movwf Canal
			call RUN_ADC
			movf ADRESH,0			
			RETURN
;-----------------------------------------------------------------------------------------
;-----------Subrutina para obtener la temperatura(valor de retorno en W)------------------
OBT_TEMP	movlw .1		;Canal 1 para la temperatura
			movwf Canal
			call RUN_ADC
			movf ADRESH,0			
			RETURN
;-----------------------------------------------------------------------------------------			
;-------------------------------------------------------------
; Generado con PDEL ver SP  r 1.0  el 04/09/2006 Hs 12:53:01
; Descripcion: Delay 1000000 ciclos
;-------------------------------------------------------------
DELAY1S  movlw     .14       ; 1 set numero de repeticion  (C)
    	movwf     PDel0     ; 1 |
PLoop0  movlw     .72       ; 1 set numero de repeticion  (B)
        movwf     PDel1     ; 1 |
PLoop1  movlw     .247      ; 1 set numero de repeticion  (A)
        movwf     PDel2     ; 1 |
PLoop2  clrwdt              ; 1 clear watchdog
        decfsz    PDel2, 1  ; 1 + (1) es el tiempo 0  ? (A)
        goto      PLoop2    ; 2 no, loop
        decfsz    PDel1,  1 ; 1 + (1) es el tiempo 0  ? (B)
        goto      PLoop1    ; 2 no, loop
        decfsz    PDel0,  1 ; 1 + (1) es el tiempo 0  ? (C)
        goto      PLoop0    ; 2 no, loop
PDelL1  goto PDelL2         ; 2 ciclos delay
PDelL2  clrwdt              ; 1 ciclo delay
        return              ; 2+2 Fin.
;-------------------------------------------------------------


		end


			
			
		

	

		
			
