; ---------------------------------------------------------------------------
; - En este area se agregan todas las definiciones de etiquetas y registros
; ---------------------------------------------------------------------------
	;list      p=16F877	      ; list directive to define processor
	list	  p=16F877a	      ;   or this if using the 16F877a

        ;#include <p16F877.inc>        ; processor specific variable definitions
	#include <p16F877a.inc>       ;   or this if using the 16F877A

	__CONFIG _BODEN_OFF & _WRT_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC & _LVP_OFF & _CP_OFF & _CPD_OFF & _DEBUG_OFF
        errorlevel -302               ; suppress "register not in bank0, check page bits" message.
;**********************************************************************
;   Define external functions                                         *
;**********************************************************************
	extern	LCDINIT
	extern	LCDCLEAR
	extern	LCDPUTCHAR
	extern	LCDPUTCMD
	extern  L1homeLCD
	extern	L2homeLCD	
;**********************************************************************
;   definicin de pines de I/O                                	      *
;**********************************************************************
				;definicion de pines PORT A 
#define   AN0    PORTA, 0	;light sensor analog input
#define   AN1    PORTA, 1	;no utilizada
#define   VREF1  PORTA, 2	;vref negativa
#define   VREF2  PORTA, 3	;vref positiva
;#define   SCAPE  PORTA, 4	;S1
;#define   ENTER  PORTA, 5	;S2

        			;definicion de pines PORT B 
#define   DB0    PORTB, 0	;data lines for LCD
#define   DB1    PORTB, 1
#define   DB2    PORTB, 2
#define   DB3    PORTB, 3
#define   DB4    PORTB, 4
#define   DB5    PORTB, 5
#define   DB6    PORTB, 6
#define   DB7    PORTB, 7 

#define	  K1	 PORTC, 0	;definicion de pines PORT C 
#define	  K2	 PORTC, 1
#define	  K3	 PORTC, 2
#define	  K4	 PORTC, 3
#define   BUZZER PORTC, 4	
#define   K5     PORTC, 5
#define   TXD    PORTC, 6	;serial interface (this is provision only)
#define   RXD    PORTC, 7

				;definicion de pines PORT D 
#define   RS     PORTD, 7	;control lines for LCD  
#define   E      PORTD, 6

				 
#define	  C1	 PORTD, 0	;IN1
#define	  FL	 PORTD, 1	;IN2
#define	  P2	 PORTD, 2	;IN3
#define	  P1	 PORTD, 3	;IN4
;#define   UP	 PORTD, 4	;S3
;#define   DOWN   PORTD, 5	;S4

				;definicion de pines PORT E 
#define	  P1_AL	 PORTE, 0	;alarma de presin 1 
#define   P2_AL	 PORTE, 1	;alarma de presin 2
#define   C1_AL  PORTE, 2	;alarma de conductimetro 


;----------------------------------------------------------------------
;Data EEPROM Storage Locations
;----------------------------------------------------------------------
EELavadoPozo	EQU	0x01
EELmInicial		EQU	0x02
EELmHoras		EQU	0x03
EELmMinutos		EQU	0x04
EEAlarmas		EQU	0x05


_EEPROM	code	0x2100		;EEPROM data start location
	fill 0x00, 0x100 	;initialize EEPROM locations to 0


;----------------------------------------------------------------------
;Display Messages are assigned indexes that are used in DisplayTable
;----------------------------------------------------------------------
M_OSMO_01			EQU	.0
M_SELEC_FUNCION		EQU	.1
M_LavadoPozo		EQU	.2
M_LmInicial			EQU	.3
M_LmHoras			EQU	.4
M_LmMinutos			EQU	.5
M_Alarma		EQU	.6
M_EnterCode		EQU	.7
M_P1Elevada		EQU	.8
M_P2Elevada		EQU	.9
M_C1Elevada		EQU	.10
M_Minutos		EQU	.11
M_Okay			EQU	.12
M_Horas			EQU	.13
M_Menu_ON		EQU	.14
M__MenuON		EQU	.15
M_Menu_OFF		EQU	.16
M__MenuOFF		EQU	.17

;----------------------------------------------------------------------
;Definitions for L1Update, L2Update, and PromptUpdate routines
;----------------------------------------------------------------------
L1EnterCode		EQU	.0
L1Welcome		EQU	.1
L1SelectFunction	EQU	.2
L1LavadoPozo		EQU	.3
L1LmInicial		EQU	.4
L1LmHoras		EQU	.5
L1LmMinutos		EQU	.6
L1Alarma		EQU	.7


L2Menu_ON			EQU	.0
L2Menu_OFF			EQU	.1
L2_MenuON			EQU	.2
L2EnterCode			EQU	.3
L2_MenuOFF			EQU	.4
L2LavadoPozo		EQU	.5
L2LmInicial			EQU	.6
L2LmMinutos			EQU	.7
L2LmHoras			EQU	.8
L2DisplaySetMins	EQU	.9
L2DisplaySetHrs		EQU	.10
L2P1Elevada			EQU	.11
L2P2Elevada			EQU	.12
L2C1Elevada			EQU	.13

P_SetHrs		EQU	.0
P_SetMin		EQU	.1
P_Okay			EQU	.2
P_DisplayYN		EQU	.3	

							;Button assignments on PORTD y A
UP				EQU	.2   
DOWN			EQU	.3
EXIT			EQU	.4
ENTER			EQU	.5
;**********************************************************************
;   Variables                                                         *
;**********************************************************************
gprnobnk	udata_shr

_WREG		res 1	; used to save WREG during interrupt
_STATUS     res 1	; used to save STATUS during interrupt
_PCLATH		res 1	; used to save PCLATH during interrupt
_FSR		res 1	; used to save FSR during interrupt

Hours		res 1
Mins		res 1
Secs		res 1
Ticks		res 1	;Tick is 25ms





ControlFlags	res 1
#define		ButtonPress		ControlFlags, 0	
#define		MinElapsedFlag		ControlFlags, 1	
#define		ArrancaProcesoFlag	ControlFlags, 2	
#define		LavadoPozoFlag		ControlFlags, 3	
#define		LmInicialFlag		ControlFlags, 4	
#define		PermeadoFlag		ControlFlags, 5	
#define		LmCiclicoFlag		ControlFlags, 6	
#define		ParaProcesoFlag		ControlFlags, 7		

DisplayFlags	res 1
#define		UpdateDisplayFlag	DisplayFlags, 0
#define		L1UpdateFlag		DisplayFlags, 1
#define		PromptUpdateFlag	DisplayFlags, 3
#define		Okay			DisplayFlags, 4
#define		BUZZ			DisplayFlags, 5
#define		TempPM			DisplayFlags, 6
#define		borraTiempoFlag			DisplayFlags, 7

MenuState		res 1
MenuSubState	res 1

L1				res 1
L2				res 1
Prompt			res 1

grp0	udata

bres_hi			res	1	; hi byte of our 24bit variable
bres_mid		res	1	; mid byte
bres_lo			res	1	; lo byte
Horas			res 1
mins			res 1
Segs			res 1
Last_PORTD		res	1
Last_PORTA		res	1
Debounced1		res	1
Debounced2		res	1
Toggled			res	1
LastDebounced1	res	1
LastDebounced2	res	1
Held		res	1

TimeLimit	res	1

TempA		res	1	;ISR temporary variable
TempB		res	1	;ISR temporary variable

Index1		res	1	;general purpose Index variable
Index2		res	1	;general purpose Index variable

TempHours	res	1
TempMins	res	1

BCDH		res	1
BCDL		res	1

LavadoPozo	res	1
LmInicial	res	1
LmHoras		res	1
LmMinutos	res	1
Alarmas		res	1	
TempLavadoPozo	res	1
TempLmInicial	res	1
TempLmHoras	res	1
TempLmMinutos	res	1
TempAlarmas	res	1


UpCount		res	1
DownCount	res	1
Codigo		res	1
Vueltas		res	1
Button		res	1
VALUE		res	1
ADDR		res	1






; ---------------------------------------------------------------------------
; ** Programa del sistema **
; ---------------------------------------------------------------------------
STARTUP	code 	

 ;	nop		        ; 0x0000  (This nop necessary for ICD operation)
  	goto 	Start		; 0x0001
  	nop			; 0x0002
  	nop			; 0x0003
;****************************************************************************************************
ISRcode	code	0x0005
;****************************************************************************************************
;----------------------------------------------------------------------
; ISR- 
;	Interrupt Service Routine.  Determine cause of interrupt and perform appropriate action.
;----------------------------------------------------------------------
;	Interrupt Sources:
;	TMR0:		Interrupcin  para contar tiempo
;	TMR1: 		Genera interrupcin cada 25 ms para el keyscan	
;		
;
;----------------------------------------------------------------------
ISR
	;PUSH

	movwf	_WREG		; Save WREG
	movf	STATUS,W	; Save STATUS
	clrf	STATUS
	movwf	_STATUS
	movf	PCLATH, W
	movwf	_PCLATH
	movf	FSR, W
	movwf	_FSR


	pagesel	ScanKeys
	banksel	PIR1
	btfsc	PIR1, TMR1IF	;Timer 1 interrupt?
	call	ScanKeys	;Yes, then call ScanKeys 

	pagesel	UpdateDisplay		
	btfsc	UpdateDisplayFlag		;If display update flag is set,
	call	UpdateDisplay			;	update display.

	


	pagesel	Rutinahms
	btfsc	INTCON, T0IF	;Timer 0 interrupt?
	call	Rutinahms	;Yes, then Rutinahms



	;-------------------------------------------------


	;POP
	clrf    STATUS          ; select bank 0
	movf    _FSR, W		; restore the FSR
	movwf   FSR
	movf	_PCLATH, W
	movwf	PCLATH
	movf   _STATUS, W	;Swap original status register value into W
	movwf   STATUS		;Restore status register from W register
	swapf   _WREG, F	;Swap _WREG nibbles and return to _WREG
	swapf   _WREG, W	;Swap _WREG to W to restore original W value
				; without affecting STATUS.

	retfie		

;****************************************************************************************************
PROG1	code
;****************************************************************************************************
	;goto	$		;This is a trap to prevent runaway code execution
Start
	
	
	
	pagesel	Initports
	call	Initports
	pagesel	InitVariables
	call	InitVariables		;initialize variables

	pagesel	InitAdc
	call	InitAdc

	pagesel	InitTimer0
	call	InitTimer0		;set up Timer0
	pagesel	InitTimer1		;selected when using external 32kHz osc as TMR1 clock source
	call	InitTimer1
		
	
	
	
	pagesel	LCDINIT
	call	LCDINIT			;initialize the LCD

	
	banksel	TempAlarmas
	movf	TempAlarmas,w		;Initialize alarmas to value stored in EEPROM
	xorlw	0x01
	btfsc	STATUS,Z
	goto	P1elevada

								;determinar que alarma fue!!!!!!
	banksel	TempAlarmas
	movf	TempAlarmas,w		;Initialize alarmas to value stored in EEPROM
	xorlw	0x02
	btfsc	STATUS,Z
	goto	P2elevada

	banksel	TempAlarmas
	movf	TempAlarmas,w		;Initialize alarmas to value stored in EEPROM
	xorlw	0x03
	btfsc	STATUS,Z
	goto	C1elevada
	goto	ice


P1elevada           
	
	pagesel	InitP1ElevadaState
	call	InitP1ElevadaState			
	goto	UD
P2elevada
	pagesel	InitP2ElevadaState
	call	InitP2ElevadaState
	goto	UD

C1elevada
	pagesel	InitC1ElevadaState
	call	InitC1ElevadaState
	goto	UD

ice	
	pagesel	InitEnterCodeState
	call	InitEnterCodeState	;initialize the state machine to Ingrese codigo state
	bcf	MinElapsedFlag	
UD	
	pagesel	UpdateDisplay		
	call	UpdateDisplay		;update display





; Note! Now the hardware is set up we need to load the
; first count for one second into our 24bit bres variable.
;------------------------------------------------------------------------
						; Note! This example uses 4 MHz clock, which is
						; 1,000,000 counts per second.
						;
						; We require a 1 second period, so we must load
						; 1,000,000 counts each time.
						; 1,000,000 = 0F 42 40 (in hex)
						;
						; We also need to add 256 counts for the first time,
						; so we just add 1 to the mid byte.
						; Check mid overflow if needed.
						; here we load the 24bit variable.
		movlw 0x0F				; get msb value 
		movwf bres_hi			; put in hi
		movlw 0x42 +1			; get mid value (note we added 1 to it)
		movwf bres_mid			; put in mid

		movlw 0x40			; get lsb value
		movwf bres_lo			; put in mid
		pagesel Main
		goto	Main
;****************************************************************************************************
;****************************************************************************************************
; Main Loop Code
;	This is the main loop which continually performs the following tasks:
;	-if a key press has occured, update the state
;	-if no key press has occured for over a minute, return to main menu
;	-if the a clock minute has elapsed, check the unit on and off times
;	-if an X10 message has been received, control the TRIAC
;	*****-if display update flag has been set, update display
;	*****-send X-10 control messages as necessary
;
;	The various control flags are set from within the Interrupt Service Routine
;****************************************************************************************************
;****************************************************************************************************
Main

	pagesel UpdateState	
	btfsc	ButtonPress			;If any key press has occurred, 
	call	UpdateState			;	update the state.

	pagesel	InitMainMenuState
	btfsc	MinElapsedFlag			;If a minute has elapsed without a key press,
	call	InitMainMenuState	 	;	update the state.

	

	
	pagesel ControlFlags	
	btfsc	ArrancaProcesoFlag		;Si arrancaprocesoflag se seteo se  va al arranque 
	call	ArrancaProceso

	pagesel ControlFlags
	btfsc	ParaProcesoFlag
	call	ParaProceso
	

	banksel PORTE				;verifica entradas de alarma
	btfss	P1				;salta a los init de alarmas
	call	InitP1ElevadaState


	banksel PORTE
	btfss	P2
	call	InitP2ElevadaState


	banksel PORTE
	btfss	C1
	call	InitC1ElevadaState	

 	pagesel Main
	goto	Main
			
					

