// 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

#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

// 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,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;
             }
      }
 }


// 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 > 0x19)
            B= B - 12;
}


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)
      {
      hour = setHour;
      min = setMin;
      sec = 0;
      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;
   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();
        scan_PGM();
        fireLED();
        setTime();
        output_low(PIN_B7);

   }
}





                                                