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

📄 pulsepwm.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
/*
 ****************************************************************************************
 * Program        : PULSEPWM.CPP                                                        *
 * Description    : Demo program for pulse output function with PWM                     *
 * Boards Supp.   : PCI-1780                                                            *
 * APIs used      : DRV_DeviceOpen,DRV_DeviceClose, DRV_GetErrorMessage,                *
 *                  DRV_CounterReset, DRV_CounterPWMSetting, DRV_CounterPWMEnable       *
 * Revision       : 1.00                                                                *
 * Date           : 09/06/2003                  Advantech Co., Ltd.                      *
 ****************************************************************************************
 */
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>

#define MAX_DEVICES 255
#include "..\..\..\..\include\driver.h"

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

void main()
{
    DWORD  dwErrCde;
    ULONG  lDevNum;
    long   lDriverHandle;
    USHORT usChan;
    float  fPeriod, fHiPeriod;  
	long   ulOutCount;
    DEVLIST DeviceList[MAX_DEVICES];
    SHORT   gnNumOfDevices;
    SHORT   nOutEntries;
    int     i;
    int     iCmd;
    PT_CounterPWMSetting tCounterPWMSetting;

    //Step 1: Display hardware and software settings for running this example
    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);
    fflush(stdin);
    printf("PWM Channel: ");
    scanf("%d", &usChan);
    fflush(stdin);
    printf("\nplease input the period(1e-7~0.82)");
	 scanf("%f", &fPeriod);
    fflush(stdin);
	 printf("\nplease input the Hi period(5e-8~0.82)");
	 scanf("%f", &fHiPeriod);
    fflush(stdin);
	 ulOutCount = 0;
    fflush(stdin);
	
    //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: Reset counter by DRV_CounterReset
    dwErrCde = DRV_CounterReset(lDriverHandle, usChan);
    if (dwErrCde != SUCCESS)
    {
        ErrorHandler(dwErrCde);
        printf("Program terminated!\n");

        dwErrCde = DRV_DeviceClose(&lDriverHandle);

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

    // Step 5: Start counter operation by DRV_CounterPulseStart
    tCounterPWMSetting.Port = usChan;            // output channel
    tCounterPWMSetting.Period   = fPeriod;       // period
    tCounterPWMSetting.HiPeriod =  fHiPeriod;    // up cycle
    tCounterPWMSetting.OutCount = ulOutCount;             // output count
    tCounterPWMSetting.GateMode = 0;             // gate mode

    dwErrCde = DRV_CounterPWMSetting(lDriverHandle, &tCounterPWMSetting);
    if (dwErrCde != SUCCESS)
    {
       DRV_CounterReset(lDriverHandle, usChan); 
       ErrorStop(&lDriverHandle, dwErrCde);
       return;
    }

    // Step 6: Enable PWM operation by DRV_CounterPWMEnable
    dwErrCde = DRV_CounterPWMEnable(lDriverHandle, usChan);
    if(dwErrCde != SUCCESS)
    {
        DRV_CounterReset(lDriverHandle, usChan);
        ErrorStop(&lDriverHandle, dwErrCde);
        return;
    }

    // Step 7: Wait for the user's command
    
    while (true)
    {
       printf("\nPulse out on PWM%d with", usChan);
       printf("\nPeriod:      %5.3f second.", tCounterPWMSetting.Period);
       printf("\nUp cycle:    %5.4f second.", tCounterPWMSetting.HiPeriod);
       printf("\n\nPress 'M' to Modify the Period and up cycle");
       printf("\nPress 'X' to stop running...\n");
       iCmd = getch();
       if (('X' == iCmd) || ('x' == iCmd)) 
       {
          break;
       }else if ((('M' == iCmd) || ('m' == iCmd)) && ulOutCount==0) 
       {
          printf("\n");
          printf("\nplease input the period(1e-7~0.82)");
          scanf("%f", &fPeriod);
          fflush(stdin);
          printf("\nplease input the Hi period(5e-8~0.82)");
          scanf("%f", &fHiPeriod);
          fflush(stdin);
          
          tCounterPWMSetting.Period   = fPeriod;         // period
          tCounterPWMSetting.HiPeriod =  fHiPeriod; 
          dwErrCde = DRV_CounterPWMSetting(lDriverHandle, &tCounterPWMSetting);
          if (dwErrCde != SUCCESS)
          {
             DRV_CounterReset(lDriverHandle, usChan);
             ErrorStop(&lDriverHandle, dwErrCde);
             return;
          }
       }
    }

    // Step 8: Stop PWM output by DRV_CounterReset
    dwErrCde = DRV_CounterReset(lDriverHandle, usChan);

    // Step 9: Close device
    dwErrCde = DRV_DeviceClose(&lDriverHandle);
}//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 + -