;****************************************************************************************************
;ISRRoutines
;****************************************************************************************************
;----------------------------------------------------------------------
;Rutinahms
;----------------------------------------------------------------------
Rutinahms						;-------------------------------------------------
						; Note! we get here every 256 instructions, we
						; can now do our special one second timing system.				

						; This consists of three main steps;
						; * subtract 256 counts from our 24bit variable
						; * test if we reached the setpoint
						; * if so, add 1,000,000 counts to 24bit variable and generate event.
						;-------------------------------------------------
						; * optimised 24 bit subtract here 
						; This is done with the minimum instructions.
						; We subtract 256 from the 24bit variable
						; by just decrementing the mid byte.
		movf	bres_mid,w		
		xorlw	0x00			; first test for mid==0
		btfsc	STATUS,Z		; nz = no underflow needed
		decf bres_hi,f			; z, so is underflow, so dec the msb

		decfsz bres_mid,f		; dec the mid byte (subtract 256)

						; now the full 24bit optimised subtract is done!
						; this is about 4 times faster than a "proper"
						; 24bit subtract.

		goto int_exit			; nz, so definitely not one second yet.
						; in most cases the entire int takes
						; only 16 instructions.
	;------------------------
						; * test if we have reached one second.
						; only gets here when mid==0, it MAY be one second.
						; only gets to here 1 in every 256 times.
						; (this is our best optimised test)
						; it gets here when bres_mid ==0.
		movf	bres_hi,w		
		xorlw	0x00		
		btfss	STATUS,Z		; test hi for zero too
						; z = both hi and mid are zero, is one second!
		goto 	int_exit		; nz, so not one second yet.

	;-------------------------------------------------
	; Only gets to here if we have reached one second.

	; now we can generate our one second event, like add
	; one second to our clock or whatever.
	; (in this example we toggle a led)

	; The other thing we need to do is add 1,000,000 counts
	; to our 24bit variable and start all over again.
	;-------------------------------------------------
						; Add the 1,000,000 counts first.
						; One second = 1,000,000 = 0F 42 40 (in hex)
						; As we know hi==0 and mid==0 this makes it very fast.
						; This is an optimised 24bit add, because we can
						; just load the top two bytes and only need to do
						; a real add on the bottom byte. This is much quicker
						; than a "proper" 24bit add.
		movlw 0x0F			; get msb value 
		movwf bres_hi			; load in msb
		movlw 0x42			; get mid value
		movwf bres_mid			; load in mid
		movlw 0x40			; lsb value to add
		addwf bres_lo,f			; add it to the remainder already in lsb
		btfsc STATUS,C			; nc = no overflow, so mid is still ok

		incf bres_mid,f			; c, so lsb overflowed, so inc mid
						; this is optimised and relies on mid being known
						; and that mid won't overflow from one inc.
						; that's it! Our optimised 24bit add is done,
						; this is roughly twice as quick as a "proper"
						; 24bit add.
	;-------------------------
	;!!!!rutina h m s!!!!			; now we do the "event" that we do every one second.
	;--------------------------
						; Add your own code here for your one second event.
		incf 	Segs,f			; incrementa la variable seg
		movf	Segs,w			;
		xorlw	0x3C			;se fija si llego a 60 seg
		btfss	STATUS,Z		;si llego salta a incremento de minutos
		goto 	int_exit		; si no; termina el servicio
		clrf	Segs
		incf 	mins,f			; incrementa la variable min
		movf	mins,w			;
		xorlw	0x3C			;se fija si llego a 60 min
		btfss	STATUS,Z		;si llego salta a incremento de horas
		goto 	int_exit		; si no; termina el servicio				
							
		clrf	mins
		incf 	Horas,f			; incrementa la variable horas
		
				
						
	;-------------------------------------------------
	; now our one second event is all done, we can exit the
	; interrupt handler.
	;-------------------------------------------------
int_exit
		BCF 	INTCON,T0IF		; reset the tmr0 interrupt flag
		return


;----------------------------------------------------------------------
;UpdateClock - 
;	Macro executed within ScanKeys routine.
;	update TMR1H, Hours, Secs, Mins, and set the UpdateDisplayFlag.
;----------------------------------------------------------------------
UpdateClock	macro		;Implemented as macro to save space on stack
	movlw	0xFC		;preload Timer 1 for 25ms interrupt (see InitTimer1 for calculation)
	movwf	TMR1H

	banksel	Ticks
	incf	Ticks, F	;Each tick = 25ms.  40 ticks = 1 sec.
	movlw	.40
;	movlw	.15		;to make clock run faster for testing purposes
	subwf	Ticks,W
	btfss	STATUS,Z
	goto	EndUpdateClock
	clrf	Ticks	
	incf	Secs,F

	movlw	.60
	subwf	Secs,W
	btfss	STATUS,Z
	goto	EndUpdateClock
	clrf	Secs
	incf	Mins,F

	movlw	.60
	subwf	Mins,W
	btfss	STATUS,Z
	goto	EndUpdateClock
	clrf	Mins
	incf	Hours,F


EndUpdateClock

	endm


;-------------------------------------------------
;MinuteElapsed
;	Macro called from ScanKeys routine.
;	
;	If 1 minute elapses without a button press, set Minute Elapsed Flag (MinElapsedFlag)
;----------------------------------------------------------------------
MinuteElapsed	macro		;Implemented as macro to save space on stack
	btfss	ButtonPress	;check to see if button has been pressed
	goto	CheckTimeLimit	;if not, check time limit
	movf	Secs, W		;if so, set new TimeLimit:
	btfsc	STATUS, Z	;	if Seconds is 0, set new TimeLimit to 59
	movlw	.60
	movwf	TimeLimit	
	decf	TimeLimit, F	;	otherwise, set new TimeLimit to Seconds - 1
	goto	EndMinuteElapsed
			
CheckTimeLimit
	movf	Secs, W		;
	subwf	TimeLimit, W	;
	btfsc	STATUS,Z	;if Seconds = TimeLimit
	bsf	MinElapsedFlag	;set the minute elapsed flag
	btfss	STATUS,Z	;otherwise,
EndMinuteElapsed
	bcf	MinElapsedFlag	;clear the minute elapsed flag 
		
	endm
;----------------------------------------------------------------------
;ButtonToggle
;	Macro called from ScanKeys routine.
;	Checks to see if each button has been pressed and then released, if so
;	then corresponding bit of Toggled is clear, otherwise it's set.
;
;
;----------------------------------------------------------------------
ButtonToggle	macro		;Implemented as macro to save space on stack
	

	btfsc	LastDebounced2,UP		;If button was pressed
	goto	$+5
	btfss	Debounced2,UP			;and is now not pressed
	goto	$+3
	bcf		Toggled,UP			;clear the button's Toggled bit.
	goto	$+2				
	bsf		Toggled,UP			;Otherwise, set the bit.

	btfsc	LastDebounced2, DOWN		;If button was pressed
	goto	$+5
	btfss	Debounced2, DOWN			;and is now not pressed
	goto	$+3
	bcf		Toggled, DOWN			;clear the button's Toggled bit.
	goto	$+2				
	bsf		Toggled, DOWN			;Otherwise, set the bit.

	btfsc	LastDebounced1, ENTER		;If button was pressed
	goto	$+5
	btfss	Debounced1, ENTER		;and is now not pressed
	goto	$+3
	bcf	Toggled, ENTER			;clear the button's Toggled bit.
	goto	$+2				
	bsf	Toggled, ENTER			;Otherwise, set the bit.

	btfsc	LastDebounced1, EXIT		;If button was pressed
	goto	$+5
	btfss	Debounced1, EXIT			;and is now not pressed
	goto	$+3
	bcf	Toggled, EXIT			;clear the button's Toggled bit.
	goto	$+2				
	bsf	Toggled, EXIT			;Otherwise, set the bit.

	movf	Toggled, W
	sublw	0xFE		;If any button has toggled, subtracting FE results in Carry
	btfsc	STATUS, C
	bsf	ButtonPress	;if any button toggled, set flag

	endm
;----------------------------------------------------------------------
;ButtonHeld
;	Macro called from ScanKeys routine.
;	Checks to see if a button has been held for N consecutive KeyScan calls.
;	If it has, then the ButtonHeld flag for that button is cleared.  The flag
;	is set on the subsequent call of ButtonHeld.
;
;	This routine only checks Up and Down buttons.
;	
;----------------------------------------------------------------------
ButtonHeld	macro		;Implemented as macro to save space on stack
	banksel	UpCount
	bsf		Held, UP
	bsf		Held, DOWN

	btfsc	Debounced2, UP		;Is Up button pressed?
	goto 	ClearUpCount	 	;if not, clear the held counter for the Up button
	incf	UpCount, F		;if so, increment held counter for Up button
	movlw	.10			;and check to see if it equals 10
	subwf	UpCount, W			
	btfss	STATUS, Z		;if UpCount=5,
	goto	DownButtonHeld
	bcf		Toggled, UP
	bcf		Held, UP		;	clear the Held bit (active low)
;	bsf	Debounced, UP		;	and set the Debounced bit (so Toggle is not detected)
;	bsf	Last_PORTB, UP
	goto	DownButtonHeld
ClearUpCount
	clrf	UpCount			;

DownButtonHeld
	btfsc	Debounced2, DOWN		;Is Down button pressed?
	goto 	ClearDownCount	 	;if not, clear the held counter for the Down button
	incf	DownCount, F		;if so, increment held counter for Down button
	movlw	.10			;and check to see if it equals 10
	subwf	DownCount, W			
	btfss	STATUS, Z		;if DownCount=5,
	goto	EndButtonHeld
	bcf		Toggled, DOWN
	bcf		Held, DOWN		;	clear the Held bit (active low)
;	bsf	Debounced, DOWN		;	and set the Debounced bit (so Toggle is not detected)
;	bsf	Last_PORTB, DOWN
	goto	EndButtonHeld
ClearDownCount
	clrf	DownCount

EndButtonHeld
	btfss	Held, UP		;if the Up flag has been clear, clear the UpCount
	clrf	UpCount
	btfss	Held, DOWN		;if the Down flag has been clear, clear the DownCount
	clrf	DownCount

	movf	Held, W
	sublw	0xFE			;If any button held, subtracting FE results in Carry
	btfsc	STATUS, C
	bsf	ButtonPress		;if any button held, set flag

	endm

;----------------------------------------------------------------------
;ButtonPip
;	Macro called from ScanKeys routine.
;	Si se presiona un boton emite un pip por 1 seg
;	.
;	
;----------------------------------------------------------------------
ButtonPip	macro

	btfss	ButtonPress	;check to see if button has been pressed
	goto	EndButtonPip
	
	movf	Ticks,w		;
	sublw	.39		;
	btfsc	STATUS,Z	;if Ticks = 39
	goto	apagabuzzer
	bsf		BUZZER
	goto	EndButtonPip


apagabuzzer
	bcf	BUZZER
	
EndButtonPip

	endm

;------------------------------------
;----------------------------------------------------------------------
;ScanKeys
;	UP     = Debounced_PORTA, 1
;	DOWN   = Debounced_PORTA, 2
;   
;	ENTER  = Debounced_PORTB, 4
;	EXIT   = Debounced_PORTB, 5
;	ON_OFF = Debounced_PORTB, 5
;	
;	Buttons are active low.  If a button is low for two scans in a row,
;	it is considered to be debounced.
;
;	When a button that was pressed is released, it is considered to be Toggled.
;	When a button is held for N iterations of ScanKeys, it is considered to be Held.
;
;----------------------------------------------------------------------
ScanKeys
	banksel	PIR1
	bcf	PIR1, TMR1IF

	UpdateClock



	banksel	PORTA
	movf	PORTA, W	;read PORTA
	iorwf	Last_PORTA, W	;Compare with PORTA from previous key scan
				;If any bit is pulled low for 2 scans in a row, 
				;the button associated with that bit is considered
				;to be pressed.
	iorlw	b'11001111'	;Mask the bits that aren't buttons (make them 1's)	
	movwf	Debounced1	;Save the debounced values.

	movf	PORTA, W	;Save PORTB to compare in next scan.
	movwf	Last_PORTA


	banksel	PORTD
	movf	PORTD, W	;read PORTD
	iorwf	Last_PORTD, W	;Compare with PORTD from previous key scan
				;If any bit is pulled low for 2 scans in a row, 
				;the button associated with that bit is considered
				;to be pressed.
	iorlw	b'11001111'	;Mask the bits that aren't buttons (make them 1's)	
	movwf	Debounced2	;Save the debounced values.
	rrf		Debounced2, F
	rrf		Debounced2, F

	movf	PORTD, W	;Save PORTB to compare in next scan.
	movwf	Last_PORTD

	
	ButtonToggle		;Check to see which pressed buttons have been released
	ButtonHeld		;Check to see which buttons have been held down
	ButtonPip		;Si se presiono un boton,emite el beep
	MinuteElapsed		;Check to see if a minute has elapsed since last button pressed

	movf	Debounced1, W	;save debounced to last debounced for next time
	movwf	LastDebounced1
	movf	Debounced2, W	;save debounced to last debounced for next time
	movwf	LastDebounced2

	return
