/* Standard Include for 16F877 Chip */
#include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT

/* Delay for 4 mhz crystal */
#use delay (clock=4000000)

/* Use standard IO for all */
#use standard_io(D)

main()
{
   int d2_value = 0;
   int a1_value = 0;

/* ADC Setup */
   setup_adc(  ADC_CLOCK_INTERNAL  ); /* in 16f877.h */
   setup_adc_ports( ALL_ANALOG ); /* in 16f877.h */
   set_adc_channel(1); /* PIN_A1 */
/*
while ( input(PIN_B0) ) {
	delay_ms( 5000 );
	value = read_adc();
	#printf("A/D value = %2x\n\r", value);
}
*/


/*
   while (1) {
      output_high(PIN_D1);
      delay_ms(500);
      output_low(PIN_D1);
      delay_ms(500);
   }
*/

   while (1 == 1)
   {
      d2_value = input(PIN_D2);
      
      if (d2_value)
      {
         output_high(PIN_D1);
         delay_ms(250);
         output_low(PIN_D1);
         delay_ms(250);
      }
      else
      {
         a1_value = read_adc();
         if (a1_value >= 64)
         {
            output_high(PIN_D0);
         }
         else
         {
            output_low(PIN_D0);
         }
         output_high(PIN_D1);
      }
   }
}

