The Use of ftime() in Implementing a Periodic Process



/* Program ftime_1.c
**
** Illustrates use of ftime function to implement a periodic process.
**
** In this example, an LED is flashed num times where num is in the
** range of 1 to 10.  This may require a time in the range of 0.5 to 5.0
** seconds.  This process is repeated every 5.5 seconds.
**
** Note that a call to ftime returns the elapsed time since Jan 1, '70
** in seconds and the fractional number of millisecs.  As the .time is
** a very large number, converting this to a float and combining it with
** millitm will result in the loss of significant figures.  Thus, in the
** following I used t.time % 86400 (the number of secs in a day) and
** combined this with only the 0.1 second portion of millitm.
**
** However, this causes a problem when there is a rollover.  Thus, if
** there is a rollover (T_thresh > 86400.0), T_thresh is adjusted to
** T_thresh - 86400 and the program loops until the T_current <= T_thresh
** (rollover occurred) and then loops until T_current > = T_thresh.
**
** Flashlite V25              LED
**
** P2.0 ----------------------->|--- 330 -------- GRD
**
** copyright, Peter H. Anderson, Baltimore, MD, Sept, '00
*/

#include <stdio.h>
#include <dos.h>
#include <sys\timeb.h>

#define SEG 0xf000
#define P2_OFFSET 0xff10
#define PM2_OFFSET 0xff11
#define PMC2_OFFSET 0xff12

#define PERIOD 5.5
#define FINAL

typedef unsigned char byte;

void flash(int num_flashes);

byte far *p2, *pm2, *pmc2;

void main(void)
{
   struct timeb t;
   float t_thresh, t_current;
   int num_flashes;

#ifdef FINAL
   p2 = MK_FP(SEG,P2_OFFSET);
   pm2= MK_FP(SEG,PM2_OFFSET);
   pmc2=MK_FP(SEG,PMC2_OFFSET);

   *pmc2 =0x00;
   *pm2  =0xfe;
   *p2   =0x00;
#endif

   ftime(&t);
   t.time = t.time % 86400;
   t_thresh = (float) t.time + ((float) (t.millitm/100)) / 10.0;
   num_flashes = 1;

   while(1)
   {
      flash(num_flashes);

      t_thresh = t_thresh + PERIOD;
      printf("t_thresh = %.2f\n", t_thresh);

      if (t_thresh > 86400.0)
      {
         t_thresh = t_thresh - 86400.0;
         while(1)  /* wait for it to count up */
         {
            ftime(&t);
            t.time = t.time % 86400;
            t_current = (float) t.time + ((float) (t.millitm/100)) / 10.0;
            if (t_current <= t_thresh) /* loop until it rolls over */
            {
               break;
            }
         }
      }

      while(1)
      {
         ftime(&t);
         t.time = t.time % 86400;
         t_current = (float) t.time + ((float) t.millitm/100) / 10.0;
/*
         printf("t_current = %.2f\n", t_thresh);
         delay(500);
*/
         if (t_current > t_thresh)
         {
            printf("!!!!!\n");
            break;
         }
      }

      ++num_flashes;
      if (num_flashes > 10)
      {
         num_flashes = 1;
      }
   }  /* outter while */
}

void flash(int num_flashes)
{
   int n;
   for (n=0; n<num_flashes; n++)
   {
#ifdef FINAL
      *p2 = 0x01;
#endif
      printf(".");
      delay(250);
#ifdef FINAL
      *p2 = 0x00;
#endif
      delay(250);
   }
   printf("\n");
}