//============================================================================================
//      tc_clock.c            ver 2.2   (7/xx/08)
//
//      copyright (c) 2001-2008 Vaux Electronics, Inc.
//============================================================================================


//============================================================================================
//
//  Public routine xxx
//
//
//  Timer-2 is an 8-ms interrupt handler that provides time/date counting,
//  and seconds-time-out for motor-turn-off feature.
//
//      In main prog:
//
//          ...
//          #PRIORITY     TIMER1, TIMER2, RDA, TBE              // give timer1 high priority
//          ...
//          #INCLUDE      "tc_clock.c"                          // routines for time and date counting,
//                                                              //   and seconds-time-out for motor control (& timer-2 intr)
//          ...
//          void main()
//          {
//            ...
//            // ----- timer-2 interrupt used for time/date and motor timeout delay counter:
//            setup_timer_2 (T2_DIV_BY_16, T2PERIOD, T2POSTSCALE);  // T2PERIOD and T2POSTSCALE defined in tc_clock.c
//            set_timer2 (0);
//            enable_interrupts (INT_TIMER2);
//            enable_interrupts (GLOBAL);
//            ...
//            T2_delay_seconds = 5;
//            while(T2_delay_seconds)
//              // wait;
//            // ok, 5 seconds elapsed
//          }
//
//============================================================================================
//
//      to do:
//          - if RTC chip is available, init vars, get current date at day-wrap...
//
//      tc_clock.c            ver 2.2   (7/xx/08)
//          - tweaks
//
//      tc_clock.c            ver 2.1   (7/xx/07)
//          - created from tc_port_io.c (pulled out timer-2 intr)
//
//============================================================================================


//  ----- prototypes:
void T2_isr(void);                                  // timer-2 intr is called every 8-ms


// ----- defines:


// ----- global variables (if static, compiler inits to 0):

int1   t2_second_flag = 0;                          // set every second, for main prog to test/reset
int1   t2_minute_flag = 0;                          // set every minute, for main prog to test/reset
int1   t2_15minute_flag = 0;                        // set every 15 minutes, for main prog to test/reset
int1   t2_hour_flag = 0;                            // set every hour, for main prog to test/reset
int8   t2_delay_seconds = 0;                        // main can load for delay (secs), and wait until 0



//--------------------------------------------------------------------------------------------
//  void T2_isr(void)                               timer-2 intr is called every 8-ms
//
//  Timer-2 is an 8-ms interrupt handler that provides time/date counting,
//  and seconds-time-out for motor-turn-off feature
//
//    For 20MHz clock, this int routine is called every time timer 2 overflows (250->0),
//    which is every 8.0 ms (125 times/sec):
//
//                         Clock MHz                         20000000
//     ------------------------------------------   =   ------------------  =  125 times/sec
//     (4)(T2_divider)(T2_period+1)(T2_postscale)        (4)(16)(250)(10)


#define     T2PERIOD        249                      // 1 less than desired num
#define     T2POSTSCALE      10                      //

#define     T2_INTS_PER_SECOND          125          // T2 will count 125 ints/sec (@8 ms) for 1-second rate

static int8 T2_int_count = T2_INTS_PER_SECOND;       // number of T2 interrupts left before a second has elapsed


#int_TIMER2
void T2_isr(void)                                    // this occurs every 8-ms
{

  // ========== start of timer-1 isr ==========

  #IFDEF T2_INT_MON
    output_high (T2_INT_MON);                         // TIMER-2 isr monitor pin
  #ENDIF



  // ========== seconds/minutes/hours/day/month/year counter rollover actions ==========

  // ----- this occurs every 1 second, so reload T2_int_count:
  if( --t2_int_count == 0 )
  {
    t2_int_count = T2_INTS_PER_SECOND;

    t2_second_flag = 1;                                // main prog can test/reset this (for every-second func)


    // ========== do something every 1-second:


    // ----- main prog can load T2_delay_seconds for delay (secs), and wait until 0:
    if( T2_delay_seconds )
      --T2_delay_seconds;                             // decrement seconds-delay counter if not 0


    // (add other 1-sec-rate stuff here)


    // ----- toggle led every second:
    //  if( (int1)(seconds))
    //    output_high(STATUS_LED);
    //  else
    //    output_low(STATUS_LED);


    //  ----- this occurs every 1 minute, so reload second counter:
    if( ++second == 60 )
    {
      second = 0;

      t2_minute_flag = 1;                                // main prog can test/reset this (for every-minute func)

      // ========== do something every 1-minute:


      // (add other 1-min-rate stuff here)

--------
       //  ----- this occurs every 15 minutes, so reload second counter:
       if( ++15minutes == 15 )
       {
         second = 0;
   
         t2_minute_flag = 1;                                // main prog can test/reset this (for every-15minute func)
   
         // ========== do something every 15-minute:
   
   
         // (add other 1-min-rate stuff here)
   
   
-----
         //  ----- this occurs every 1 hour, so reload minute counter:
         if( ++minute == 60 )
         {
           minute = 0;
   
           t2_hour_flag = 1;                                  // main prog can test/reset this (for every-hour func)
   
           // ========== do something every 1-hour:
   
   
           // (add other 1-hour-rate stuff here)
   
   
           //  ----- this occurs every 1 day, so reload hour counter:
           if( ++hour == 24 )
           {
             hour = 0;
   
   
           // ========== do something every 1-day:


        // (add other 1-day-rate stuff here)


        //  ----- if RTC chip is available, get current date:
        // ??????????????????????????????


        }
      }
    }
  }



  // ========== non-rollover actions ==========

  // TEST:   every 2 minutes, turn off all motors:
  if( minute == 2 )
  {
    MOT_port_image &= ~ALL_MOTORS_ON;
    PORT_A = MOT_port_image;

  }


  // ========== end of timer-2 isr ==========

  #IFDEF T2_INT_MON
    output_low (T2_INT_MON);                          // TIMER-2 isr monitor pin
  #ENDIF

}
