@ __config _INTRC_OSC_NOCLKOUT & _MCLRE_ON & _BODEN_OFF & _LVP_OFF & _WDT_OFF & _PWRTE_OFF
;*******************************************************************************
Define OSC 4
CMCON = 7 

;*******************************************************************************
Precarga	Var Word													
Symbol LED1 = PORTB.0 
Symbol LED2 = PORTB.1
Symbol SW1 =  PORTA.2 
Symbol SW2 =  PORTA.3 


_1000_ms CON 1*10    ; Conteos para 1seg con interrupciones cada 100ms
                ; T2s : 1*(1000/100) = 10 
 
_2000_ms CON 2*10    ; Conteos para 2seg con interrupciones cada 100ms
                ; T2s : 2*(1000/100) = 20
                
Pulsos VAR BYTE  
ContadorPulsos VAR BYTE
EstadoTarea  VAR BYTE

ESPERAR_PRESIONAR_SW1 CON     0
ESPERAR_SOLTAR_SW1 CON        1
CAPTURAR_PULSOS_PARA_TON CON  2
ESPERAR_TON  CON              3
ESPERAR_PRESIONAR_SW2 CON     4
ESPERAR_SOLTAR_SW2 CON        5
CAPTURAR_PULSOS_PARA_TOFF CON 6
ESPERAR_TOFF  CON             7
 
;*******************************************************************************


TRISA=%11111111                    
TRISB=%00000000
                  
PORTB=0
LED1 = 0
LED2 = 0					
	
	T1CON = %00111101			
	Precarga = 53036	       'Desborde cada 100mS	
	TMR1H = Precarga.HighByte  'Precargar el byte alto	
	TMR1L = Precarga.LowByte   'Precargar el byte bajo	
	PIE1.0 = 1				   'Habilita la interrupcin por desborde 		
	INTCON = %11000000         'Habilita interrupciones globales y perifricas
	
	
	On Interrupt GoTo Interrupcion

Main:
    EstadoTarea = ESPERAR_PRESIONAR_SW1 'Inicializo EstadoTarea
    LOW LED1' apago el led inicialmente
Lazo:
    SELECT CASE EstadoTarea
        CASE ESPERAR_PRESIONAR_SW1 
            IF SW1=0  THEN 
                EstadoTarea = CAPTURAR_PULSOS_PARA_TON ' voy al siguiente caso
            Endif
        CASE CAPTURAR_PULSOS_PARA_TON
            ContadorPulsos = Pulsos
            EstadoTarea = ESPERAR_TON 
        CASE ESPERAR_TON
            IF ((Pulsos- ContadorPulsos) > _2000_ms) THEN
                HIGH LED1   
                EstadoTarea = ESPERAR_PRESIONAR_SW2 
                Endif
        CASE ESPERAR_PRESIONAR_SW2
            IF SW2=0  THEN 
                LOW LED1' apago el led 
                EstadoTarea = 0' regreso al primer estado                 
            Endif
    End Select

GOTO Lazo:



Interrupcion:
	Disable
	
	If PIR1.0 = 1 Then			; Ocurri un desborde del Timer 1
	            'Call _set_timer1(0xfc17)  'Recarga registros TMR1 para desborde 1mSeg. y borra PIR1.TMR1IF
                T1CON.0 = 0  'Para el contador del TMR1
                TMR1H = Precarga.HighByte 'Recarga el contador byte alto
                TMR1L = Precarga.LowByte  'Recarga el contador byte bajo
                T1CON.0 = 1  'Activa el contador del TMR1
                PIR1.0 = 0	 'Flash dosborde a cero

 Pulsos = Pulsos + 1
    endif
   
    INTCON.2 = 0    ' Reset timer interrupt flag            
	
	Resume
	Enable


	End