;****************************************************************************************************
;INITIALIZATION ROUTINES
;****************************************************************************************************	
;----------------------------------------------------------------------
; InitTimer1-                                   
;----------------------------------------------------------------------
;Timer1 is used with an external 32kHz crystal oscillator and configured
;to generate an interrupt 1/25ms.
;	TMR1 interrupt= 1/(FOSC/4) * prescaler * (FFFFh+1-preload)
;			1/1MHz * 8         * (FFFFh+1-F3CBh) = 25msec
;	- Initialize Timer1 with F3CBh             
;		- Configure Timer1                         
;		- Prescale 1:8                         
;		        
;		- DO NOT SYNCHRONIZE                   
;		- INTERNAL Clock Source                
;		- Enable Timer1                        
;   - Clear Timer1 Overflow Interrupt Flag     
;   - Enable Timer1 Overflow Interrupt                       
;----------------------------------------------------------------------
InitTimer1
	BANKSEL TMR1H		;Initialize Timer1 with FCE0h
	movlw	0xF3		
	movwf	TMR1H
	movlw	0xCB			
	movwf	TMR1L		
	movlw	B'00110001'	;configure Timer 1  
	movwf	T1CON		
	bcf	PIR1,TMR1IF	;Clear Timer1 Overflow Interrupt Flag
	BANKSEL PIE1
	bsf	PIE1,TMR1IE	;Enable Timer1 Overflow Interrupt
	return
;----------------------------------------------------------------------
; InitAdc
;----------------------------------------------------------------------	
InitAdc

	banksel	CMCON
	movlw	0x07
	movwf	CMCON

	banksel	ADCON0		;ADC conversion clock is Fosc/32
	movlw	b'00000000'	;Analog channel is RA0
	movwf	ADCON0		;ADC is on
				
		
	banksel	ADCON1		;ADC result is left justified
	movlw	b'00000110'	;RA0 is analog input, all other channels are digital
	movwf	ADCON1		;

	return


;----------------------------------------------------------------------
; InitTimer0
;----------------------------------------------------------------------	
; Timer0 se usa para la contabilizacion de tiempos de proceso
;	                                 
;----------------------------------------------------------------------
InitTimer0
	banksel	OPTION_REG
	movlw	b'10001000'	;; Note! We set the prescaler to the wdt, so timer0
				; has NO prescaler and will overflow every 256 
				; instructions and make an interrupt.
	movwf	OPTION_REG	
	bcf	INTCON, T0IF	;Clear Timer1 Overflow Interrupt Flag
;	bsf	INTCON, T0IE	;Enable Timer0 Overflow Interrupt
	return
;----------------------------------------------------------------------
; Initports                             
;----------------------------------------------------------------------
Initports
				; PORTA pins direction setup
	banksel	TRISA		; 1=input, 0=output
	movlw b'11111111'	; all 6 porta are inputs
	movwf TRISA		; send mask to porta	

				; PORTB pins direction setup
	banksel	TRISB		; 1=input, 0=output
	movlw b'00000000'	
	movwf TRISB		; send mask to portb
	
				; PORTC pins direction setup
	banksel	TRISC		; 1=input, 0=output
	movlw b'00000000'	
	movwf TRISC		; send mask to portc
	
				; PORTD pins direction setup
	banksel	TRISD		; 1=input, 0=output
	movlw b'00111111'	;
	movwf TRISD		; send mask to portd
	
				; PORTE pins direction setup
	banksel	TRISE		; 1=input, 0=output
	movlw b'00000000'	; 
	movwf TRISE		; send mask to porte	

	return
;----------------------------------------------------------------------
; InitVariables                              
;----------------------------------------------------------------------
InitVariables 

	banksel	Secs
	clrf	Secs		;initialize clock variables
	clrf	Mins
	clrf	Hours
	clrf	Codigo
	clrf	mins
	clrf	Segs
	clrf	Horas
	clrf	MenuSubState	;initialize state variables
	clrf	MenuState
	bsf		UpdateDisplayFlag	;set flags to update display
	bsf		L1UpdateFlag
	bcf		PromptUpdateFlag
	bcf		ButtonPress	;initialize ScanKey variables
	movlw	0xFF
	movwf	Debounced1
	movwf	Debounced2
	movwf	LastDebounced1
	movwf	LastDebounced2
	movwf	Toggled
	movwf	Held
	banksel	PORTA
	clrf	PORTA
	banksel	PORTB
	clrf	PORTB
	banksel	PORTC
	clrf	PORTC
	banksel	PORTD
	clrf	PORTD
	banksel	PORTE
	clrf	PORTE
	
	clrf	L1		;L1 initialized to Welcome Message
	movlw	L2EnterCode	;L2 initialized to EnterCode
	movwf	L2
	
	movlw	EELavadoPozo	;Initialize limp_pozo TIME to value stored in EEPROM
	pagesel	ReadData
	call	ReadData
	banksel	LavadoPozo
	movwf	LavadoPozo
	movlw	.60		;Make sure limp_pozo is a value 0-60 minutos
	subwf	LavadoPozo, W
	btfsc	STATUS, C
	clrf	LavadoPozo
	movwf	LavadoPozo
	
	movlw	EELmInicial		;Initialize fast_flush address to value stored in EEPROM
	pagesel	ReadData
	call	ReadData
	banksel	TempLmInicial
	movwf	TempLmInicial
	movlw	.60		;Make sure LmInicial is a value 0-60 minutos
	subwf	LmInicial, W
	btfsc	STATUS, C
	clrf	LmInicial
	movwf	TempLmInicial

	movlw	EELmHoras		;Initialize anti_escal address to value stored in EEPROM
	pagesel	ReadData
	call	ReadData
	banksel	LmHoras
	movwf	LmHoras
	movlw	.24		;Make sure fast_flush c is a value 0-10 horas
	subwf	LmHoras, W
	btfsc	STATUS, C
	clrf	LmHoras
	movwf	TempLmHoras

	movlw	EELmMinutos		;Initialize cloro time to value stored in EEPROM
	pagesel	ReadData
	call	ReadData
	banksel	LmMinutos
	movwf	LmMinutos
	movlw	.60		;Make sure fast_flush is a value 0-60 minutos
	subwf	LmMinutos, W
	btfsc	STATUS, C
	clrf	LmMinutos
	movwf	TempLmMinutos
	

	movlw	EEAlarmas		;Initialize alarmas to value stored in EEPROM
	pagesel	ReadData
	call	ReadData
	banksel	Alarmas
	movwf	Alarmas
	movlw	.3				;Make sure alarmas is a value 0-3 minutos
	subwf	Alarmas, W
	btfsc	STATUS, C
	clrf	Alarmas
	movwf	TempAlarmas
	return
;----------------------------------------------------------------------
; EnableInterrupts
;	- First, clear peripheral interrupt flags            
;   	- Enable Peripheral Interrupts             
;   	- Enable Global Interrupts                    
;----------------------------------------------------------------------
;EnableInterrupts 
;	BANKSEL	PIR1
;	clrf	PIR1
;	clrf	PIR2
;	
;	bsf	INTCON, PEIE	;Enable Peripheral Interrupts
;	bsf	INTCON, GIE	;Enable Global Interrupts
;	return


;****************************************************************************************************
;control de procesos
;****************************************************************************************************


;----------------------------------------------------------------------
;BSOn
;----------------------------------------------------------------------     
BSOn	macro
	banksel	PORTC
	bsf	K1	;set K1 pin high
	endm	

;----------------------------------------------------------------------
;BSOff
;----------------------------------------------------------------------     
BSOff	macro
	banksel	PORTC
	bcf	K1	;set K1 pin high
	endm	



;----------------------------------------------------------------------
;V1On
;----------------------------------------------------------------------     
V1On	macro
	banksel	PORTC
	bsf	K2	;set K2 pin high
	endm	

;----------------------------------------------------------------------
;V1Off
;----------------------------------------------------------------------     
V1Off	macro
	banksel	PORTC
	bcf	K2	;set K2 pin high
	endm	


;----------------------------------------------------------------------
;DAOn
;----------------------------------------------------------------------     
DAOn	macro
	banksel	PORTC
	bsf	K3	;set K3 pin high
	endm	

;----------------------------------------------------------------------
;DAOff
;----------------------------------------------------------------------     
DAOff	macro
	banksel	PORTC
	bcf	K3	;set K3 pin high
	endm	

;----------------------------------------------------------------------
;DCLOn
;----------------------------------------------------------------------     
DCLOn	macro
	banksel	PORTC
	bsf	K4	;set K4 pin high
	endm	

;----------------------------------------------------------------------
;DCLOff
;----------------------------------------------------------------------     
DCLOff	macro
	banksel	PORTC
	bcf	K4	;set K4 pin high
	endm	
;----------------------------------------------------------------------
;V2On
;----------------------------------------------------------------------     
V2On	macro
	banksel	PORTC
	bsf	K5	;set K5 pin high
	endm	

;----------------------------------------------------------------------
;V2Off
;----------------------------------------------------------------------     
V2Off	macro
	banksel	PORTC
	bcf	K5	;set K5 pin high
	endm	
;----------------------------------------------------------------------
;IniHMS
;---------------------------------------------------------------------- 
InitHMS	
	clrf	Horas
	clrf	mins
	clrf	Segs
	return


;******************************************************************************************
;ArrancaProceso
;******************************************************************************************
ArrancaProceso
	banksel	PORTD
	btfss	FL				;Se fija si el flotante se cerro
	goto	ParadaFlotante			;Si llego para el proceso
	

						;check buttons and perform appropriate action
	btfsc	LavadoPozoFlag
	goto	Lavadopozo
	btfsc	LmInicialFlag
	goto	Lminicial
	btfsc	PermeadoFlag
	goto	Permeado
	btfsc	LmCiclicoFlag
	goto	LmCiclico
	goto	EndArrancaProceso		; Si no sigue

Lavadopozo
	btfss	borraTiempoFlag			;inicializa una sola vez Horas, mins Segs
	call	InitHMS
	bsf		borraTiempoFlag			;setea el flag para que se borre eltiempo una sola vez por etapa
	BSOn					;enciende la bomba
	V1On					;enciende la valvula 1
	call	ReadLavadoPozoTime
	movf	TempLavadoPozo,w
	xorwf	mins, W				;se fija si los minutos coinciden con el tiempo seteado	'				btfss	STATUS,Z
	goto	EndArrancaProceso		;si no coinciden se va
	bcf	borraTiempoFlag			;si llego al tiempo seteado borra bandera de tiempo
	V1Off					;apaga V1
	bcf	LavadoPozoFlag			;borra bandera de Lavado de Pozo
	bsf	LmInicialFlag			;Setea la bandera del proximo Paso
	goto	EndArrancaProceso

Lminicial
	btfss	borraTiempoFlag			;inicializa una sola vez Horas, mins Segs
	call	InitHMS
	bsf		borraTiempoFlag			;setea el flag para que se borre eltiempo una sola vez por etapa
	DAOn					;enciende doasador de antiescalante
	V2On					;enciende la valvula 2
	call	ReadLmInicialTime
	movf	TempLmInicial,w
	xorwf	mins, W				;se fija si los minutos coinciden con el tiempo seteado	'				btfss	STATUS,Z
	goto	EndArrancaProceso		;si no coinciden se va
	bcf		borraTiempoFlag			;si llego al tiempo seteado borra bandera de tiempo
	V2Off					;apaga V2
	bcf		LmInicialFlag			;borra bandera de Lavado de membrana inicial
	bsf		PermeadoFlag			;Setea la bandera del proximo Paso
	goto	EndArrancaProceso


Permeado
	btfss	borraTiempoFlag			;inicializa una sola vez Horas, mins Segs
	call	InitHMS
	bsf	borraTiempoFlag			;setea el flag para que se borre eltiempo una sola vez por etapa
	DCLOn					;enciende dosador de Cloro
	call	ReadLmHorasTime
	movf	TempLmHoras,w
	xorwf	Horas, W			;se fija si las horas coinciden con el tiempo seteado	'				btfss	STATUS,Z
	goto	EndArrancaProceso		;si no coinciden se va
	bcf	borraTiempoFlag			;si llego al tiempo seteado borra bandera de tiempo
	DCLOff					;apaga dosador de cloro
	bcf	PermeadoFlag			;borra bandera de Lavado de membrana inicial
	bsf	LmCiclicoFlag			;Setea la bandera del proximo Paso
	goto	EndArrancaProceso




LmCiclico
	btfss	borraTiempoFlag			;inicializa una sola vez Horas, mins Segs
	call	InitHMS
	bsf	borraTiempoFlag			;setea el flag para que se borre eltiempo una sola vez por etapa
	V2On					;enciende valvula 2
	call	ReadLmMinutosTime
	movf	TempLmMinutos,w
	xorwf	mins, W				;se fija si los minutos coinciden con el tiempo seteado	'				btfss	STATUS,Z
	goto	EndArrancaProceso		;si no coinciden se va
	bcf	borraTiempoFlag			;si llego al tiempo seteado borra bandera de tiempo
	V2Off					;apaga dosador de cloro
	bcf	LmCiclicoFlag			;borra bandera de Lavado de membrana inicial
	bsf	PermeadoFlag			;Setea la bandera del proximo Paso
	goto	EndArrancaProceso






ParadaFlotante
	bcf	LavadoPozoFlag
	bcf	LmInicialFlag
	bcf	PermeadoFlag
	bcf	LmCiclicoFlag
	BSOff
	DCLOff
	DAOff
	V1Off
	V2Off




EndArrancaProceso

	return
;******************************************************************************************
;ParaProceso
;******************************************************************************************

ParaProceso
	
	bcf	ArrancaProcesoFlag
	bcf	ParaProcesoFlag
	bcf	LavadoPozoFlag
	bcf	LmInicialFlag
	bcf	PermeadoFlag
	bcf	LmCiclicoFlag
	BSOff
	DCLOff
	DAOff
	V1Off
	V2Off



	return



