// WinkIntc.c

#include <16f628.h>                    // Selecciona el PIC
#fuses XT,NOWDT,NOPROTECT,PUT,BROWNOUT // Opciones de configuración
#use delay(clock=4000000)              // Velocidad del Cristal : 4 Mhz
#use standard_io(B)                    // PORTB en estandar IO digital
#use fixed_io(b_outputs=PIN_B0)	      // B0 como salida en PORTB 

byte const NInts=7;                   // Numero de interrupciones para 0.25 Segundos

                                       // VARIABLES GLOBALES
                                       
char C_Ints=0;                         // Contador de Interrupciones ocurridas
char Flag=0;                           // Flag que cambia cada NInts interrupciones
char K=0;                              // Estado anterior del Flag


#int_RTCC                              // Interrupción por desbordamiento
RTCC_isr() {                           // del TIMER0 RTCC

   if(C_Ints > NInts){                 // Si las ints ocurridas > ints para 0.25 seg.

      if(Flag==0){
         Flag=1;
      }
      else{
         Flag=0;
      }

      C_Ints=0;                        // Reinicializo Contador de Ints
   }
   ++C_Ints;
}



void main(void) {

   setup_counters(RTCC_INTERNAL,RTCC_DIV_128); // Configuración de TIMER0 : Clock Interno y Presescaler 128
   setup_timer_1(T1_DISABLED);                 // para una RTCC cada 33.3 milisegundos -> 1 Segundo = 30 RTCC
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_RTCC);                // Habilito Interrupcion RTCC
   enable_interrupts(global);                  // Habilito Interrupciones

   output_low(PIN_B0);                         // Empiezo apagando el Led 
   

   
   do{                                         // Bucle infinito

      if(Flag!=K)
      {                                        // si ha cambiado flag 
         if(Flag==0){ output_low(PIN_B0); }    // Si es 0 Apago el Led 
         if(Flag==1){ output_high(PIN_B0); }   // si es 1 Enciendo el Led
         k=Flag;                               // Guardo estado anterior de Flag 
      }

   }While(TRUE);

}
