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

📄 pwmin.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
/*
 ****************************************************************************************
 * Program        : PWMIN.CPP                                                           *
 * Description    : Demo program for PWM width measurement function                     *
 * Boards Supp.   :                                                                     *
 * APIs used      : DRV_DeviceOpen,DRV_DeviceClose, DRV_GetErrorMessage,                *
 *                  DRV_CounterReset, DRV_PWMStartRead                                  *
 * Revision       : 1.00                                                                *
 * Date           : 10/19/2003                   Advantech Co., Ltd.                    *
 ****************************************************************************************
 */
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define WIN_CONSOLE
#include "..\..\..\include\driver.h"
#define  MAX_DEVICES  100

/******************************
 * Local function declaration *
 ******************************/
void ErrorHandler(DWORD dwErrCde);
void ErrorStop(long*, DWORD);

void main()
{
    int i;
	DWORD  dwErrCde;
    ULONG  lDevNum;
    long   lDriverHandle;
    short gnNumOfDevices;
    short nOutEntries; 
	DEVLIST DeviceList[MAX_DEVICES ];

    USHORT usChan;									// input channel          
	float  fHighperiod = 0.0; 
	float  fLowperiod = 0.0;
    PT_PWMStartRead  ptPWMStartRead;

    //Step 1: Display hardware and software settings for running this example
    printf("----------------Demo the PWM width Measurement---------------\n");
    printf("Before running this example, please\n");
    printf("use the device installation utility to add the device.\n");
	// Add type of PC Laboratory Card
    dwErrCde = DRV_DeviceGetList(&DeviceList[0], MAX_DEVICES, &nOutEntries);
    // Return the number of devices which you install in the system using
    // Device Installation
    dwErrCde = DRV_DeviceGetNumOfList(&gnNumOfDevices);
    printf("This is the installed device list:\n");
    for (i = 0; i < gnNumOfDevices; i++)
    {
        printf("  %3.3d %s\n",DeviceList[i].dwDeviceNum,DeviceList[i].szDeviceName);
    }
  
 
    //Step 2: Input parameters
    printf("\nPlease input parameters--------------------");
    printf("\nDevice Number (check the device installation utility): ");
    scanf("%d", &lDevNum);
    printf("\nInput Channel: ");
    scanf("\n%d", &usChan);

    //Step 3: Open device
    dwErrCde = DRV_DeviceOpen(lDevNum, &lDriverHandle);   
    if (dwErrCde != SUCCESS)
    {
        ErrorHandler(dwErrCde);
        printf("Program terminated!\n");

        printf("Press any key to exit....");
        getch();
        exit(1);
    }
 
    // Step 4: Start PWM width measurement
	ptPWMStartRead.usChan = usChan;
    ptPWMStartRead.flHiperiod = (FLOAT far *)&fHighperiod;
    ptPWMStartRead.flLowperiod = (FLOAT far *)&fLowperiod;
    while(!kbhit())
	{
		if ((dwErrCde = DRV_PWMStartRead(lDriverHandle,
				   (LPT_PWMStartRead)&ptPWMStartRead)) != 0)
		{
			ErrorHandler(dwErrCde);
			printf("Program terminated!\n");

			printf("Press any key to exit....");
			getch();
			exit(1);
		 }
		printf( "High Period = %fus    Low Period = %fus\n", fHighperiod, fLowperiod);
		Sleep((DWORD)500);
	}
    if (kbhit()) getch();

    // Step 5: stop PWM width measurement 
    dwErrCde = DRV_CounterReset(lDriverHandle,(LPARAM)usChan);
	if (dwErrCde != SUCCESS)
    {
        ErrorHandler(dwErrCde);
        printf("Program terminated!\n");
        printf("Press any key to exit....");
        getch();
        exit(1);
    }

    dwErrCde = DRV_DeviceClose(&lDriverHandle);

    printf("\nPress any key to exit....");
    getch();
}//main

/**********************************************************************
 * Function: ErrorHandler
 *           Show the error message for the corresponding error code
 * input:    dwErrCde, IN, Error code
 * return:   none
 **********************************************************************/
void ErrorHandler(DWORD dwErrCde)
{
    char szErrMsg[180];

    DRV_GetErrorMessage(dwErrCde, szErrMsg);
    printf("\nError(%d): %s\n", dwErrCde & 0xffff, szErrMsg);
}//ErrorHandler

/**********************************************************************
 * Function:   ErrorStop
 *             Release all resource and terminate program if error occurs 
 * Paramaters: pDrvHandle, IN/OUT, pointer to Driver handle
 *             dwErrCde, IN, Error code.
 * return:     none             
 **********************************************************************/
void ErrorStop(long *pDrvHandle, DWORD dwErrCde)
{
    //Error message 
    ErrorHandler(dwErrCde);
    printf("Program terminated!\n");
    
    //Close device
    DRV_DeviceClose(pDrvHandle);

    printf("Press any key to exit....");
    getch();
    exit(1);
}//ErrorStop

⌨️ 快捷键说明

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