/*
	timer.c
	
   Sample program to read and to write to the RTC (Real Time Clock)
   on the Z104 or ZISA boards.

   The program reads and displays the RTC, then updates and displays
   the RTC, then returns the RTC to its original values and displays
   the date and time every second from then on.
*/

#use "default.h"

#if (BOARD_TYPE!=Z104ISA)
#fatal "This program only runs on the Z104 or ZISA boards."
#endif

void showtime(struct tm thistime);

main()
{
   struct tm dtime;
   int saveday;
   int savehour;
   int lastsec;

   tmc_rd(&dtime);          // read the time
   showtime(dtime);
	printf("\n\n");   

   saveday = dtime.tm_mday;         // save the day
   savehour = dtime.tm_hour;        // save the hour

   dtime.tm_mday = 11;              // set day to 11
   dtime.tm_hour = 4;               // set hour to 4 a.m.

   tmc_wr(&dtime);                  // write the new time
   tmc_rd(&dtime);                  // read the new time
   showtime(dtime);                 // show new time
	printf("\n\n");   

   
   dtime.tm_mday = saveday;
   dtime.tm_hour = savehour;
   tmc_wr(&dtime);                  // restore the day and hour

   while(1)
   {
      tmc_rd(&dtime);               // read time forever
      if(dtime.tm_sec != lastsec)   // show when seconds is changed
      {
         lastsec = dtime.tm_sec;
         showtime(dtime);
      }
      runwatch();	// can also execute the functions through the watch window
      hitwd();
   }
}

void showtime(struct tm thistime)
{   
      printf("Date/Time is %2d-%2d-%2d-%1d, ", thistime.tm_mon, 
                     thistime.tm_mday, thistime.tm_year, thistime.tm_wday);
      printf("  %2d:%2d:%2d\r", thistime.tm_hour, thistime.tm_min,
                     thistime.tm_sec);
}