#include <16F877A.h>
#device adc=8
#use delay(clock=10000000)
#use rs232(BAUD=9600,XMIT=PIN_C6,RCV=PIN_C7,ERRORS)
#fuses WDT,HS, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#define ADDRESS 0 // set this to the address of the floor (0 or 1 in simmons)
void main() {
  int i=1,a,b,d,e;
  char r,a0,c0,c1,c2;
  setup_adc_ports(NO_ANALOGS);
  setup_adc(ADC_OFF);
  setup_psp(PSP_DISABLED);
  setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
  setup_timer_1(T1_DISABLED);
  setup_timer_2(T2_DISABLED,0,1);
  setup_comparator(NC_NC_NC_NC);
  setup_vref(FALSE);
  setup_wdt(WDT_2304MS);
  r=getc();
  output_a(0);
  output_b(0);
  output_d(0);
  output_e(0);
  while(1) {
    r=getc();
    if(r==0xFE) {
      restart_wdt();
      a0=getc();
      c2=getc();
      c1=getc();
      c0=getc();
      if(a0==ADDRESS) {
        b=(c0&0b00000011)|((c0&0b00111100)<<2);
        output_b(b);
        d=(c1<<2);
        output_d(d);
        a=(c2&0b00001111)|((c2&0b00010000)<<1);
        output_a(a);
        e=((c2&0b00100000)>>5);
        output_e(e);
      }
    }
  }
}
