⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 measure.c

📁 keilc pic的版本 才搞到的 希望对大家有帮助
💻 C
字号:
/*****************************************************************************/
/*                                                                           */
/*      MEASURE.C:  Remote Measurement Recorder using the C166 COMPILER      */
/*                                                                           */
/*****************************************************************************/

static const char menu[] = 
   "\n"
   "+*********** REMOTE MEASUREMENT RECORDER using C166 ***********+\n"
   "| This program is a simple Measurement Recorder. It is based   |\n"
   "| on the 80C166 CPU and records the state of Port 2 and the    |\n"
   "| voltage on the four analog inputs AN0 through AN3.           |\n"
   "+ command -+ syntax -----+ function ---------------------------+\n"
   "| Read     | R [n]       | read <n> recorded measurements      |\n"
   "| Display  | D           | display current measurement values  |\n"
   "| Time     | T hh:mm:ss  | set time                            |\n"
   "| Interval | I mm:ss.ttt | set interval time                   |\n"
   "| Clear    | C           | clear measurement records           |\n"
   "| Quit     | Q           | quit measurement recording          |\n"
   "| Start    | S           | start measurement recording         |\n"
   "+----------+-------------+-------------------------------------+\n";

#include <stdio.h>                       /* standard I/O .h-file              */
#include <stdlib.h>                      /* standard library .h-file          */
#include <reg167.h>                      /* special function register 80C167  */
#include <ctype.h>                       /* character functions               */
#include "measure.h"                     /* global project definition file    */


struct interval setinterval;                /* interval setting values        */
struct interval interval;                   /* interval counter               */

static bit measurement_interval;            /* measurement interval over      */
static bit mdisplay;                        /* measurement display requested  */
static bit startflag;                       /* start measurement recording    */

struct mrec current;                        /* current measurements           */

#define RAM    8192                         /* size of DATA RAM is 8 KBytes   */
#define SCNT  (RAM / sizeof (current))      /* number of records in DATA RAM  */

static struct mrec save_record [SCNT];      /* buffer for measurements        */
static unsigned int sindex;                 /* save index                     */
static unsigned int savefirst;              /* save first index               */

const char ERROR [] = "\n*** ERROR: %s\n";  /* ERROR message string in code   */

#define PERIOD     -2500                    /* 1 msec interrupt period        */
#define WRONGINDEX 0xffff                   /* error signal for wrong index   */


/* The following function is called from the interrupt service routine. */
/******************************************************************************/
/*               Save current measurements in save_record                     */
/******************************************************************************/
static void save_measurements (void) {
  save_record[sindex++] = current;             /* copy current measurements   */
  if (sindex == SCNT) sindex = 0;              /* check bounds of sindex      */
  if (sindex == savefirst)  {                  /* check circular buffer limits*/
    if (++savefirst == SCNT)  savefirst = 0;   /* check bounds of savefirst   */
  }
}


/******************************************************************************/
/*                Timer 0 interrupt service function                          */
/*                executes each 1 msec @ 40 MHz Crystal Clock                  */
/******************************************************************************/
static void timer0(void) interrupt 0x20 using INTREGS {
 /* Int Vector at 0080H, other Reg Bank */

  unsigned char i;

  if (measurement_interval)  {                 /* measurement done ?          */
    save_measurements ();                      /* yes -> save measurements    */
    measurement_interval = 0;                  /* Save measurements done      */
  }

                                               /* check if interval is over   */
  if (interval.min  == 0 && 
      interval.sec  == 0 && 
      interval.msec == 0     )  {
    interval = setinterval;                    /* set interval time again     */
    measurement_interval = startflag;          /* determine measurement flag  */
  }
  else  {                                      /* update interval time        */
    if (interval.msec-- == 0)  {               /* calculate millisecond       */
      interval.msec = 999;
      if (interval.sec-- == 0)  {              /* calculate second            */
        interval.sec = 59;
        interval.min--;                        /* calculate minute            */
      }
    }
  }
                                               /* update current time         */
  if (++current.time.msec == 1000)  {        /* update millisecond cnt      */
    current.time.msec = 0;

    if (++current.time.sec == 60)  {         /* update second counter       */
      current.time.sec = 0;

      if (++current.time.min == 60)  {       /* update minute counter       */
        current.time.min = 0;

        if (++current.time.hour == 24)  {    /* update hour counter         */
          current.time.hour = 0;
        }
      }
    }
  }	/* end of if( ++current.time.msec... */

  if (measurement_interval || mdisplay)  {   /* process measurement         */
    current.port2 = P2;                      /* read port 2                 */
    for (i = 0; i != 4; i++)  {              /* loop for 4 A/D inputs       */
      ADCON = i;                             /* select A/D input,single conv.*/
      ADST = 1;                              /* start conversion            */
      while (ADBSY) { ; }                    /* wait for A/D result         */
      current.analog[i] = ADDAT & 0x03FF;    /* result of A/D process       */ 
    }
    mdisplay = 0;                            /* mdisplay = 0 for ready sig. */
  }
}