;parar todo y borrar banderas
;****************************************************************************************************
MenuNav	code	0x0800
;****************************************************************************************************
	goto	$		;This is a trap to prevent runaway code execution
;----------------------------------------------------------------------
;UpdateState
;
;----------------------------------------------------------------------
UpdateState

	movlw	HIGH(UpdateState)
	movwf	PCLATH
;*********
;	bcf	INTCON, INTE		;disable X-10 service
;*********
	movf	MenuState, W
	addwf	PCL, F
	goto	US_EnterCode
	goto	US_MainMenuState
	goto	US_SelectFunctionState		
	goto	US_LavadoPozoTimestate		
	goto	US_LmInicialTimestate		
	goto	US_LmHorasTimestate	
	goto	US_LmMinutosTimestate			
	goto	US_Alarmas				



US_EnterCode
	pagesel	EnterCode
	goto	EnterCode
US_MainMenuState
	pagesel	MainMenuState
	goto	MainMenuState
US_SelectFunctionState
	pagesel	SelectFunctionState
	goto	SelectFunctionState
US_LavadoPozoTimestate
	pagesel	LavadoPozoTimestate		;SetSystemTimeState
	goto	LavadoPozoTimestate		;SetSystemTimeState
US_LmInicialTimestate
	pagesel	LmInicialTimestate		;SetSystemAddrState
	goto	LmInicialTimestate		;SetSystemAddrState
US_LmHorasTimestate
	pagesel	LmHorasTimestate
	goto	LmHorasTimestate
US_LmMinutosTimestate
	pagesel	LmMinutosTimestate
	goto	LmMinutosTimestate
US_Alarmas
	pagesel	Alarma
	goto	Alarma

EndUpdateState
	bcf	ButtonPress
	bsf	UpdateDisplayFlag
;*********
;	bsf	INTCON, INTE		;re-enable X-10 service
;*********
	return

;----------------------------------------------------------------------
;
;	EnterCode
;	Habilitacin de teclado up,up,down,up, enter,exit
;		
;----------------------------------------------------------------------
EnterCode
	movlw	L2EnterCode		;Update second line
	movwf	L2

	banksel	Toggled
	btfss	Toggled, UP
	goto	ECS_UP
	btfss	Toggled, DOWN
	goto	ECS_DOWN
	btfss	Toggled, ENTER
	goto	ECS_ENTER
	btfss	Toggled, EXIT
	goto	ECS_EXIT

ECS_UP
	movlw	UP
	movwf	Button
	incf	Vueltas,F
	goto	sequence
ECS_DOWN
	movlw	DOWN
	movwf	Button
	incf	Vueltas,F
	goto	sequence
ECS_ENTER
	movlw	ENTER
	movwf	Button
	incf	Vueltas,F
	goto	sequence
	
ECS_EXIT

	movlw	ENTER
	movwf	Button
	incf	Vueltas,F
	goto	sequence

sequence
	movf	Vueltas,W
	xorlw	.1
	btfsc	STATUS,Z
	goto	sequence1

	movf	Vueltas,W
	xorlw	.2
	btfsc	STATUS,Z
	goto	sequence2
	
	movf	Vueltas,W
	xorlw	.3
	btfsc	STATUS,Z
	goto	sequence3

	movf	Vueltas,W
	xorlw	.4
	btfsc	STATUS,Z
	goto	sequence4

	movf	Vueltas,W
	xorlw	.5
	btfsc	STATUS,Z
	goto	sequence5

	movf	Vueltas,W
	xorlw	.6
	btfsc	STATUS,Z
	goto	sequence6

	clrf	Vueltas
	goto	EndECS
sequence1

	movf	Button,w				;
	xorlw	UP				;se fija si es igual a UP
	btfss	STATUS,Z			;si es igual sigue sequencia
	goto 	compare				;si no sale
	bsf		Codigo,0	
	goto	EndECS
sequence2
	movf	Button,w				;
	xorlw	UP				;se fija si es igual a UP
	btfss	STATUS,Z			;si es igual sigue sequencia
	goto 	compare				;si no sale
	bsf		Codigo,1	
	goto	EndECS

sequence3
	movf	Button,w				;
	xorlw	DOWN				;se fija si es igual a DOWN
	btfss	STATUS,Z			;si es igual sigue sequencia
	goto 	compare				;si no sale
	bsf		Codigo,2	
	goto	EndECS
sequence4
	movf	Button,w				;
	xorlw	UP				;se fija si es igual a UP
	btfss	STATUS,Z			;si es igual sigue sequencia
	goto 	compare				;si no sale
	bsf		Codigo,3	
	goto	EndECS
sequence5
	movf	Button,w				;
	xorlw	ENTER				;se fija si es igual a ENTER
	btfss	STATUS,Z			;si es igual sigue sequencia
	goto 	compare				;si no sale
	bsf		Codigo,4	
	goto	EndECS
sequence6
	movf	Button,w				;
	xorlw	EXIT				;se fija si es igual a EXIT
	btfss	STATUS,Z			;si es igual sigue sequencia
	goto 	compare				;si no sale
	bsf		Codigo,5	
	goto	EndECS
						
			
compare
	movf	Codigo,W
	xorlw	b'00111111'			
	btfss	STATUS,Z
	goto	clrcode
	pagesel	InitMainMenuState
	call	InitMainMenuState
	goto	EndECS		

clrcode
	clrf	Codigo
EndECS
	pagesel	EndUpdateState
	goto	EndUpdateState
;----------------------------------------------------------------------
;
;	MainMenuState
;	User interface to select function.
;		UP- scrolls forward through functions
;		DOWN- scrolls backward through functions
;		ENTER- selects function
;		EXIT- returns to Main Screen
;----------------------------------------------------------------------

MainMenuState
	movlw	HIGH(MainMenuState)
	movwf	PCLATH		
	movf	MenuSubState, W		;Based on MenuSubState, call appropriate subroutine
	addwf	PCL, F
	goto	call_MMS0
	goto	call_MMS1
	goto	call_MMS2
	goto	call_MMS3

call_MMS0
	pagesel	MMS0
	goto	MMS0

call_MMS1
	pagesel	MMS1
	goto	MMS1

call_MMS2
	pagesel	MMS2
	goto	MMS2

call_MMS3
	pagesel	MMS3
	goto	MMS3
				
ExitMMS
	banksel	Toggled
	btfsc	Toggled, EXIT			;check exit button
	goto	EndMSS
	pagesel	InitEnterCodeState
	call	InitEnterCodeState
EndMSS					
	pagesel	EndUpdateState
	goto	EndUpdateState

;----------------------------------------------------------------------
;MMS0
;----------------------------------------------------------------------
MMS0	
	pagesel	MMS0
	banksel	Toggled
	;check buttons and perform appropriate action
	btfss	Toggled, UP
	goto	MMS0_UP
	btfss	Toggled, DOWN
	goto	MMS0_DOWN
	;btfss	Toggled, MENU
	;goto	SFS0_MENU
	btfss	Toggled, ENTER
	goto	MMS0_ENTER
	goto	EndMMS0

MMS0_UP					;si se toca UP va a MMS1
	movlw	L2_MenuON		;Update second line
	movwf	L2
	movlw	.1
	movwf	MenuSubState
	goto	EndMMS0

MMS0_DOWN				;si se toca DOWN va a MMS1
	movlw	L2_MenuON		;Update second line
	movwf	L2
	movlw	.1
	movwf	MenuSubState
	goto	EndMMS0
MMS0_ENTER
	bsf	ArrancaProcesoFlag		;si se toca ENTER va a MMS2 y levanta bandera arranca proceso
	movlw	L2Menu_OFF		;Update second line
	movwf	L2
	movlw	.2
	movwf	MenuSubState
	goto	EndMMS0	



EndMMS0
	pagesel	ExitMMS
	goto	ExitMMS

;----------------------------------------------------------------------
;MMS1
;----------------------------------------------------------------------
MMS1	
	pagesel	MMS1
	banksel	Toggled
	btfss	Toggled, UP
	goto	MMS1_UP
	btfss	Toggled, DOWN
	goto	MMS1_DOWN
	;btfss	Toggled, MENU
	;goto	MMS1_MENU
	btfss	Toggled, ENTER
	goto	MMS1_ENTER
	goto	EndMMS1
MMS1_UP

	movlw	L2Menu_ON	 ;Update second line
	movwf	L2
	movlw	.0
	movwf	MenuSubState
	goto	EndMMS1

MMS1_DOWN
	movlw	L2Menu_ON	;Update second line
	movwf	L2
	movlw	.0
	movwf	MenuSubState
	goto	EndMMS1

MMS1_ENTER
	pagesel	InitSelectFunctionState
	call	InitSelectFunctionState

EndMMS1
	pagesel	ExitMMS
	goto	ExitMMS

;----------------------------------------------------------------------
;MMS2
;----------------------------------------------------------------------
MMS2	
	pagesel	MMS2
	banksel	Toggled
	btfss	Toggled, UP
	goto	MMS2_UP
	btfss	Toggled, DOWN
	goto	MMS2_DOWN
	;btfss	Toggled, MENU
	;goto	MMS2_MENU
	btfss	Toggled, ENTER
	goto	MMS2_ENTER
	goto	EndMMS2

MMS2_UP

	movlw	L2_MenuOFF		;Update second line
	movwf	L2
	movlw	.3
	movwf	MenuSubState
	goto	EndMMS2

MMS2_DOWN
	movlw	L2_MenuOFF		;Update second line
	movwf	L2
	movlw	.3
	movwf	MenuSubState
	goto	EndMMS2

MMS2_ENTER
	bsf	ParaProcesoFlag		;si se toca ENTER va a MMS2 y levanta bandera para proceso
	movlw	L2Menu_ON		;Update second line
	movwf	L2
	movlw	.0
	movwf	MenuSubState
	goto	EndMMS2	


EndMMS2
	pagesel	ExitMMS
	goto	ExitMMS

;----------------------------------------------------------------------
;MMS3
;----------------------------------------------------------------------
MMS3	
	pagesel	MMS3
	banksel	Toggled
	btfss	Toggled, UP
	goto	MMS3_UP
	btfss	Toggled, DOWN
	goto	MMS3_DOWN
	;btfss	Toggled, MENU
	;goto	MMS3_MENU
	btfss	Toggled, ENTER
	goto	MMS3_ENTER
	goto	EndMMS3

MMS3_UP
	movlw	L2Menu_OFF	;Update second line
	movwf	L2
	movlw	.2
	movwf	MenuSubState
	goto	EndMMS3

MMS3_DOWN
	movlw	L2Menu_OFF	;Update second line
	movwf	L2
	movlw	.2
	movwf	MenuSubState
	goto	EndMMS3

MMS3_ENTER
	pagesel	InitSelectFunctionState
	call	InitSelectFunctionState

EndMMS3
	pagesel	ExitMMS
	goto	ExitMMS



;----------------------------------------------------------------------
;SelectFunctionState
;	User interface to select function.
;		UP- scrolls forward through functions
;		DOWN- scrolls backward through functions
;		MENU- scrolls forward through functions
;		ENTER- selects function
;		EXIT- returns to Main Screen
;----------------------------------------------------------------------
SelectFunctionState
	movlw	HIGH(SelectFunctionState)
	movwf	PCLATH		
	movf	MenuSubState, W		;Based on MenuSubState, call appropriate subroutine
	addwf	PCL, F
	goto	call_SFS0
	goto	call_SFS1
	goto	call_SFS2
	goto	call_SFS3

call_SFS0
	pagesel	SFS0
	goto	SFS0

call_SFS1
	pagesel	SFS1
	goto	SFS1

call_SFS2
	pagesel	SFS2
	goto	SFS2

call_SFS3
	pagesel	SFS3
	goto	SFS3
				
ExitSFS
	banksel	Toggled
	btfsc	Toggled, EXIT			;check exit button
	goto	EndSFS
	pagesel	InitEnterCodeState
	call	InitEnterCodeState
EndSFS					
	pagesel	EndUpdateState
	goto	EndUpdateState

;----------------------------------------------------------------------
;SFS0
;----------------------------------------------------------------------
SFS0	
	pagesel	SFS0
	banksel	Toggled
	;check buttons and perform appropriate action
	btfss	Toggled, UP
	goto	SFS0_UP
	btfss	Toggled, DOWN
	goto	SFS0_DOWN
	;btfss	Toggled, MENU
	;goto	SFS0_MENU
	btfss	Toggled, ENTER
	goto	SFS0_ENTER
	goto	EndSFS0

SFS0_UP

	movlw	L2LmInicial		;Update second line
	movwf	L2
	movlw	.1
	movwf	MenuSubState
	goto	EndSFS0

SFS0_DOWN
	movlw	L2LmMinutos		;Update second line
	movwf	L2
	movlw	.3
	movwf	MenuSubState
	goto	EndSFS0

SFS0_ENTER
	pagesel	InitLavadoPozoTimeState
	call	InitLavadoPozoTimeState				;InitSetSystemTimeState

EndSFS0
	pagesel	ExitSFS
	goto	ExitSFS

;----------------------------------------------------------------------
;SFS1
;----------------------------------------------------------------------
SFS1	
	pagesel	SFS1
	banksel	Toggled
	btfss	Toggled, UP
	goto	SFS1_UP
	btfss	Toggled, DOWN
	goto	SFS1_DOWN
	;btfss	Toggled, MENU
	;goto	SFS1_MENU
	btfss	Toggled, ENTER
	goto	SFS1_ENTER
	goto	EndSFS1

