#use "default.h"

#if (BOARD_TYPE!=LittleG)
#fatal "This program only runs on the BL16xx/LittleG."
#endif

/*
	GENRTC.C

	Sample program to read and write the RTC (Real Time Clock) of the
	BL16xx/LittleG.

	The RTC is read, then changed, then set back to the original time
	and date. Finally, the RTC is read and displayed every second.
*/

void showtime(struct tm thistime);

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

   tm_rd(&dtime);          // read and display 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.
   dtime.tm_year = 96;              // set year to 96

   tm_wr(&dtime);                  // write the new time
   tm_rd(&dtime);                  // read and display the new time
   showtime(dtime);                
	printf("\n\n");
	   
   
   dtime.tm_mday = saveday;
   dtime.tm_hour = savehour;
   tm_wr(&dtime);                  // restore the day and hour

   while(1)
   {
      tm_rd(&dtime);                // read and display the time every second
      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
   }
}

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);

}

