836demo1.c

来自「研华工控卡PCL836的一些例程序」· C语言 代码 · 共 139 行

C
139
字号
/*****************************************************************************
 *                                                                           *
 *          836DEMO1.C - Example of  PCL-836  counter  card                  *
 *                       Event counter without filter demo program           *
 *                                            Advantech Co,.Ltd.  2/24/97    *
 *****************************************************************************
*/
#include <dos.h>
#include <stdio.h>
#include <bios.h>
#include <conio.h>
#include <stdlib.h>
#include "..\driver.h"

void ShowErrorMessage(unsigned long ErrorCode);
void dis_main(void);

/*=============================================================*/
/*   Main program                                              */
/*=============================================================*/
main(void)
{
     unsigned short usBaseAddress, usDeviceNumber;
     unsigned short usCounter;
     unsigned short usOverflow0, usOverflow1, usOverflow2;
     unsigned short usOverflow3, usOverflow4, usOverflow5;
     unsigned long  ulCount0, ulCount1, ulCount2;
     unsigned long  ulCount3, ulCount4, ulCount5;
     unsigned long  retCode;
     unsigned int   keyin;

     // default base address & device number
     usBaseAddress = 0x240;
     usDeviceNumber = 0x00;

     // display demo program description
     clrscr();
     dis_main();

     // Open device driver
     retCode = DeviceOpen(usDeviceNumber, usBaseAddress);
     if (retCode != 0L)
     {
        ShowErrorMessage(retCode);
        return;
     }

     // initilize counter function
     for (usCounter=0 ; usCounter < 6 ; usCounter++)
     {
       CounterConfig(usDeviceNumber, usCounter, 0, 80000);
       retCode = CounterEventStart(usDeviceNumber, usCounter);
       if (retCode != 0L)
       {
        ShowErrorMessage(retCode);
        return;
       }
     }

     // read counter function
     while(1)
     {
       // show counter header
       gotoxy(14,9);
       printf("COUNT 0   COUNT 1   COUNT 2   COUNT 3   COUNT 4   COUNT 5");

       do
       {
          CounterEventRead(usDeviceNumber, 0, &usOverflow0, &ulCount0);
          CounterEventRead(usDeviceNumber, 1, &usOverflow1, &ulCount1);
          CounterEventRead(usDeviceNumber, 2, &usOverflow2, &ulCount2);
          CounterEventRead(usDeviceNumber, 3, &usOverflow3, &ulCount3);
          CounterEventRead(usDeviceNumber, 4, &usOverflow4, &ulCount4);
          CounterEventRead(usDeviceNumber, 5, &usOverflow5, &ulCount5);

           // show counter value
           gotoxy(1,10);
           printf("Counting = %9lu %9lu %9lu %9lu %9lu %9lu",
               ulCount0, ulCount1, ulCount2, ulCount3, ulCount4, ulCount5);
           gotoxy(1,12);
           printf("Overflow = %9u %9u %9u %9u %9u %9u", usOverflow0, usOverflow1,
                  usOverflow2, usOverflow3, usOverflow4, usOverflow5);

       } while(!bioskey(1));

       keyin = bioskey(0);
       if((keyin &= 0x0ff) == 27)
          break;

       gotoxy(15,20);
       printf("  Press ESC to ABORT, other key to CONTINUE !");
       if((bioskey(0) & 0x0ff) == 27)
          break;
     }

     for (usCounter=0 ; usCounter < 6 ; usCounter++)
        CounterReset(usDeviceNumber, usCounter);
     DeviceClose(usDeviceNumber);
     return;
}

/*=============================================================*/
/*  display description                                        */
/*=============================================================*/
void dis_main(void)
{
     gotoxy(25,3);
     printf("******  PCL836DEMO 1  ***** ");
     gotoxy(10,5);
     printf("The demo program uses event-counter to test counter 0 to 5");
     gotoxy(10,6);
     printf("This Base Address is set to 0x240.");
}

/*=============================================================*/
/*     show error message                                      */
/*=============================================================*/
void ShowErrorMessage(unsigned long ErrorCode)
{
   char szErrorMsg[10][80]=
   {
   {"DeviceNumber is out of range ,its range is from 0 to 9."},
   {"Invalid Base Address."},
   {"This DeviceNumber is in use !"},
   {"Invalid Base Address, DeviceOpen must be called before any other functions."},
   {"Illegal Channel"},
   {"Illegal Input Param"},
   {"The freqence of calling the function is too high."},
   {"FOUT frequency is out of range "},
   {"Invalid Port Channel"}
   };

   gotoxy(10,24);
   if ((ErrorCode > (unsigned long)MaxErrorCode) || (ErrorCode == 0L))
    printf("No Error");
   else
    printf("%s\n",&szErrorMsg[ErrorCode-1][0]);
}

⌨️ 快捷键说明

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