SFS1_UP
	movlw	L2LmHoras	 ;Update second line
	movwf	L2
	movlw	.2
	movwf	MenuSubState
	goto	EndSFS1

SFS1_DOWN
	movlw	L2LavadoPozo		;Update second line
	movwf	L2
	movlw	.0
	movwf	MenuSubState
	goto	EndSFS1

SFS1_ENTER
	pagesel	InitLmInicialTimeState
	call	InitLmInicialTimeState

EndSFS1
	pagesel	ExitSFS
	goto	ExitSFS

;----------------------------------------------------------------------
;SFS2
;----------------------------------------------------------------------
SFS2	
	pagesel	SFS2
	banksel	Toggled
	btfss	Toggled, UP
	goto	SFS2_UP
	btfss	Toggled, DOWN
	goto	SFS2_DOWN
	;btfss	Toggled, MENU
	;goto	SFS2_MENU
	btfss	Toggled, ENTER
	goto	SFS2_ENTER
	goto	EndSFS2

SFS2_UP
	movlw	L2LmMinutos	;Update second line
	movwf	L2
	movlw	.3
	movwf	MenuSubState
	goto	EndSFS2

SFS2_DOWN
	movlw	L2LmInicial		;Update second line
	movwf	L2
	movlw	.1
	movwf	MenuSubState
	goto	EndSFS2

SFS2_ENTER
	pagesel	InitLmHorasTimeState
	call	InitLmHorasTimeState

EndSFS2
	pagesel	ExitSFS
	goto	ExitSFS

;----------------------------------------------------------------------
;SFS3
;----------------------------------------------------------------------
SFS3	
	pagesel	SFS3
	banksel	Toggled
	btfss	Toggled, UP
	goto	SFS3_UP
	btfss	Toggled, DOWN
	goto	SFS3_DOWN
	;btfss	Toggled, MENU
	;goto	SFS3_MENU
	btfss	Toggled, ENTER
	goto	SFS3_ENTER
	goto	EndSFS3

SFS3_UP
	movlw	L2LavadoPozo	;Update second line
	movwf	L2
	movlw	.0
	movwf	MenuSubState
	goto	EndSFS3

SFS3_DOWN
	movlw	L2LmHoras	;Update second line
	movwf	L2
	movlw	.2
	movwf	MenuSubState
	goto	EndSFS3

SFS3_ENTER
	pagesel	InitLmMinutosTimeState
	call	InitLmMinutosTimeState
EndSFS3
	pagesel	ExitSFS
	goto	ExitSFS

;----------------------------------------------------------------------
; LavadoPozoTimestate:
;	user interface to set system time (hours and minutes)
;	SubMenuState=0, initialize variables
;
;	SubMenuState=1, adjust system minutes:
;		UP- increments minutes
;		DOWN- decrements minutes
;		ENTER- goes to confirm new time
;		EXIT- returns to SetSystemHours screen, without changing time
;	SubMenuState=2, confirm time:
;		UP- select between Yes and No
;		DOWN- select between Yes and No
;		ENTER- if Yes is selected, new time is saved, exit to Main screen
;			if No is selected, new time is not saved, exit to Main
;		EXIT- returns to Select Function screen, without changing time
;
;----------------------------------------------------------------------
LavadoPozoTimestate
	movlw	HIGH(LavadoPozoTimestate)
	movwf	PCLATH		
	movf	MenuSubState, W		;Based on MenuSubState, call appropriate subroutine
	addwf	PCL, F
	goto	call_LPTS0
	goto	call_LPTS1
	

call_LPTS0
	pagesel	LPTS0
	goto	LPTS0

call_LPTS1
	pagesel	LPTS1
	goto	LPTS1

				
ExitLPTS
	pagesel	EndLPTS
	banksel	Toggled
	btfsc	Toggled, EXIT			;check exit button
	goto	EndLPTS
	pagesel	InitEnterCodeState
	call	InitEnterCodeState
EndLPTS					
	pagesel	EndUpdateState
	goto	EndUpdateState

;----------------------------------------------------------------------
;LPTS0
;----------------------------------------------------------------------
LPTS0	
	pagesel	LPTS0
	banksel	Toggled
	;check buttons and perform appropriate action
	btfss	Toggled, UP
	goto	LPTS0_UP
	btfss	Toggled, DOWN
	goto	LPTS0_DOWN
	btfss	Toggled, ENTER
	goto	LPTS0_ENTER
	goto	EndLPTS0

LPTS0_UP
	pagesel	AdjustMinutesUp
	call	AdjustMinutesUp
	pagesel	EndLPTS0
	goto	EndLPTS0

LPTS0_DOWN
	pagesel	AdjustMinutesDown
	call	AdjustMinutesDown
	pagesel	EndLPTS0
	goto	EndLPTS0

LPTS0_ENTER
	movlw	.1
	movwf	MenuSubState

	movlw	P_Okay
	movwf	Prompt
	bsf	PromptUpdateFlag

EndLPTS0
	pagesel	ExitLPTS
	goto	ExitLPTS


;----------------------------------------------------------------------
;LPTS1
;----------------------------------------------------------------------
LPTS1	
	pagesel	LPTS1
	banksel	Toggled
	;check buttons and perform appropriate action
	btfss	Toggled, UP
	goto	LPTS1_UP
	btfss	Toggled, DOWN
	goto	LPTS1_DOWN
	btfss	Toggled, ENTER
	goto	LPTS1_ENTER
	goto	EndLPTS1

LPTS1_UP
	pagesel	ToggleOkay
	call	ToggleOkay
	bsf	PromptUpdateFlag
	pagesel	EndLPTS1
	goto	EndLPTS1

LPTS1_DOWN
	pagesel	ToggleOkay
	call	ToggleOkay
	bsf	PromptUpdateFlag
	pagesel	EndLPTS1
	goto	EndLPTS1

LPTS1_ENTER
	btfss	Okay
	goto	LPTS1_notOKAY		;if Okay is not 1, then return without saving the values
	;movf	TempHours, W
	;movwf	Hours
	movf	TempMins, W
	movwf	TempLavadoPozo
	;clrf	Secs
	;bcf	PM
	;btfsc	TempPM
	;bsf	PM
	banksel	TempLavadoPozo
	movf	TempLavadoPozo, W
	movwf	VALUE			;Save new LavadoPozo to EEPROM
	movlw	EELavadoPozo
	pagesel	WriteData
	call	WriteData
LPTS1_notOKAY
	pagesel	InitMainMenuState
	call	InitMainMenuState

EndLPTS1
	pagesel	ExitLPTS
	goto	ExitLPTS


;----------------------------------------------------------------------
; LmInicialTimestate:
;	user interface to set system time (hours and minutes)
;	SubMenuState=0, initialize variables
;
;	SubMenuState=1, adjust system minutes:
;		UP- increments minutes
;		DOWN- decrements minutes
;		ENTER- goes to confirm new time
;		EXIT- returns to SetSystemHours screen, without changing time
;	SubMenuState=2, confirm time:
;		UP- select between Yes and No
;		DOWN- select between Yes and No
;		ENTER- if Yes is selected, new time is saved, exit to Main screen
;			if No is selected, new time is not saved, exit to Main
;		EXIT- returns to Select Function screen, without changing time
;
;----------------------------------------------------------------------
LmInicialTimestate
	movlw	HIGH(LmInicialTimestate)
	movwf	PCLATH		
	movf	MenuSubState, W		;Based on MenuSubState, call appropriate subroutine
	addwf	PCL, F
	goto	call_LMITS0
	goto	call_LMITS1
	

call_LMITS0
	pagesel	LMITS0
	goto	LMITS0

call_LMITS1
	pagesel	LMITS1
	goto	LMITS1

				
ExitLMITS
	pagesel	EndLMITS
	banksel	Toggled
	btfsc	Toggled, EXIT			;check exit button
	goto	EndLMITS
	pagesel	InitEnterCodeState
	call	InitEnterCodeState
EndLMITS					
	pagesel	EndUpdateState
	goto	EndUpdateState

;----------------------------------------------------------------------
;LMITS0
;----------------------------------------------------------------------
LMITS0	
	pagesel	LMITS0
	banksel	Toggled
	;check buttons and perform appropriate action
	btfss	Toggled, UP
	goto	LMITS0_UP
	btfss	Toggled, DOWN
	goto	LMITS0_DOWN
	btfss	Toggled, ENTER
	goto	LMITS0_ENTER
	goto	EndLMITS0

LMITS0_UP
	pagesel	AdjustMinutesUp
	call	AdjustMinutesUp
	pagesel	EndLMITS0
	goto	EndLMITS0

LMITS0_DOWN
	pagesel	AdjustMinutesDown
	call	AdjustMinutesDown
	pagesel	EndLMITS0
	goto	EndLMITS0

LMITS0_ENTER
	movlw	.1
	movwf	MenuSubState

	movlw	P_Okay
	movwf	Prompt
	bsf	PromptUpdateFlag

EndLMITS0
	pagesel	ExitLMITS
	goto	ExitLMITS


;----------------------------------------------------------------------
;LMITS1
;----------------------------------------------------------------------
LMITS1	
	pagesel	LMITS1
	banksel	Toggled
	;check buttons and perform appropriate action
	btfss	Toggled, UP
	goto	LMITS1_UP
	btfss	Toggled, DOWN
	goto	LMITS1_DOWN
	btfss	Toggled, ENTER
	goto	LMITS1_ENTER
	goto	EndLMITS1

LMITS1_UP
	pagesel	ToggleOkay
	call	ToggleOkay
	bsf	PromptUpdateFlag
	pagesel	EndLMITS1
	goto	EndLMITS1

LMITS1_DOWN
	pagesel	ToggleOkay
	call	ToggleOkay
	bsf	PromptUpdateFlag
	pagesel	EndLMITS1
	goto	EndLMITS1

LMITS1_ENTER
	btfss	Okay
	goto	LMITS1_notOKAY		;if Okay is not 1, then return without saving the values
	;movf	TempHours, W
	;movwf	Hours
	movf	TempMins, W
	movwf	TempLmInicial
	;clrf	Secs
	;bcf	PM
	;btfsc	TempPM
	;bsf	PM
	banksel	TempLmInicial
	movf	TempLmInicial, W
	movwf	VALUE			;Save new LmHoras to EEPROM
	movlw	EELmInicial
	pagesel	WriteData
	call	WriteData
LMITS1_notOKAY
	pagesel	InitMainMenuState
	call	InitMainMenuState

EndLMITS1
	pagesel	ExitLMITS
	goto	ExitLMITS

;----------------------------------------------------------------------
; LmHorasTimestate:
;	user interface to set system time (hours and minutes)
;	SubMenuState=0, initialize variables
;
;	SubMenuState=1, adjust system minutes:
;		UP- increments minutes
;		DOWN- decrements minutes
;		ENTER- goes to confirm new time
;		EXIT- returns to SetSystemHours screen, without changing time
;	SubMenuState=2, confirm time:
;		UP- select between Yes and No
;		DOWN- select between Yes and No
;		ENTER- if Yes is selected, new time is saved, exit to Main screen
;			if No is selected, new time is not saved, exit to Main
;		EXIT- returns to Select Function screen, without changing time
;
;----------------------------------------------------------------------
LmHorasTimestate
	movlw	HIGH(LmHorasTimestate)
	movwf	PCLATH		
	movf	MenuSubState, W		;Based on MenuSubState, call appropriate subroutine
	addwf	PCL, F
	goto	call_LMHTS0
	goto	call_LMHTS1
	

call_LMHTS0
	pagesel	LMHTS0
	goto	LMHTS0

call_LMHTS1
	pagesel	LMHTS1
	goto	LMHTS1

				
ExitLMHTS
	pagesel	EndLMHTS
	banksel	Toggled
	btfsc	Toggled, EXIT			;check exit button
	goto	EndLMHTS
	pagesel	InitEnterCodeState
	call	InitEnterCodeState
EndLMHTS					
	pagesel	EndUpdateState
	goto	EndUpdateState

;----------------------------------------------------------------------
;LMHTS0
;----------------------------------------------------------------------
LMHTS0	
	pagesel	LMHTS0
	banksel	Toggled
	;check buttons and perform appropriate action
	btfss	Toggled, UP
	goto	LMHTS0_UP
	btfss	Toggled, DOWN
	goto	LMHTS0_DOWN
	btfss	Toggled, ENTER
	goto	LMHTS0_ENTER
	goto	EndLMHTS0

LMHTS0_UP
	pagesel	AdjustHoursUp
	call	AdjustHoursUp
	pagesel	EndLMHTS0
	goto	EndLMHTS0

LMHTS0_DOWN
	pagesel	AdjustHoursDown
	call	AdjustHoursDown
	pagesel	EndLMHTS0
	goto	EndLMHTS0

LMHTS0_ENTER
	movlw	.1
	movwf	MenuSubState

	movlw	P_Okay
	movwf	Prompt
	bsf	PromptUpdateFlag

EndLMHTS0
	pagesel	ExitLMHTS
	goto	ExitLMHTS


;----------------------------------------------------------------------
;LMHTS1
;----------------------------------------------------------------------
LMHTS1	
	pagesel	LMHTS1
	banksel	Toggled
	;check buttons and perform appropriate action
	btfss	Toggled, UP
	goto	LMHTS1_UP
	btfss	Toggled, DOWN
	goto	LMHTS1_DOWN
	btfss	Toggled, ENTER
	goto	LMHTS1_ENTER
	goto	EndLMHTS1

LMHTS1_UP
	pagesel	ToggleOkay
	call	ToggleOkay
	bsf	PromptUpdateFlag
	pagesel	EndLMHTS1
	goto	EndLMHTS1

LMHTS1_DOWN
	pagesel	ToggleOkay
	call	ToggleOkay
	bsf	PromptUpdateFlag
	pagesel	EndLMHTS1
	goto	EndLMHTS1