/******************************************************************************/
/*                       Calculate first Read Index                           */
/******************************************************************************/
static unsigned int read_index (char *buffer) {
  int index = 0;
  int args;

  if (setinterval.min == 0     &&              /* check if setinterval is     */
      setinterval.sec == 0     &&              /* below 1 second and          */
      setinterval.msec < 999   &&              /* measurements are collected  */
      startflag                   )  {         
    printf (ERROR, "QUIT MEASUREMENTS BEFORE READ");
    return (WRONGINDEX);                       /* no display on the fly if    */
  }                                            /* interval time < 1 second    */
  args = sscanf (buffer, "%d", &index);        /* scan input for read count   */
  if (args == 0  ||  index == 0  || args == EOF)  index = SCNT-1;
  index = sindex - index;                      /* calculate first read index  */
  if (index < 0) index += SCNT;                /* from read count             */
  return (index);
}
  

/******************************************************************************/
/*                         Clear Measurement Records                          */
/******************************************************************************/
static void clear_records (void) {
  unsigned int idx;                            /* index for loop              */

  startflag = 0;                               /* stop measurement collecting */
  sindex = savefirst = 0;                      /* reset circular buffer index */
  for (idx = 0; idx != SCNT; idx++)  {         /* mark all records unused     */
    save_record[idx].time.hour = 0xff;         /* unused flag: hour = 0xff    */
  }     
}


/******************************************************************************/
/***************************      MAIN PROGRAM      ***************************/
/******************************************************************************/
void main ( void )  {                          /* main entry for program      */
  static char near cmdbuf [15];                /* command input buffer        */
  unsigned char i;                             /* index for command buffer    */
  unsigned int idx;                            /* index for circular buffer   */

#ifndef MCB167          /* no init of serial interface on Monitor target */
  /* initialize the serial interface */
  P3  |= 0x0400;        /* SET PORT 3.10 OUTPUT LATCH (TXD)              */
  DP3 |= 0x0400;        /* SET PORT 3.10 DIRECTION CONTROL (TXD OUTPUT)  */
  DP3 &= 0xF7FF;        /* RESET PORT 3.11 DIRECTION CONTROL (RXD INPUT) */
  S0TIC = 0x80;         /* SET TRANSMIT INTERRUPT FLAG                   */
  S0RIC = 0x00;         /* DELETE RECEIVE INTERRUPT FLAG                 */
  S0BG  = 0x40;         /* SET BAUDRATE TO 9600 BAUD                     */
  S0CON = 0x8011;       /* SET SERIAL MODE                               */
#endif

  /* setup the timer 0 interrupt */
  T0REL = PERIOD;	    /* set reload value */
  T0    = PERIOD;
  T0IC  = 0x44;         /* set T0IE and ILVL = 1 */
  IEN   = 1;            /* set global interrupt enable flag */
  T01CON = 0x40;        /* start timer 0 */


  clear_records ();                            /* initialize circular buffer  */
  printf ( menu );                             /* display command menu        */
  while (1)  {                                 /* loop forever                */
    printf ("\nCommand: ");                    
    getline (&cmdbuf[0], sizeof (cmdbuf));     /* input command line          */

    for (i = 0; cmdbuf[i] != 0; i++)  {        /* convert to upper characters */
      cmdbuf[i] = toupper(cmdbuf[i]);
    }

    for (i = 0; cmdbuf[i] == ' '; i++) { ; }   /* skip blanks                 */

    switch (cmdbuf[i])  {                      /* proceed to command function */

      case 'R':                                /* Read circular Buffer        */
        if ((idx = read_index (&cmdbuf[i+1])) == WRONGINDEX)  break;
        while (idx != sindex)  {               /* check end of table          */
          if (S0RIR)  {                        /* check serial interface      */
            if (_getkey() == 0x1B) break;      /* for escape character        */
          }
          if (save_record[idx].time.hour != 0xff)  {
            measure_display (save_record[idx]);      /* display record        */
            printf ("\n");
          }
          if (++idx == SCNT) idx = 0;          /* next circular buffer entry  */
        }
        break;

      case 'T':                                /* Enter Current Time          */
        set_time (&cmdbuf[i+1]);
        break;

      case 'I':                                /* Enter Interval Time         */
        set_interval (&cmdbuf[i+1]);
        break;

      case 'D':                                /* Display Command             */
        printf ("\nDisplay current Measurements: (ESC to abort)\n");
        do  {
          while (! S0RIR)  {                   /* check serial interface      */
            mdisplay = 1;                      /* request measurement         */
            while (mdisplay) { ; }             /* wait for measurement        */
            measure_display (current);         /* display values              */
          }
        } while (_getkey () != 0x1b);          /* escape terminates command   */
        printf ("\n\n");
        break;

      case 'S':                                /* Start Command               */
        printf ("\nStart Measurement Recording\n");
        startflag = 1;
        break;

      case 'Q':                                /* Quit Command                */
        printf ("\nQuit Measurement Recording\n");
        startflag = 0;
        break;

      case 'C':                                /* Clear Command               */
        printf ("\nClear Measurement Records\n");
        clear_records ();
        break;

      default:                                 /* Error Handling              */
        printf (ERROR, "UNKNOWN COMMAND");
        printf (menu);                         /* display command menu        */
        break;
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -