
#include <18F8722.H>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) 

 int i;  //general purpose counter
 
 int1 clk_in;
  

void wait_for_high_to_low() 
    {
     while(input(PIN_B1)) ;       /* if it's high, wait for a low */
     delay_us(3);                 /* account for fall time */
     while(!input(PIN_B1));       /* wait for signal to go high */
    }


void main()
   {
  
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_spi2(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_timer_4(T4_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   
   clk_in = FALSE;  // default to internal clock
   
   //output_low(PIN_D4);
          
   for (i=0; i<100; i++)    //loop for a second to check for the NTP 1PPS
      {        
        if( !input(PIN_B0) )  // exit check loop if pin goes low 
          {
            clk_in = TRUE;
             break;
          } //if
        else
             delay_ms(10);
      } //for
   
   while (TRUE)      //run this program forever
        {
         
          // NTP  Clock Routine        
          if (clk_in == TRUE) // we have a clock input
            {
             output_high (PIN_D0); // external clock indicator LED
             for(i=0; i<9; i++) // 9 20ms pulse/second
                {
                 wait_for_high_to_low();
         
                 output_high(PIN_B1);  //pulse output
                 output_high(PIN_D1);  // LED output
         
                 delay_ms(20);   // 20ms pulse
                           
                 output_low(PIN_B1);
                 output_low(PIN_D1);
                 } // for
        
              // 1 40ms pulse 
              
             wait_for_high_to_low();
             
             output_high(PIN_B1);      
             output_high(PIN_D2);
     
             delay_ms(40);
     
             output_low(PIN_B0);
             output_low(PIN_D2);
             
             } // if clk_in
            
       // no NTP hooked up so we'll create our own pulses
       else
          {
           output_high(PIN_D3);  //internal clock indicator
           for(i=0; i<9; i++) // 9 20ms pulse/second
             {
               output_high(PIN_B1);  //pulse output
               output_high(PIN_D4);  // LED output
         
               delay_ms(20);   // 20ms pulse
         
               output_low(PIN_B1);
               output_low(PIN_D4);
         
                delay_ms(980);
                } //for
        
             // 1 40ms pulse        
             output_high(PIN_B1);      
             output_high(PIN_D5);
     
             delay_ms(40);
     
             output_low(PIN_B0);
             output_low(PIN_D5);
     
             delay_ms(960);
             } //else 
          
     } //while
} //main