LMHTS1_ENTER
	btfss	Okay
	goto	LMHTS1_notOKAY		;if Okay is not 1, then return without saving the values
	movf	TempHours, W
	movwf	TempLmHoras
	;movf	TempMins, W
	;movwf	TempLmInicial
	;clrf	Secs
	;bcf	PM
	;btfsc	TempPM
	;bsf	PM
	banksel	TempLmHoras
	movf	TempLmHoras, W
	movwf	VALUE			;Save new LmHoras to EEPROM
	movlw	EELmHoras
	pagesel	WriteData
	call	WriteData
LMHTS1_notOKAY
	pagesel	InitMainMenuState
	call	InitMainMenuState

EndLMHTS1
	pagesel	ExitLMHTS
	goto	ExitLMHTS


;----------------------------------------------------------------------
; LmMinutosTimestate:
;	user interface to set system time (hours and minutes)
;	SubMenuState=0, initialize variables
;
;	SubMenuState=1, adjust system minutes:
;		UP- increments minutes
;		DOWN- decrements minutes
;		ENTER- goes to confirm new time
;		EXIT- returns to SetSystemHours screen, without changing time
;	SubMenuState=2, confirm time:
;		UP- select between Yes and No
;		DOWN- select between Yes and No
;		ENTER- if Yes is selected, new time is saved, exit to Main screen
;			if No is selected, new time is not saved, exit to Main
;		EXIT- returns to Select Function screen, without changing time
;
;----------------------------------------------------------------------
LmMinutosTimestate
	movlw	HIGH(LmMinutosTimestate)
	movwf	PCLATH		
	movf	MenuSubState, W		;Based on MenuSubState, call appropriate subroutine
	addwf	PCL, F
	goto	call_LMMTS0
	goto	call_LMMTS1
	

call_LMMTS0
	pagesel	LMMTS0
	goto	LMMTS0

call_LMMTS1
	pagesel	LMMTS1
	goto	LMMTS1

				
ExitLMMTS
	pagesel	EndLMMTS
	banksel	Toggled
	btfsc	Toggled, EXIT			;check exit button
	goto	EndLMMTS
	pagesel	InitEnterCodeState
	call	InitEnterCodeState
EndLMMTS					
	pagesel	EndUpdateState
	goto	EndUpdateState

;----------------------------------------------------------------------
;LMMTS0
;----------------------------------------------------------------------
LMMTS0	
	pagesel	LMMTS0
	banksel	Toggled
	;check buttons and perform appropriate action
	btfss	Toggled, UP
	goto	LMMTS0_UP
	btfss	Toggled, DOWN
	goto	LMMTS0_DOWN
	btfss	Toggled, ENTER
	goto	LMMTS0_ENTER
	goto	EndLMMTS0

LMMTS0_UP
	pagesel	AdjustMinutesUp
	call	AdjustMinutesUp
	pagesel	EndLMMTS0
	goto	EndLMMTS0

LMMTS0_DOWN
	pagesel	AdjustMinutesDown
	call	AdjustMinutesDown
	pagesel	EndLMMTS0
	goto	EndLMMTS0

LMMTS0_ENTER
	movlw	.1
	movwf	MenuSubState

	movlw	P_Okay
	movwf	Prompt
	bsf	PromptUpdateFlag

EndLMMTS0
	pagesel	ExitLMMTS
	goto	ExitLMMTS


;----------------------------------------------------------------------
;LMMTS1
;----------------------------------------------------------------------
LMMTS1	
	pagesel	LMMTS1
	banksel	Toggled
	;check buttons and perform appropriate action
	btfss	Toggled, UP
	goto	LMMTS1_UP
	btfss	Toggled, DOWN
	goto	LMMTS1_DOWN
	btfss	Toggled, ENTER
	goto	LMMTS1_ENTER
	goto	EndLMMTS1

LMMTS1_UP
	pagesel	ToggleOkay
	call	ToggleOkay
	bsf	PromptUpdateFlag
	pagesel	EndLMMTS1
	goto	EndLMMTS1

LMMTS1_DOWN
	pagesel	ToggleOkay
	call	ToggleOkay
	bsf	PromptUpdateFlag
	pagesel	EndLMMTS1
	goto	EndLMMTS1

LMMTS1_ENTER
	btfss	Okay
	goto	LMMTS1_notOKAY		;if Okay is not 1, then return without saving the values
	;movf	TempHours, W
	;movwf	Hours
	movf	TempMins, W
	movwf	TempLmMinutos
	;clrf	Secs
	;bcf	PM
	;btfsc	TempPM
	;bsf	PM

	banksel	TempLmMinutos
	movf	TempLmMinutos, W
	movwf	VALUE			;Save new LmMinutos to EEPROM
	movlw	EELmMinutos
	pagesel	WriteData
	call	WriteData




LMMTS1_notOKAY
	pagesel	InitMainMenuState
	call	InitMainMenuState

EndLMMTS1
	pagesel	ExitLMMTS
	goto	ExitLMMTS
;----------------------------------------------------------------------
;
;	Alarmas
;	Habilitacin de teclado up,up,down,up, enter,exit
;		
;----------------------------------------------------------------------
Alarma
	;movlw	L2EnterCode		;Update second line
	;movwf	L2

	banksel	Toggled
	btfss	Toggled, UP
	goto	AL_UP
	btfss	Toggled, DOWN
	goto	AL_DOWN
	btfss	Toggled, ENTER
	goto	AL_ENTER
	btfss	Toggled, EXIT
	goto	AL_EXIT

AL_UP
	movlw	UP
	movwf	Button
	incf	Vueltas,F
	goto	sequence_1
AL_DOWN
	movlw	DOWN
	movwf	Button
	incf	Vueltas,F
	goto	sequence_1
AL_ENTER
	movlw	ENTER
	movwf	Button
	incf	Vueltas,F
	goto	sequence_1
	
AL_EXIT
	bsf	BUZZ
	movlw	ENTER
	movwf	Button
	incf	Vueltas,F
	goto	sequence1

sequence_1
	movf	Vueltas,W
	xorlw	.1
	btfsc	STATUS,Z
	goto	sequence11

	movf	Vueltas,W
	xorlw	.2
	btfsc	STATUS,Z
	goto	sequence12
	
	movf	Vueltas,W
	xorlw	.3
	btfsc	STATUS,Z
	goto	sequence13

	movf	Vueltas,W
	xorlw	.4
	btfsc	STATUS,Z
	goto	sequence14

	movf	Vueltas,W
	xorlw	.5
	btfsc	STATUS,Z
	goto	sequence15

	movf	Vueltas,W
	xorlw	.6
	btfsc	STATUS,Z
	goto	sequence16

	clrf	Vueltas
	goto	EndAL
sequence11

	movf	Button,w				;
	xorlw	UP				;se fija si es igual a UP
	btfss	STATUS,Z			;si es igual sigue sequencia
	goto 	compare1				;si no sale
	bsf		Codigo,0	
	goto	EndECS
sequence12
	movf	Button,w				;
	xorlw	UP				;se fija si es igual a UP
	btfss	STATUS,Z			;si es igual sigue sequencia
	goto 	compare1				;si no sale
	bsf		Codigo,1	
	goto	EndECS

sequence13
	movf	Button,w				;
	xorlw	DOWN				;se fija si es igual a DOWN
	btfss	STATUS,Z			;si es igual sigue sequencia
	goto 	compare1				;si no sale
	bsf	Codigo,2	
	goto	EndECS
sequence14
	movf	Button,w				;
	xorlw	UP				;se fija si es igual a UP
	btfss	STATUS,Z			;si es igual sigue sequencia
	goto 	compare1				;si no sale
	bsf		Codigo,3	
	goto	EndECS
sequence15
	movf	Button,w				;
	xorlw	ENTER				;se fija si es igual a ENTER
	btfss	STATUS,Z			;si es igual sigue sequencia
	goto 	compare1				;si no sale
	bsf		Codigo,4	
	goto	EndECS
sequence16
	movf	Button,w				;
	xorlw	EXIT				;se fija si es igual a EXIT
	btfss	STATUS,Z			;si es igual sigue sequencia
	goto 	compare1				;si no sale
	bsf		Codigo,5	
	goto	EndECS
						
			
compare1
	movf	Codigo,W
	xorlw	b'00111111'			
	btfss	STATUS,Z
	goto	clrcode1
	banksel	PORTE	
	bcf	P1_AL
	bcf	P2_AL
	bcf	C1_AL
	bcf	BUZZER
	bcf	BUZZ	
	bcf	borraTiempoFlag	
	pagesel	InitMainMenuState
	call	InitMainMenuState
	goto	EndAL		

clrcode1
	
	clrf	Codigo
	btfsc	BUZZ
	goto	hey

	pagesel	InitHMS
	btfss	borraTiempoFlag			;inicializa una sola vez Horas, mins Segs
	call	InitHMS
	bsf	borraTiempoFlag			;setea el flag para que se borre eltiempo una sola vez por etapa
	movf	mins,w
	xorlw	.1				;se fija si las horas coinciden con el tiempo seteado	'				btfss	STATUS,Z
	goto	EndAL		;si no coinciden se va
	bcf	borraTiempoFlag			;si llego al tiempo seteado borra bandera de tiempo
	movlw 	b'00010000'			; mask for bit 3
	xorwf 	PORTC,f						
	goto	EndAL					
hey	
	bcf	BUZZER
	bcf	borraTiempoFlag
EndAL	
	pagesel	EndUpdateState
	goto	EndUpdateState


	
;----------------------------------------------------------------------
;InitEnterCodeState
;----------------------------------------------------------------------
InitEnterCodeState
	clrf	MenuState
	clrf	MenuSubState

	;movlw	L1Welcome		;Update display settings
	;movwf	L1
	clrf	L1
	bsf	L1UpdateFlag
	movlw	L2EnterCode
	movwf	L2
	return
;----------------------------------------------------------------------
;InitMainMenuState
;----------------------------------------------------------------------
InitMainMenuState
	clrf	MenuState
	clrf	MenuSubState

;	movlw	L1Welcome		;Update display settings
;	movwf	L1
	clrf	L1
	bsf	L1UpdateFlag
	movlw	L2Menu_ON
	movwf	L2
	return

;----------------------------------------------------------------------
;InitSelectFunctionState
;----------------------------------------------------------------------
InitSelectFunctionState
	movlw	L1SelectFunction
	movwf	MenuState		;MainMenuState=SelectFunction
	clrf	MenuSubState

	movlw	L1SelectFunction	;Update display settings
	movwf	L1
	bsf	L1UpdateFlag
	movlw	L2LavadoPozo
	movwf	L2
	return

;----------------------------------------------------------------------
;InitLavadoPozoTimeState
;----------------------------------------------------------------------
InitLavadoPozoTimeState

	movlw	L1LavadoPozo
	movwf	MenuState
	clrf	MenuSubState
	bsf	Okay			;set default Okay to Yes

	movlw	L1LavadoPozo	;Update display settings
	movwf	L1
	bsf	L1UpdateFlag
	movlw	L2DisplaySetMins
	movwf	L2

	movlw	P_SetMin
	movwf	Prompt
	bsf	PromptUpdateFlag

	banksel	TempHours		
	;movf	Hours, W
	;movwf	TempHours
	movf	Mins, W
	movwf	TempMins
	;bcf	TempPM
	;btfsc	PM
	;bsf	TempPM



	return

;----------------------------------------------------------------------
;InitLmInicialTimeState
;----------------------------------------------------------------------
InitLmInicialTimeState
	movlw	L1LmInicial
	movwf	MenuState
	clrf	MenuSubState
	bsf	Okay			;set default Okay to Yes

	movlw	L1LmInicial		;Update display settings
	movwf	L1
	bsf	L1UpdateFlag
	movlw	L2DisplaySetMins
	movwf	L2

	movlw	P_SetMin
	movwf	Prompt
	bsf	PromptUpdateFlag

	banksel	TempHours		
	;movf	Hours, W
	;movwf	TempHours
	movf	Mins, W
	movwf	TempMins
	;bcf	TempPM
	;btfsc	PM
	;bsf	TempPM
	return

;----------------------------------------------------------------------
;InitLmHorasTimeState
;----------------------------------------------------------------------
InitLmHorasTimeState
	movlw	L1LmHoras
	movwf	MenuState
	clrf	MenuSubState

	movlw	L1LmHoras		;Update display settings
	movwf	L1
	bsf	L1UpdateFlag
	movlw	L2DisplaySetHrs
	movwf	L2

	movlw	P_SetHrs
	movwf	Prompt
	bsf	PromptUpdateFlag

	banksel	TempHours		
	movf	Hours, W
	movwf	TempHours
	;movf	Mins, W
	;movwf	TempMins
	;bcf	TempPM
	;btfsc	PM
	;bsf	TempPM
	

	return

;----------------------------------------------------------------------
;InitLmMinutosTimeState
;----------------------------------------------------------------------
InitLmMinutosTimeState

	movlw	L1LmMinutos
	movwf	MenuState
	clrf	MenuSubState
	bsf	Okay			;set default Okay to Yes

	movlw	L1LmMinutos	;Update display settings
	movwf	L1
	bsf	L1UpdateFlag
	movlw	L2DisplaySetMins
	movwf	L2

	movlw	P_SetMin
	movwf	Prompt
	bsf	PromptUpdateFlag


	banksel	TempHours		
	;movf	Hours, W
	;movwf	TempHours
	movf	Mins, W
	movwf	TempMins
	;bcf	TempPM
	;btfsc	PM
	;bsf	TempPM

	return

;----------------------------------------------------------------------
;InitP1ElevadaState
;----------------------------------------------------------------------
InitP1ElevadaState
	movlw	L1Alarma
	movwf	MenuState
	clrf	MenuSubState
	movlw	L1Alarma		;Update display settings
	movwf	L1
	bsf	L1UpdateFlag
	movlw	L2P1Elevada
	movwf	L2
	banksel	PORTE	
	bsf	P1_AL

	return

;----------------------------------------------------------------------
;InitP2ElevadaState
;----------------------------------------------------------------------
InitP2ElevadaState
	movlw	L1Alarma
	movwf	MenuState
	clrf	MenuSubState
	movlw	L1Alarma		;Update display settings
	movwf	L1
	bsf	L1UpdateFlag
	movlw	L2P2Elevada
	movwf	L2
	banksel	PORTE	
	bsf	P2_AL

	return
;----------------------------------------------------------------------
;InitC1ElevadaState
;----------------------------------------------------------------------
InitC1ElevadaState
	movlw	L1Alarma
	movwf	MenuState
	clrf	MenuSubState
	movlw	L1Alarma		;Update display settings
	movwf	L1
	bsf	L1UpdateFlag
	movlw	L2C1Elevada
	movwf	L2
	banksel	PORTE	
	bsf	C1_AL

	return


;----------------------------------------------------------------------
; AdjustHoursUp:
;	increments hours when called
;----------------------------------------------------------------------
AdjustHoursUp

	incf	TempHours,F

	movlw	.24
	subwf	TempHours,W
	btfss	STATUS,Z
	goto	EndAdjustHoursUp
	clrf	TempHours

EndAdjustHoursUp
	return

;----------------------------------------------------------------------
; AdjustHoursDown:
;	decrements hours when called
;----------------------------------------------------------------------
AdjustHoursDown
	
	movf	TempHours,F		;check to see if TempMins is at zero already
	btfss	STATUS,Z
	goto	decHours		;if not, simply decrement TempMins
	movlw	.24		;if it is at zero, load Hours with 24 
	movwf	TempHours		;	(which is decremented to 23 on next step) 
decHours
	decf	TempHours,F

	return


	



;----------------------------------------------------------------------
; AdjustMinutesUp:
;	increments minutes when called
;----------------------------------------------------------------------
AdjustMinutesUp
	incf	TempMins,F

	movlw	.60
	subwf	TempMins,W
	btfss	STATUS,Z
	goto	EndAdjustMinutesUp
	clrf	TempMins

EndAdjustMinutesUp
	return

;----------------------------------------------------------------------
; AdjustMinutesDown:
;	decrements minutes when called
;----------------------------------------------------------------------
AdjustMinutesDown
	
	movf	TempMins,F		;check to see if TempMins is at zero already
	btfss	STATUS,Z
	goto	decMins		;if not, simply decrement TempMins
	movlw	.60		;if it is at zero, load Hours with 60 
	movwf	TempMins		;	(which is decremented to 59 on next step) 
decMins
	decf	TempMins,F

	return






;----------------------------------------------------------------------
; ToggleOkay:
;	Toggles Okay from Yes to No, No to Yes
;----------------------------------------------------------------------
ToggleOkay
	btfss	Okay
	goto    SetYes	
	bcf	Okay
	goto	EndToggleOkay
SetYes	
	bsf	Okay
EndToggleOkay
	return

;****************************************************************************************************
;Data EEPROM read and write code
;****************************************************************************************************
;----------------------------------------------------------------------
;WriteData- address to write is in ADDR, data to write is in VALUE
;----------------------------------------------------------------------
WriteData
	pagesel	WriteData
	banksel	EECON1
	btfsc	EECON1, WR	; Wait for write to finish
	goto	$-1		
	banksel	ADDR
	movf	ADDR, W		; Copy address in ADDR to EEADR register
	banksel	EEADR	
	movwf	EEADR		
	banksel	VALUE
	movf	VALUE, W	; Copy address in VALUE to EEDATA register
	banksel	EEDATA
	movwf	EEDATA
	banksel	EECON1		
	bcf	EECON1, EEPGD	; Point to data memory
	
	bcf	INTCON, GIE	; Disable interrupts during write
	bsf	EECON1, WREN	; Enable writes

	movlw	0x55		; Write 55h to EECON2
	movwf	EECON2
	movlw	0xAA		; Write AAh to EECON2
	movwf	EECON2
	bsf	EECON1, WR	; Start write operation
	bsf	INTCON, GIE	; Re-enable interrupts
	bcf	EECON1, WREN	; Disable writes	

	return

;----------------------------------------------------------------------
;ReadData- address to read is in ADDR
;----------------------------------------------------------------------
ReadData
	pagesel	ReadData
	banksel	EECON1
	btfsc	EECON1, WR	; Wait for write to finish
	goto	$-1
	banksel ADDR
	movf	ADDR, W
	banksel EEADR
	movwf	EEADR		; move address in W to EEADR register
	banksel	EECON1
	bcf	EECON1, EEPGD	; Point to Data memory
	bsf	EECON1, RD	; Start read operation
	banksel	EEDATA
	movf	EEDATA, W	; W=EEDATA

	return

;----------------------------------------------------------------------
;ReadLavadoPozoTime- Retrieves Unit On Time from DATA EEPROM and copies into.
;		Unit to retrieve is in TempUnit
;----------------------------------------------------------------------
ReadLavadoPozoTime
	movf	EELavadoPozo, W	
	movwf	ADDR		;put address in ADDR
	pagesel	ReadData	
	call	ReadData	;data at this address is placed in W
	banksel	TempLavadoPozo
	movwf	TempLavadoPozo	;save data in TempLavadoPozo


	return
;----------------------------------------------------------------------
;ReadLmInicialTime- Retrieves Unit On Time from DATA EEPROM and copies into.
;		Unit to retrieve is in TempLmInicial
;----------------------------------------------------------------------
ReadLmInicialTime
	movf	EELmInicial, W	
	movwf	ADDR		;put address in ADDR
	pagesel	ReadData	
	call	ReadData	;data at this address is placed in W
	banksel	TempLmInicial
	movwf	TempLmInicial	;save data in TempLmInicial


	return
;----------------------------------------------------------------------
;ReadLmHorasTime- Retrieves Unit On Time from DATA EEPROM and copies into.
;		Unit to retrieve is in TempLmHoras
;----------------------------------------------------------------------
ReadLmHorasTime
	movf	EELmHoras, W	
	movwf	ADDR		;put address in ADDR
	pagesel	ReadData	
	call	ReadData	;data at this address is placed in W
	banksel	TempLmHoras
	movwf	TempLmHoras	;save data in TempLmHoras


	return
;----------------------------------------------------------------------
;ReadLmMinutosTime- Retrieves Unit On Time from DATA EEPROM and copies into.
;		Unit to retrieve is in TempUnit
;----------------------------------------------------------------------
ReadLmMinutosTime
	movf	EELmMinutos, W	
	movwf	ADDR		;put address in ADDR
	pagesel	ReadData	
	call	ReadData	;data at this address is placed in W
	banksel	TempLmMinutos
	movwf	TempLmMinutos	;save data in TempLmMinutos


	return
;----------------------------------------------------------------------
;ReadAlarmasTime- Retrieves Unit On Time from DATA EEPROM and copies into.
;		Unit to retrieve is in TempUnit
;----------------------------------------------------------------------
ReadAlarmasTime
	movf	EEAlarmas, W	
	movwf	ADDR		;put address in ADDR
	pagesel	ReadData	
	call	ReadData	;data at this address is placed in W
	banksel	TempAlarmas
	movwf	TempAlarmas	;save data in TempAlarmas


	return

;****************************************************************************************************
;Unit Control Routines
;****************************************************************************************************



;*****************
UnitControlCode	code	0x0e00
;*****************
	goto	$		;This is a trap to prevent runaway code execution


;****************************************************************************************************
MessageDisplayCode	code	0x1800 ;Program Memory Page 3
;****************************************************************************************************
	goto	$		;This is a trap to prevent runaway code execution
;----------------------------------------------------------------------
;UpdateLine1
;	Based on the value of L1, display the appropriate message
;	0 = "    OSMO-01      "
;	1 = "Selec. Funcion:"
;	2 = "Lavado Pozo "
;	3 = "L.M. inicial "
;	4 = "L.M horas    "
;	5 = "L.M minutos "
;	6 = "Alarma"
;	
;----------------------------------------------------------------------
UpdateLine1
	pagesel	L1homeLCD
	call	L1homeLCD
	
	movlw	HIGH(UpdateLine1)	;***pre-load PCLATH***
	movwf	PCLATH
	bcf	STATUS, C
	rlf	L1, W		;shift left to index by 2 lines at a time
	addwf	PCL, F
	movlw	M_OSMO_01
	goto	DisplayUpdateLine1
	movlw	M_SELEC_FUNCION
	goto	DisplayUpdateLine1
	movlw	M_LavadoPozo
	goto	DisplayUpdateLine1
	movlw	M_LmInicial
	goto	DisplayUpdateLine1
	movlw	M_LmHoras
	goto	DisplayUpdateLine1
	movlw	M_LmMinutos
	goto	DisplayUpdateLine1
	movlw	M_Alarma
	goto	DisplayUpdateLine1
	
	
DisplayUpdateLine1
	pagesel	DisplayMessage
	call	DisplayMessage

	return

;----------------------------------------------------------------------
;UpdateLine2
;	Based on the value of L2, display the appropriate message
;	0 = "  MENU ->ON "
;	1 = "  MENU ->OFF"
;	2 = "->MENU   ON "
;	3 = "Entrar Secuencia"
;	4 = "->MENU   OFF"
;	5 = " Lavado de Pozo "
;	6 = "L Membrana Inic."
;	7 = "L Membrana Mins." 
;	8 = "L Membrana Hors."
;	9 = DisplaySetMins(00 formato)
;	10=DisplaySethrs(00 formato)
;	10="   P1 Elevada    "
;	11="   P2 Elevada    "
;	12="   C1 Elevada    "

;	If PromptUpdateFlag is set, UpdatePrompt is called
;
;
;----------------------------------------------------------------------
UpdateLine2
	pagesel	L2homeLCD
	call	L2homeLCD		

	movlw	HIGH(UpdateLine2)	;***pre-load PCLATH***
	movwf	PCLATH
	bcf	STATUS, C
	rlf	L2, W		;shift left to index by two lines at a time
	addwf	PCL, F
	movlw	M_Menu_ON
	goto	DisplayUpdateLine2
	movlw	M_Menu_OFF
	goto	DisplayUpdateLine2
	movlw	M__MenuON
	goto	DisplayUpdateLine2
	movlw	M_EnterCode
	goto	DisplayUpdateLine2
	movlw	M__MenuOFF
	goto	DisplayUpdateLine2
	movlw	M_LavadoPozo
	goto	DisplayUpdateLine2	
	movlw	M_LmInicial
	goto	DisplayUpdateLine2
	movlw	M_LmMinutos
	goto	DisplayUpdateLine2
	movlw	M_LmHoras
	goto	DisplayUpdateLine2	
	call	DisplaySetMins
	goto	EndUpdateLine2
	call	DisplaySetHrs
	goto	EndUpdateLine2
	movlw	M_P1Elevada
	goto	DisplayUpdateLine2
	movlw	M_P2Elevada
	goto	DisplayUpdateLine2
	movlw	M_C1Elevada
	goto	DisplayUpdateLine2


DisplayUpdateLine2
	pagesel	DisplayMessage
	call	DisplayMessage

EndUpdateLine2

	return

;----------------------------------------------------------------------
;UpdatePrompt
;	Based on the value of Prompt, display the appropriate message
;	0 = "MINUTOS"
;	1 = " HORAS "
;	2 = " Okay? " and call DisplayOkay to display "Y" or "N"
;;	3 = DisplayYN	(displays Y or N only, without "Okay?" prompt)
;----------------------------------------------------------------------

UpdatePrompt

	movlw	HIGH(UpdatePrompt)	;***pre-load PCLATH***
	movwf	PCLATH	
	bcf	STATUS, C
	rlf	Prompt, W
	addwf	PCL, F
	movlw	M_Minutos
	goto	DisplayUpdatePrompt
	movlw	M_Horas
	goto	DisplayUpdatePrompt
	movlw	M_Okay
	goto	DisplayUpdatePrompt
	
	pagesel	DisplayOkay
	call	DisplayOkay
	pagesel	EndUpdatePrompt
	goto	EndUpdatePrompt	
	
DisplayUpdatePrompt
	pagesel	DisplayMessage
	call	DisplayMessage

	movf	Prompt, W			;If prompt is Okay?, also display Y or N
	sublw	0x02
	pagesel	DisplayOkay
	btfsc	STATUS, Z
	call	DisplayOkay

EndUpdatePrompt	
	return

;----------------------------------------------------------------------
;UpdateDisplay
;	If L1UpdateFlag is set, then update line 1
;	Always update line 2
;----------------------------------------------------------------------
UpdateDisplay
	
	pagesel	UpdateLine1
	btfsc	L1UpdateFlag
	call	UpdateLine1

	pagesel	UpdateLine2
	call	UpdateLine2

	pagesel	UpdatePrompt
	btfsc	PromptUpdateFlag
	call	UpdatePrompt

	bcf	UpdateDisplayFlag
	bcf	L1UpdateFlag
	bcf	PromptUpdateFlag

	return



;----------------------------------------------------------------------
;DisplayOkay 
;	If Okay flag is 0 display 'N', if 1 display 'Y'
;----------------------------------------------------------------------
DisplayOkay
	btfss	Okay			;If Okay flag is 0
	goto	DisplayNo		; display 'N' for no
	movlw	'S'				;Otherwise, display 's' for yes
	goto	EndDisplayOkay	
