/*  Filename     PKIT9.C
    Description  TIMER
    Hardware     DEMIER1
    Compiler     CCS PCW C Complier V3.24
    Engineer     Anan Phukittikul
    Company      Sila Research Co.,Ltd.
*/

#include <16F676.h>
#fuses INTRC_IO,PUT,NOWDT,NOMCLR,NOBROWNOUT,NOCPD,NOPROTECT

#use delay (clock=4000000)



#define   LEDT2     PIN_C0   // LED Timer 2
#define   LEDT1     PIN_C1   // LED Timer1
#define   RELAY     PIN_C2   // RELAY
#define   SW        PIN_C3   // Switch input
#define   MP10M     PIN_C4   // Motion Sensor MP10M
#define   SOUND     PIN_C5   // Sound Detector
#define   DIP1      PIN_A2   // Timer Mode ON=Min  OFF=Sec
#define   DIP2      PIN_A3   // |--------------|
#define   DIP3      PIN_A4   // |---- MODE ----|
#define   DIP4      PIN_A5   // |--------------|

void T1 (void) {                        // TIMER 1
    int16 a,b;
    set_adc_channel (0);
    delay_us(10);
    a = read_adc ();                    //  x = 0-1023 (10-bit adc) , 0-5 volt
    a = (a * 250) / 1023;               //  convert to average time
    if(input(DIP1)==1){              //  Sec
      output_low(LEDT1);
      for(b=0;b<=a;b++){
         delay_ms(1000);
      }
      output_high(LEDT1);
    }
    if(input(DIP1)==0){              // MIN
      output_low(LEDT1);
      for(b=0;b<=a;b++){
         delay_ms(60000);
      }
      output_high(LEDT1);
    }
}

void T2 (void){                         // TIMER 2
    int16 c,d;
    set_adc_channel (1);
    delay_us(10);
    c = read_adc ();                    //  x = 0-1023 (10-bit adc) , 0-5 volt
    c = (c * 250) / 1023;               //  convert to average time
    if(input(DIP1)==1){              //  Sec
      output_low(LEDT2);
      for(d=0;d<=c;d++){
         delay_ms(1000);
      }
      output_high(LEDT2);
    }
    if(input(DIP1)==0){            //MIN
      output_low(LEDT2);
      for(d=0;d<=c;d++){
         delay_ms(60000);
      }
      output_high(LEDT2);
    }
}


//--------------MODE -----------------------//

void mode0 (void){        //   MODE 0

      output_low(RELAY);
      T1();
      while(1){
        output_high(RELAY);
      }
}

void mode1 (void){       //   MODE 1

      output_high(RELAY);
      T1();
      while(1){
         output_low(RELAY);
      }
}

void mode2 (void){        //  MODE 2

   while(1){
     output_low(RELAY);
     T1();
     output_high(RELAY);
     T2();
   }
}

void mode3 (void){     // MODE 3
   while(1){
      output_high(RELAY);
      T1();
      output_low(RELAY);
      T2();
   }
}

void mode4(void){   // MODE 4
   int1 y,z;
   delay_ms(5000);      //setup delay
   while(1){
      y = input(SW) & input(SOUND);// Check Input
      z = y & !input(MP10M);
      if(z==0){
         output_low(RELAY);
         T1();
         output_high(RELAY);
         T2();
      }
   }
}


void mode5(void){       //MODE 5
   int1 y,z;
   delay_ms(5000);      //setup delay
   while(1){
      y = input(SW) & input(SOUND);// Check Input
      z = y & !input(MP10M);
      if(z==0){
         output_high(RELAY);
         T1();
         output_low(RELAY);
         T2();
         output_high(RELAY);
      }
   }
}

void mode6 (void){     // mode 6
   int8 x,y,z;
   delay_ms(5000);     // setup delay
   while(1){
      y = input(SW) & input(SOUND);// Check Input
      z = y & !input(MP10M);
      if(z==0){
         for(x=0;x<=2;x++){
            output_low(RELAY);
            T1();
            output_high(RELAY);
            T2();
         }
      }
   }
}

void mode7 (void){     // MODE 7
      int1 x,y,z;
      x = 0;
      delay_ms(5000);      // setup delay
      while(1){
            y = input(SW) & input(SOUND);// Check Input
            z = y & !input(MP10M);
            if(z==0){
               x = !x;
               if(x==0){
                  output_high(RELAY);
 //                 output_high(LEDT1);
 //                 output_low(LEDT2);
 //                 delay_ms(2000);
                    T2();
               }
               if(x==1){
                  output_low(RELAY);
 //                 output_high(LEDT2);
 //                 output_low(LEDT1);
 //                 delay_ms(2000);
                     T1();
               }
            }
      }
}

void main (void) {
   int8 x;
   delay_ms(250);
   setup_adc_ports (sAN0);    // Analog in CH0
   setup_adc_ports (sAN1);    // Analog in CH1
   setup_adc_ports (VSS_VDD); // Set Vref  0 - 5 V
   setup_adc (ADC_CLOCK_INTERNAL);
   delay_ms(250);
   x = !input(DIP2);                //-----------------------
   x = (x << 1) | !input(DIP3);      // ---- Check MODE ------
   x = (x << 1) | !input(DIP4);      // ----------------------
   switch(x){
       case 0 : mode0();break;
       case 1 : mode1();break;
       case 2 : mode2();break;
       case 3 : mode3();break;
       case 4 : mode4();break;
       case 5 : mode5();break;
       case 6 : mode6();break;
       case 7 : mode7();break;
   }
}
