#include "main.h"


#define LED    PIN_B2  
#define TRAFO1 PIN_B1  
#define TRAFO2 PIN_A2  
#define ALERT  PIN_B5  
#define OPTRON PIN_A0  


void main()
{
   output_high(ALERT);
   
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
//   setup_wdt(WDT_1152MS|WDT_DIV_16);
   setup_wdt(WDT_288MS);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_oscillator(OSC_1MHZ|OSC_TIMER1);

   restart_wdt();
   
   while(true)
   {
      int8 n;
      
      for(n=0;n<165;n++) // 20ms
      {
         output_low(TRAFO1);
         output_high(TRAFO2);
         delay_us(28);           // 121us
         output_high(TRAFO1);
         output_low(TRAFO2);
      }
      restart_wdt();
      output_high(TRAFO1);
      output_high(TRAFO2);
      output_toggle(LED);
      delay_ms(100);
      restart_wdt();
      
      int16 i;

      for(i=0;i<1300;i++)
      {
         delay_ms(1);
         restart_wdt();
         if(input(OPTRON)==0) 
         {
            output_low(ALERT);
            while(TRUE) restart_wdt();
         }
      }      
   }
}