#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000)
#use RS232(baud=9600,xmit=PIN_C6,rcv=PIN_C7, FORCE_SW)
#include <lcd.c>

int res;

void wake_ECU(void)
{
   
   output_high (PIN_C6);
   Delay_ms(200);
   output_low (PIN_C6);
   Delay_ms(200);

   output_high (PIN_C6);
   Delay_ms(200);
   output_low (PIN_C6);
   Delay_ms(200);
   output_low (PIN_C6);
   Delay_ms(200);
   output_low (PIN_C6);
   Delay_ms(200);
   output_low (PIN_C6);
   Delay_ms(200);
   output_low (PIN_C6);
   Delay_ms(200);
   output_low (PIN_C6);
   Delay_ms(200);

   output_low (PIN_C6);
   Delay_ms(200);
   output_high (PIN_C6);
   Delay_ms(200);

  /* output_high (PIN_C6);
   Delay_ms(200);
   output_low (PIN_C6);
   Delay_ms(200);

   output_high (PIN_C6);
   Delay_ms(200);
   output_low (PIN_C6);
   Delay_ms(200);
   output_high (PIN_C6);
   Delay_ms(200);
   output_low (PIN_C6);
   Delay_ms(200);
   output_high (PIN_C6);
   Delay_ms(200);
   output_low (PIN_C6);
   Delay_ms(200);
   output_high (PIN_C6);
   Delay_ms(200);

   output_low (PIN_C6);
   Delay_ms(200);
   output_high (PIN_C6);
   Delay_ms(200);*/

}

kw1281_ecu_sync ()
{
   //int ecu[3] = {0x55,0x01,0x8A}, i;
   int ecu[3], i;

   for (i=0; i<=2; i++){
         ecu[i] = getc();
     }

   if ((ecu[0] == 0x55) && (ecu[1] == 0x01) && (ecu[2] == 0x8A))
     {
      putc (0xFF-ecu[2]);
      getc();
      printf(lcd_putc,"\fConnection.. OK!");
      return 1;
     }
   else
      printf(lcd_putc,"\fNO Connection...");
      return 0;
     }

void main() {

   lcd_init();
   wake_ECU();
   res = kw1281_ecu_sync();

while(TRUE) {
   //putc(0x55);
   //res=getc();
   //Delay_ms(200);
   printf(lcd_putc,"\nres= %1.2U", res);
   }
}