836demo4.c

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

C
128
字号
/*****************************************************************************
 *                                                                           *
 *          836DEMO4.C - Example of  PCL-836  counter  card                  *
 *                       Digital Input/Outout 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 usReadPort0, usReadPort1;
     unsigned short usWritePort0, usWritePort1;
     unsigned long  retCode;
     unsigned int   keyin;

     // default base address & device number
     usBaseAddress = 0x240;
     usDeviceNumber = 0x00;
     usWritePort0 = 0xaa;
     usWritePort1 = 0x33;

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

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

     // perform Digital Input/Output function
     while(1)
     {
       // show counter header
       gotoxy(14,9);
       printf("Digital Input Port 0 Value: ");
       gotoxy(14,10);
       printf("Digital Input Port 1 Value: ");
       gotoxy(14,11);
       printf("Digital Output Port 0 Value: %2x", usWritePort0);
       gotoxy(14,12);
       printf("Digital Output Port 1 Value: %2x", usWritePort1);

       do
       {
          DioWritePortByte(usDeviceNumber, 0, 0xff, usWritePort0);
          DioWritePortByte(usDeviceNumber, 1, 0xff, usWritePort1);
          DioReadPortByte(usDeviceNumber, 0, &usReadPort0);
          DioReadPortByte(usDeviceNumber, 1, &usReadPort1);

           // show counter value
           gotoxy(43,9);
           printf("%2x", usReadPort0);
           gotoxy(43,10);
           printf("%2x", usReadPort1);

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

     DeviceClose(usDeviceNumber);
     return;
}

/*=============================================================*/
/*  display description                                        */
/*=============================================================*/
void dis_main(void)
{
     gotoxy(24,3);
     printf("******  PCL836DEMO 4  ***** ");
     gotoxy(9,5);
     printf("The demo program tests digital input/output.");
     gotoxy(9,6);
     printf("This Base Address is set to 0x240.");
     gotoxy(9,7);
     printf("Please use a flat cable (20pin) to connect CN1(DIN/OUT) and CN2 (DIN/IN)");
}

/*=============================================================*/
/*     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 + -
显示快捷键?