home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 September / pcwk_09_96.iso / demo / wgelectr / pk51demo / files.2 / EXAMPLES / MEASURE / MCOMMAND.C < prev    next >
C/C++ Source or Header  |  1995-06-08  |  6KB  |  122 lines

  1. /*------------------------------------------------------------------------------
  2. MCOMMAND.C:  Time Set Commands for the Remote Measurement Recorder
  3.  
  4. Copyright 1995 KEIL Software, Inc.
  5. ------------------------------------------------------------------------------*/
  6.  
  7. #include <stdio.h>                       /* standard ANSI I/O .h-file         */
  8. #include <reg515.h>                      /* special function register 80515   */
  9.  
  10. struct clock {                           /* structure of the clock record     */
  11.   unsigned char    hour;                 /* hour                              */
  12.   unsigned char     min;                 /* minute                            */
  13.   unsigned char     sec;                 /* second                            */
  14.   unsigned int     msec;                 /* milli second                      */
  15. };
  16.  
  17. struct mrec  {                           /* structure for measurement records */
  18.   struct   clock   time;                 /* time of measurement               */
  19.   unsigned char   port4;                 /* state of port 4                   */
  20.   unsigned char   port5;                 /* state of port 5                   */
  21.   unsigned char  analog [4];             /* voltage on analog Pins AN0 .. AN3 */
  22. };
  23.  
  24. struct interval  {                       /* structure for interval record     */
  25.   unsigned char     min;                 /* minute                            */
  26.   unsigned char     sec;                 /* second                            */
  27.   unsigned int     msec;                 /* millisecond                       */
  28. };
  29.  
  30.  
  31. extern code char ERROR [];               /* ERROR message string              */
  32. extern struct mrec current;              /* current measurements              */
  33. extern struct interval setinterval;      /* interval setting values           */
  34. extern struct interval interval;         /* interval counter                  */
  35.  
  36.  
  37.  
  38. /******************************************************************************/
  39. /*                           Display measurements                             */
  40. /******************************************************************************/
  41. measure_display (struct mrec display)  {    
  42.   unsigned char i;                       /* index count for AN0 - AN3         */
  43.  
  44.   printf ("\rTime: %2bd:%02bd:%02bd.%03d  P4:%b02X P5:%b02X ",
  45.            display.time.hour,            /* display hour                      */
  46.            display.time.min,             /* display minute                    */
  47.            display.time.sec,             /* display second                    */
  48.            display.time.msec,            /* display millisecond               */
  49.            display.port4,                /* display port 4 value              */
  50.            display.port5);               /* display port 5 value              */
  51.  
  52.   for (i = 0; i != 4; i++)  {            /* display AN0 through AN3           */
  53.     printf (" AN%bd:%4.2fV", i, (float) display.analog[i] * 5.0 / 256);
  54.   }
  55. }
  56.  
  57.  
  58. /******************************************************************************/
  59. /*                           Set Current Time                                 */
  60. /******************************************************************************/
  61. void set_time (char * buffer)
  62. {
  63.   struct clock time;                     /* temporary time values             */
  64.   unsigned char args;                    /* number of arguments               */
  65.  
  66.   time.sec = 0;                          /* preset seconds...                 */
  67.   time.msec = 0;                         /* ...and milliseconds               */
  68.   args = sscanf (buffer, "%bd:%bd:%bd",  /* scan input line for               */
  69.                  &time.hour,             /* hour, minute and second           */
  70.                  &time.min,
  71.                  &time.sec);
  72.   if (time.hour > 23  ||  time.min > 59  ||    /* check for valid inputs      */
  73.       time.sec > 59   ||  args < 2       ||  args == EOF)  {
  74.     printf (ERROR, "INVALID TIME FORMAT");
  75.   } 
  76.   else  {                                /* if inputs valid then              */
  77.     EAL = 0;                             /* disable interrupts while          */
  78.     current.time = time;                 /* setting the new time              */
  79.     interval.min = 0;                    /* force new interval                */
  80.     interval.sec = 0;
  81.     interval.msec = 0;
  82.     EAL = 1;                             /* enable interrupts again           */
  83.   }
  84. }
  85.   
  86.  
  87. /******************************************************************************/
  88. /*                            Set Interval Time                               */
  89. /******************************************************************************/
  90. void set_interval (char * buffer) {
  91.   struct interval itime;                 /* temporary interval record         */
  92.   unsigned char args;                    /* number of arguments               */
  93.   float second;                          /* float sec for ss.mmm format       */
  94.  
  95.   args = sscanf (buffer, "%bd:%f",       /* scan input line for               */
  96.                  &itime.min,             /* minute, second and                */
  97.                  &second);               /* milliseconds                      */
  98.                                          /* check valid inputs                */
  99.   if (second >= 60.0  ||  args < 2  || args == EOF)  {
  100.     printf (ERROR, "INVALID INTERVAL FORMAT");
  101.   }
  102.   else  {                                /* if inputs are valid then          */
  103.     itime.sec = second;                  /* calculate second and millisecond..*/
  104.     itime.msec = (second - itime.sec) * 1000;  /* ..from float values         */
  105.     if (itime.min != 0 || itime.sec != 0 || itime.msec != 0)  {
  106.       if (itime.msec-- == 0)  {          /* correct interval time for         */
  107.         itime.msec = 999;                /* interrupt routine                 */
  108.         if (itime.sec-- == 0)  {
  109.           itime.sec = 59;
  110.           itime.min--;
  111.         }
  112.       }
  113.     }
  114.     EAL = 0;                             /* disable interrupts for copy       */
  115.     setinterval = itime;                 /* of the new setinterval time       */
  116.     interval.min = 0;                    /* force new interval                */
  117.     interval.sec = 0;
  118.     interval.msec = 0;
  119.     EAL = 1;                             /* enable interrupts again           */
  120.   }
  121. }
  122.