DisplayNo
	movlw	'N'			
EndDisplayOkay
	pagesel LCDPUTCHAR	
	call	LCDPUTCHAR
	movlw	' '
	call	LCDPUTCHAR
	movlw	' '
	call	LCDPUTCHAR
	return

;----------------------------------------------------------------------
;DisplaySetTime - Moves cursor to home of line 2,
;calls Bin2BCD and displays hours, minutes, seconds as shown below.
;	******************
;	**
;	*MM        *
;	******************
;----------------------------------------------------------------------
DisplaySetMins
	pagesel	L2homeLCD
	call	L2homeLCD

	movf	TempLmMinutos, W		;Display TempHours
	pagesel	DisplayDigits
	call	DisplayDigits

	pagesel	LCDPUTCHAR
	movlw	' '
	call	LCDPUTCHAR

	pagesel	UpdateLine2	

	return

;----------------------------------------------------------------------
;DisplaySetTime - Moves cursor to home of line 2,
;calls Bin2BCD and displays hours, minutes, seconds as shown below.
;	******************
;	**
;	*HH        *
;	******************
;----------------------------------------------------------------------
DisplaySetHrs
	pagesel	L2homeLCD
	call	L2homeLCD

	movf	TempLmHoras, W		;Display TempHours
	pagesel	DisplayDigits
	call	DisplayDigits

	pagesel	LCDPUTCHAR
	movlw	' '
	call	LCDPUTCHAR

	pagesel	UpdateLine2	

	return




;**********************************************************************
;MessageDisplayCode2	code	0x1900
;**********************************************************************
	goto	$		;This is a trap to prevent runaway code execution
;----------------------------------------------------------------------
;DisplayDigits - Takes value in W and displays as two BCD digits
;	******************
;	*XX*
;	******************
;	where X is 0-9
;----------------------------------------------------------------------
DisplayDigits

	pagesel	Bin2BCD
	call	Bin2BCD
	pagesel LCDPUTCHAR
	movf	BCDH, W
	call	LCDPUTCHAR
	movf	BCDL, W
	call	LCDPUTCHAR

	return

;----------------------------------------------------------------------
;Bin2BCD - converts binary variable in W to BCD variable, stored in BCDH
;		and BCDL
;----------------------------------------------------------------------
Bin2BCD
	movwf	BCDL		;store binary variable in BCDL
	clrf	BCDH		;clear BCDH
BCDtenth			
	movlw	.10		; Loop to subtract 10
	subwf	BCDL,W		; from low byte and
	btfss	STATUS,C	; increment high byte
	goto	BCDEnd		; while low byte is > 0
	movwf	BCDL
	incf	BCDH,F
	goto	BCDtenth
BCDEnd
	movlw	0x30		;add 0x30 ('0') to convert
	addwf	BCDL, F		;variables from 0-9 to ASCII
	addwf	BCDH, F		;equivalent of '0' to '9'

	return

;**********************************************************************
DisplayTables	code	0x1900
;**********************************************************************
	goto	$		;This is a trap to prevent runaway code execution
;----------------------------------------------------------------------
; DisplayMessage
;	W->Index1 contains the number of the string to be displayed
;		0  "    OSMO-01     "	
;		1  "Selec. Funcion: "
;		2  " Lavado de Pozo " 	
;		3  " Lav Mem Inicial"
;		4  " Lav Mem Horas  "
;		5  " Lav Mem Minutos"
;		6  "     Alarma     "
;		7  "Entrar Secuencia"
;		8  " Presion 1 Alta "
;		9  " Presion 2 Alta "
;		10 "Conduct. 1 Alta "
;		11 "Minutos"
;---below message tables are in DisplayTables2 area---
;		12 "Okay? "
;		13 "Horas"
;		14 "   Menu  -->ON  "
;		15 "-->Menu     ON  "
;		16 "   Menu  -->OFF "
;		17 "-->Menu     OFF "
;	Index2 is used to point to each character within the table.                          
;----------------------------------------------------------------------
DisplayMessage
	movwf	Index1
	clrf	Index2
DTLoop
	movlw	HIGH(DisplayMessage)	;***pre-load PCLATH***
	movwf	PCLATH
	bcf	STATUS, C
	movf	Index1, W
	addwf	PCL, F
	goto	call_DTWelcome
	goto	call_DTSelectFunction
	goto	call_DTLavadoPozo
	goto	call_DTLmInicial
	goto	call_DTLmHoras
	goto	call_DTLmMinutos
	goto	call_DTAlarma
	goto	call_DTEnterCode
	goto	call_DTP1Elevada
	goto	call_DTP2Elevada
	goto	call_DTC1Elevada
	goto	call_DTMinutos
	goto	call_DTOkay
	goto	call_DTHoras
	goto	call_DTMenu_ON
	goto	call_DT_MenuON
	goto	call_DTMenu_OFF
	goto	call_DT_MenuOFF

call_DTWelcome
	pagesel	DTWelcome
	call	DTWelcome
	pagesel	DTCheck
	goto	DTCheck

call_DTSelectFunction
	pagesel	DTSelectFunction
	call	DTSelectFunction
	pagesel	DTCheck
	goto	DTCheck

call_DTLavadoPozo
	pagesel	DTLavadoPozo
	call	DTLavadoPozo
	pagesel	DTCheck
	goto	DTCheck

call_DTLmInicial
	pagesel	DTLmInicial
	call	DTLmInicial
	pagesel	DTCheck
	goto	DTCheck

call_DTLmHoras
	pagesel	DTLmHoras
	call	DTLmHoras
	pagesel	DTCheck
	goto	DTCheck

call_DTLmMinutos
	pagesel	DTLmMinutos
	call	DTLmMinutos
	pagesel	DTCheck
	goto	DTCheck

call_DTAlarma
	pagesel	DTAlarma
	call	DTAlarma
	pagesel	DTCheck
	goto	DTCheck

call_DTEnterCode
	pagesel	DTEnterCode
	call	DTEnterCode
	pagesel	DTCheck
	goto	DTCheck

call_DTP1Elevada
	pagesel	DTP1Elevada
	call	DTP1Elevada
	pagesel	DTCheck
	goto	DTCheck

call_DTP2Elevada
	pagesel	DTP2Elevada
	call	DTP2Elevada
	pagesel	DTCheck
	goto	DTCheck

call_DTC1Elevada
	pagesel	DTC1Elevada
	call	DTC1Elevada
	pagesel	DTCheck
	goto	DTCheck

call_DTMinutos
	pagesel	DTMinutos
	call	DTMinutos
	pagesel	DTCheck
	goto	DTCheck

call_DTOkay
	pagesel	DTOkay
	call	DTOkay
	pagesel	DTCheck
	goto	DTCheck

call_DTHoras
	pagesel	DTHoras
	call	DTHoras
	pagesel	DTCheck
	goto	DTCheck

call_DTMenu_ON
	pagesel	DTMenu_ON
	call	DTMenu_ON
	pagesel	DTCheck
	goto	DTCheck

call_DT_MenuON
	pagesel	DT_MenuON
	call	DT_MenuON
	pagesel	DTCheck
	goto	DTCheck

call_DTMenu_OFF
	pagesel	DTMenu_OFF
	call	DTMenu_OFF
	pagesel	DTCheck
	goto	DTCheck

call_DT_MenuOFF
	pagesel	DT_MenuOFF
	call	DT_MenuOFF
	pagesel	DTCheck
	goto	DTCheck

DTCheck
	movwf	TempA
	sublw	'%'
	btfsc	STATUS, Z
	goto	DTEnd
	movf	TempA, W
	pagesel	LCDPUTCHAR
	call	LCDPUTCHAR
	incf	Index2, F
	pagesel	DTLoop
	goto 	DTLoop	
DTEnd
	return

;----------------------------------------------------------------------
;DTWelcome 
;	******************
;	*    OSMO-01     *
;	****************** 
;----------------------------------------------------------------------
DTWelcome
	movlw	HIGH(DTWelcome)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"    OSMO-01     %"




;----------------------------------------------------------------------
;DTSelectFunction 
;	******************
;	*Selec. Funcion: *
;	******************                          
;----------------------------------------------------------------------
DTSelectFunction
	movlw	HIGH(DTSelectFunction)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"Selec. Funcion: %"	;% indicates end of table

;----------------------------------------------------------------------
;DTLavadoPozo   
;	******************
;	* Lavado de Pozo *
;	******************                          
;----------------------------------------------------------------------
DTLavadoPozo
	movlw	HIGH(DTLavadoPozo)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	" Lavado de Pozo %"	;% indicates end of table

;**********************************************************************
DisplayTables2	code	0x1a00		;necessary to prevent tables from crossing page boundaries (ie: 19 to 1a)
;**********************************************************************
	goto	$		;This is a trap to prevent runaway code execution
;----------------------------------------------------------------------
;DTLmInicial
;	******************
;	*Lav Mem Inicial *
;	****************** 
;----------------------------------------------------------------------
DTLmInicial
	movlw	HIGH(DTLmInicial)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"Lav Mem Inicial %"	;% indicates end of table


;----------------------------------------------------------------------
;DTLmHoras
;	******************
;	* Lav Mem Horas  *
;	****************** 
;----------------------------------------------------------------------
DTLmHoras
	movlw	HIGH(DTLmHoras)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	" Lav Mem Horas  %"	;% indicates end of table
	
;----------------------------------------------------------------------
;DTLmMinutos
;	******************
;	*Lav Mem Minutos *
;	****************** 
;----------------------------------------------------------------------
DTLmMinutos
	movlw	HIGH(DTLmMinutos)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"Lav Mem Minutos %"	;% indicates end of table


;----------------------------------------------------------------------
;DTAlarma
;	******************
;	*     Alarma     *
;	****************** 
;----------------------------------------------------------------------
DTAlarma
	movlw	HIGH(DTAlarma)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"     Alarma     %"	;% indicates end of table
;----------------------------------------------------------------------
;DTEnterCode
;	******************
;	*Entrar Secuencia*
;	****************** 
;----------------------------------------------------------------------
DTEnterCode
	movlw	HIGH(DTEnterCode)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"Entrar Secuencia%"	;% indicates end of table
	

;----------------------------------------------------------------------
;DTP1Elevada
;	******************
;	* Presion 1 Alta *
;	****************** 
;----------------------------------------------------------------------
DTP1Elevada
	movlw	HIGH(DTP1Elevada)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	" Presion 1 Alta %"	;% indicates end of table

;----------------------------------------------------------------------
;DTP2Elevada  
;	******************
;	* Presion 2 Alta *
;	******************                        
;----------------------------------------------------------------------
DTP2Elevada
	movlw	HIGH(DTP2Elevada)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	" Presion 2 Alta %"	;% indicates end of table
	

;----------------------------------------------------------------------
;DTC1Elevada
;	******************
;	*Conduct. 1 Alta *
;	****************** 
;----------------------------------------------------------------------
DTC1Elevada
	movlw	HIGH(DTC1Elevada)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"Conduct. 1 Alta %"	;% indicates end of table

;----------------------------------------------------------------------
;DTMinutos
;	******************
;	*   Minutos	 *
;	****************** 
;----------------------------------------------------------------------
DTMinutos
	movlw	HIGH(DTMinutos)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"   Minutos%"	;% indicates end of table
	
;----------------------------------------------------------------------
;DTOkay
;	******************
;	*      Okay?     *
;	****************** 
;----------------------------------------------------------------------
DTOkay
	movlw	HIGH(DTOkay)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"   Okay?  %"	;% indicates end of table

;----------------------------------------------------------------------
;DTHoras
;	******************
;	*   Horas	 *
;	****************** 
;----------------------------------------------------------------------
DTHoras
	movlw	HIGH(DTHoras)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"   Horas%"	;% indicates end of table

;----------------------------------------------------------------------
;DTMenu_ON
;	******************
;	*   Menu  -->ON  *
;	****************** 
;----------------------------------------------------------------------
DTMenu_ON
	movlw	HIGH(DTMenu_ON)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"   Menu  -->ON  %"	;% indicates end of table

;----------------------------------------------------------------------
;DT_MenuON
;	******************
;	*-->Menu     ON  r*
;	****************** 
;----------------------------------------------------------------------
DT_MenuON
	movlw	HIGH(DT_MenuON)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"-->Menu     ON  %"	;% indicates end of table

;**********************************************************************
DisplayTables3	code	0x1b00		;necessary to prevent tables from crossing page boundaries (ie: 19 to 1a)
;**********************************************************************
	goto	$		;This is a trap to prevent runaway code execution
;----------------------------------------------------------------------
;DTMenu_OFF
;	******************
;	*   Menu  -->OFF *
;	****************** 
;----------------------------------------------------------------------
DTMenu_OFF
	movlw	HIGH(DTMenu_OFF)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"   Menu  -->OFF %"	;% indicates end of table


;---below message tables are in DisplayTables2 area---
;		12 "Okay? "
;		13 "Horas"
;		14 "   Menu  -->ON  "
;		15 "-->Menu     ON  "
;		16 "   Menu  -->OFF "
;		17 "-->Menu     OFF "



;----------------------------------------------------------------------
;DT_MenuOFF
;	******************
;	*-->Menu     OFF *
;	****************** 
;----------------------------------------------------------------------
DT_MenuOFF
	movlw	HIGH(DT_MenuOFF)	;***pre-load PCLATH***
	movwf	PCLATH
	movf	Index2, W
	addwf	PCL, F 
	dt	"-->Menu     OFF %"	;% indicates end of table



;***********************
;END OF PROGRAM
;***********************
	pagesel	Start
	goto	Start

	end     


; -------------------------------------------------------------------------
                           ; Indica Fin de codigo
; -------------------------------------------------------------------------
