/* Standard Include for 16F877 Chip */
#include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT

/* Delay for 4 mhz crystal */
#use delay (clock=4000000)

/* Setup RS232 */
#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7)
/* No inversion from CoBox to PIC, Yes Inversion from PIC or CoBox to PC*/
/* CoBox IP Address: http://128.122.151.181/ */

/* Use standard IO for D Pins */
#use standard_io(D)

main()
{
   char send_char = 65;
   char response_char = 66;
   signed int adc_value = 0;
   int received_char = 0;
   int connected = 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 */

   //delay_ms(10000);
   while (!connected)
   {
      printf("C128.122.253.189/10181");
      putc(10);
      delay_ms(1000);
      putc(send_char);
      if (kbhit())
      {
         received_char = getc();
         if (received_char == response_char)
         {
            connected = 1;
         }
      }
   }
         
   /*
   output_low(PIN_D1);
   output_high(PIN_D2);
   */
   
   while (TRUE)
   {
      //printf("%c",send_char);
      putc(send_char);
      delay_ms(1000);

      adc_value = read_adc();
      putc(adc_value);
      //printf("%d",adc_value);
            
      if (kbhit())
      {
         received_char = getc();
         printf("%c",received_char);
      }
   }
}
