📄 pulse.cpp
字号:
/*
****************************************************************************************
* Program : PULSE.CPP *
* Description : Demo program for pulse output function *
* Boards Supp. : *
* APIs used : DRV_DeviceOpen,DRV_DeviceClose, DRV_GetErrorMessage, *
* DRV_CounterReset, DRV_CounterPulseStart *
* Revision : 1.00 *
* Date : 7/8/1999 Advantech Co., Ltd. *
****************************************************************************************
*/
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <conio.h>
#define WIN_CONSOLE
#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 period;
float freq;
PT_CounterPulseStart ptCounterPulseStart;
//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");
//Step 2: Input parameters
printf("\nPlease input parameters:");
printf("\nDevice Number (check the device installation utility): ");
scanf("%d", &lDevNum);
printf("\nOutput Channel: ");
scanf("\n%d", &usChan);
printf("\nFrequency:");
scanf("%f", &freq);
period = 1/freq;
//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);
// Step 5: Start counter operation by DRV_CounterPulseStart
ptCounterPulseStart.counter = usChan; // pulse output channel
ptCounterPulseStart.period = period; // period in second
ptCounterPulseStart.UpCycle = (float)period/2;// up cycle time in second
ptCounterPulseStart.GateMode = 0; // not used
dwErrCde = DRV_CounterPulseStart(lDriverHandle, &ptCounterPulseStart);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde);
return;
}
// Step 6: exit while pressing any key
printf("\nPress any key to exit....");
getch();
// Step 7: Stop pulse output by DRV_CounterReset
dwErrCde = DRV_CounterReset(lDriverHandle, usChan);
// Step 8: 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 + -