// F84 Miniature Realtime Controller V1.0
// PIC16F84 LP Xtal 32768Hz runs rtc.c
// The RTC.C was compiled by PCW PIC C Compiler V2.266
// April 10,1999
// Copyright(C) 1999 Wichit Sirichote

// May 5, 2004 fixed bug on minute converting!

// July 23, 2004 add reset port B to high after pressed S1


#include <RTC.H>
#fuses LP,NOPROTECT,NOWDT // must include this line !!


// set clock to 19:00 when press set time button once
// change below for your convenient

//#define setHour   19
//#define setMin    0

// May 16 2000 modify set hour and set min read from eeprom space
// set clock to set hour and set min resided in eeprom
// address 213E set hour
// address 213F set min
// access two bytes by using built-in function read_eeprom(62) and read_eeprom(63)

// Scheduler format in eeprom
/*

address          data
2100     19 00 04 19 01 00 FF FF   as seen in Nigel's data memory
2108     FF FF FF FF FF FF FF FF   window
...........
meaning

2100     19  ; set hour
2101     00  ; set min
2102     04  ; output byte to be sent to port_B, '1' turn on, '0' off
....
FF byte is end of stored PGM


output byte use '1' means turn the device on

         RB7  RB6  RB5  RB4  RB3  RB2  RB1  RB0

OUTPUT    NC   5    4    3    2    1    NC   NC

NOTE! output byte seen in Nigel's EEPROM window must be HEXadecimal, e.g.
Suppose at 20:00 turn output 1 and 2 on, the output byte must be

          0    0    0    0    1    1    0    0   =  0C


*/

// rename i/o devices

#define LED  PIN_B0
#define KEY  PIN_A2

// variables declaration
char sec,min,hour, day, flag1,rate,temp,i,A,B;

// Bit assignment of flag1
// mask byte   effect
// 0x20        installation test on/off(0)
// 0x40        compare time enable bit(1)
// 0x10        blink disable (1)
// 0x01        button pressed (1)

time() // update clock every 1 second
{
   sec++;
   if ( sec >= 60)
      {
       sec = 0;
       min++;
          if ( min >= 60)
             {
             min = 0;
             hour++;
               if ( hour >= 24)
                  {
                  hour = 0;
                  day++;
                    if ( day >= 8)
                       day = 1;
                  }
                  }
             }
     
} 

// convert() function provides direct entering decimal value of preset
// time in 64 bytes eeprom data memory
// say, 19 00 04 ; when reach 19:00 turns RB2 on
// as seen by Nigel's PigPro16 eeprom window, 19 means 19h or 25d
// convert will substract 25 with 6 giving 19.
// similarly but substract with 12 if > 19, e.g., 20h or 32d convert
// function will give 32d - 12d = 20.

convert()  // entry : index pointer i
           // exit: A,B
{
   A = read_eeprom(i);
   if ((A > 0x09) && (A < 0x20))
      A = A - 6;              // convert 10h-19h to 10d-19d
      if ( A> 0x19 )
         A = A - 12;          // convert 20h-23h to 20d-23d

    B = read_eeprom(i+1);
        if ((B > 0x09) && (B < 0x20))
            B = B - 6;
        if ((B >= 0x20) && (B < 0x30))
            B= B - 12;
	     if ((B >= 0x30) && (B < 0x40))
            B= B - 18;
		  if ((B >= 0x40) && (B < 0x50))
            B= B - 24;
		  if ((B >= 0x50) && (B < 0x60))
            B= B - 30;
}


scan_PGM()  // scan program saved in user eeprom
{

   if((flag1 & 0x40) != 0) // allow entering only after S1 has been pressed
    {
   i = 0;
   while( read_eeprom(i) != 0xff) // if not EOF (0xff) do
    {
         convert();
      if (hour == A && min == B)
          port_B = ~read_eeprom(i+2)|0x03; // fire output
          i = i+3; // next address
    }
   }

}


setTime()
{
   if ((flag1 & 0x01) != 0) //input(KEY)==0)
      {

      i = 62;     // put index for reading eeprom data from 62 and 63
      convert(); // converts hex to dec
      hour = A; // read time to set saved in eeprom 213EH for set hour
      min = B; // and address 213FH for set minute
      sec = 0;

// make port B all bit high
      port_B = 0xFF;

      flag1 |= 0x40;  // enable compare time
      flag1 &= ~0x20;  // reenable test on off
      flag1 &= ~0x01; // clear key press bit
      rate = 3;
      }
}

blink() // turn LED on 100 ms
 {
   output_low(LED);
   delay_ms(100);
   output_high(LED);
 }


fireLED()
{
   temp++;
	if ( temp == rate)
      {
      blink();
      temp = 0;
      }
}

chkKEY()
{
   if(input(KEY)==0)
      {flag1 |= 0x01; // set bit 0 telling key been pressed
       flag1 |= 0x10; // disable firing LED
//       output_high(LAMP); // turn on lamp when press button
      }
}



main()
{

   setup_counters(RTCC_INTERNAL,RTCC_DIV_32); // [32768/4]/32 = 256Hz

/* 256Hz clock will be counted by Timer, when it rolls from FF to 00
   T0IF will then be set, in other word, one second has elapsed */

   output_low(LED);
   flag1 = 0;
   rate = 1;
   temp = 0;
   tmr0 = 0;
   day = 1;// Set to the required day.(Mon = 1 ,Sun =7)
   hour = 18;
   min = 0;
   sec = 0;


   while(1)
   {
       while(!bit_test(intcon,2))
        chkKEY(); // check key S1 while T0IF = 0
      bit_clear(intcon,2);

   // the following tasks executed every 1 second
        output_high(PIN_B7);  // use RB7 for measuring tasks execution
        time();
        if (day <= 5) scan_PGM();
        fireLED();
        setTime();
        output_low(PIN_B7);

   }
}



