?? adint.cpp
字號(hào):
/*
****************************************************************************************
* Program : ADINTF.CPP *
* Description : Demo program for analog input function with interrupt triggering *
* (accept user to change FIFO size) *
* Boards Supp. : PCL-818 series/816/1800/812PG/711B, MIC-2718, PCM-3718 *
* PCI-1710/1713 *
* APIs used : DRV_DeviceOpen,DRV_DeviceClose, DRV_FAIIntStart, DRV_FAIStop, *
* DRV_FAICheck, DRV_FAITransfer, DRV_GetErrorMessage *
* Revision : 1.00 *
* Date : 7/1/2003 Advantech Co., Ltd. *
****************************************************************************************
*/
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#include "..\..\..\include\driver.h"
/******************************
* Macro definition *
******************************/
#define ERR_SIZE USHORT (-1)
/******************************
* Local function declaration *
******************************/
void ErrorHandler(DWORD dwErrCde);
BOOL AllocateDataBuffer(long, int, int, USHORT**, void**);
void FreeDataBuffer(USHORT* pusINTBuf, void *pUserBuf);
void ErrorStop(long*, DWORD, USHORT*, void*);
USHORT GetFIFOSize(ULONG);
void main()
{
DWORD dwErrCde;
ULONG lDevNum;
long lDriverHandle;
int i, iSamples;
USHORT usChan;
char chFifo;
USHORT *pusINTBuf; //INT internal transfer buffer
float *pfUserBuf; //User buffer for retrieve data
USHORT usActiveBuf; //for PT_FAICheck to return data
USHORT usStopped;
ULONG ulRetrieved;
USHORT usOverrun;
USHORT usHalfReady;
ULONG ulPreRetrieved;
USHORT usFifoSize; //Fifo size
PT_FAIIntStart tFAIIntStart;
PT_FAICheck tFAICheck;
PT_FAITransfer tFAITransfer;
//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("Do you want to use FIFO ?\n");
printf("If you want, press 'y'. Any other key to use NO Fifo.\n");
fflush(stdin);
scanf("%c", &chFifo);
if(chFifo == 'y')
{
usFifoSize = GetFIFOSize(lDevNum);
if(usFifoSize == ERR_SIZE)
{
printf("Program terminated!\n");
printf("Press any key to exit....");
getch();
return;
}
if(usFifoSize == 0)
{
printf("No FIFO on the device.\n");
usFifoSize = 1;
}
else
{
printf("The FIFO size of the device you specified: %d\n", usFifoSize);
printf("Do you want to edit the FIFO size ?\n");
printf("If you want, press 'y'. Any other key to maintain the original.\n");
fflush(stdin);
scanf("%c", &chFifo);
if(chFifo == 'y')
{
printf("Input another FIFO Size (must be a natural number): ");
scanf("%d", &usFifoSize);
if(usFifoSize <= 0)
usFifoSize = 1;
}
}
}
else
{
usFifoSize = 1;
}
printf("Input Conversion Number: ");
scanf("%d", &iSamples);
printf("Input Channel: ");
scanf("%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();
return ;
}
//Step 4: Allocate INT & data buffer for interrupt transfer
if( AllocateDataBuffer(
lDriverHandle, // driver handle
iSamples, // data count
sizeof(float), // size of one data
&pusINTBuf, // INT buffer allocated
(void**)&pfUserBuf)==false) // user buffer allocated
{
printf("Program terminated\n");
DRV_DeviceClose(&lDriverHandle);
printf("Press any key to exit....");
getch();
return;
}
// Step 5: Start interrupt transfer
tFAIIntStart.TrigSrc = 0; // 0: internal trigger, 1: external trigger
tFAIIntStart.SampleRate = 10000; // pacer rate: 10KHz
tFAIIntStart.chan = usChan; // input channel
tFAIIntStart.gain = 0; // gain code:0, check manual for detail input range
tFAIIntStart.count = iSamples; // number of samples
tFAIIntStart.buffer = pusINTBuf; // data buffer pointer
tFAIIntStart.cyclic = 0; // 0: non-cyclic mode, 1: cyclic-mode
tFAIIntStart.IntrCount = usFifoSize; // FIFO interrupt
printf("\nNumber of samples:%d\nSize of Fifo:%d\n",tFAIIntStart.count,tFAIIntStart.IntrCount );
dwErrCde = DRV_FAIIntStart(lDriverHandle, &tFAIIntStart);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde, pusINTBuf, (void*)pfUserBuf);
return;
}
// Step 6: Check INT Status
tFAICheck.ActiveBuf = &usActiveBuf; // not used for single buffer
tFAICheck.stopped = &usStopped; // returned status: 1: complete, 0: imcomplete
tFAICheck.retrieved = &ulRetrieved; // actual conversion count
tFAICheck.overrun = &usOverrun; // not used for non-cyclic mode
tFAICheck.HalfReady = &usHalfReady; // not used for single buffer
ulPreRetrieved = 0;
do
{
dwErrCde = DRV_FAICheck(lDriverHandle, &tFAICheck);
if (ulPreRetrieved > ulRetrieved) break;
ulPreRetrieved = ulRetrieved;
} while ((dwErrCde==0) && (usStopped != 1));
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde, pusINTBuf, (void*)&pfUserBuf);
return;
}
// Step 7: Stop A/D conversion for high speed
dwErrCde = DRV_FAIStop(lDriverHandle);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde, pusINTBuf, (void*)&pfUserBuf);
return;
}
// Step 8: Get data from driver
tFAITransfer.ActiveBuf = 0; // 0: single buffer, 1: double buffer
tFAITransfer.DataType = 1; // 0: raw or unsigned short data, 1: float data
tFAITransfer.start = 0; // 0: returned starting point of the data buffer
tFAITransfer.count = iSamples; // requested data length. It requests all data.
tFAITransfer.overrun = &usOverrun; // returned overrun flag
tFAITransfer.DataBuffer = pfUserBuf;
dwErrCde = DRV_FAITransfer(lDriverHandle, &tFAITransfer);
if (dwErrCde != SUCCESS)
{
ErrorStop(&lDriverHandle, dwErrCde, pusINTBuf, (void*)&pfUserBuf);
return;
}
// Step 9: Display data starting from 0 to 99 of buffer location
for (i = 0; i < 100; i ++)
{
printf("Buf[%ld] = %10.6f\n", i, pfUserBuf[i]);
}
// Step 10: Free buffer
FreeDataBuffer(pusINTBuf, (void *)pfUserBuf);
// Step 11: Close device
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: AllocateDataBuffer
* Allocate data buffer for INT transfer.
* Paramaters: lDrvHandle, IN, Driver handle
* iSamples, IN, Data count.
* iDataSize, IN, Size of one data
* plINTBuf, OUT, Interrupt buffer pointer.
* pUserBuf, OUT, user data buffer. Converted data is stored
* here.
* return: TRUE - memory allocate successfully
* FALSE - allocate failed
**********************************************************************/
BOOL AllocateDataBuffer(
long lDrvHandle,
int iSamples,
int iDataSize,
USHORT ** pusINTBuf,
void ** pUserBuf)
{
// Allocate INT buffer for driver
*pusINTBuf = (USHORT*) GlobalAlloc(GPTR, iSamples * sizeof(USHORT));
if (pusINTBuf == NULL)
{
printf("\nError: Allocate memory error.\n");
return(false);
}
// Allocate memory for user buffer.
*pUserBuf = malloc(iSamples * iDataSize);
if (pUserBuf == NULL)
{
free(*pusINTBuf);
printf("\nError: Allocate memory error.\n");
return(false);
}
return(true);
}//AllocateDataBuffer
/**********************************************************************
* Function: FreeDataBuffer
* Free data buffer allocated by function AllocateDataBuffer
* Paramaters: pusINTBuf, IN, Address of Interrupt buffer
* pUserBuf, IN, Address of user buffer
* return: none
**********************************************************************/
void FreeDataBuffer(USHORT* pusINTBuf, void* pUserBuf)
{
GlobalFree((HGLOBAL)pusINTBuf);
free(pUserBuf);
}//FreeDataBuffer
/**********************************************************************
* Function: ErrorStop
* Release all resource and terminate program if error occurs
* Paramaters: pDrvHandle, IN/OUT, pointer to Driver handle
* dwErrCde, IN, Error code.
* plINTBuf, IN, Address of Interrupt buffer
* pUserBuf, IN, Address of user buffer
* return: none
**********************************************************************/
void ErrorStop( long* pDrvHandle,
DWORD dwErrCde,
USHORT* pusINTBuf,
void* pUserBuf)
{
//Free resource
DRV_FAIStop(*pDrvHandle);
FreeDataBuffer(pusINTBuf, pUserBuf);
//Error message
ErrorHandler(dwErrCde);
printf("Program terminated!\n");
//Close device
DRV_DeviceClose(pDrvHandle);
printf("Press any key to exit....");
getch();
exit(0);
}//ErrorStop
/**********************************************************************
* Function: GetFIFOSize
* Get the FIFO size of the device you specified
* Paramaters: lDevNum, IN, Device number by which to specify device
* return: the FIFO size of the specified device, if error occurs
return ERR_SIZE
*********************************************************************/
USHORT GetFIFOSize(ULONG lDevNum)
{
DWORD dwErrCde;
long lDriverHandle;
long lFifoSize;
// Step 1: Open device
dwErrCde = DRV_DeviceOpen(lDevNum, &lDriverHandle);
if (dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
return ERR_SIZE;
}
// Step 2: Get FIFO size
dwErrCde = DRV_GetFIFOSize(lDriverHandle, &lFifoSize);
if(dwErrCde != SUCCESS)
{
ErrorHandler(dwErrCde);
DRV_DeviceClose(&lDriverHandle);
return ERR_SIZE;
}
// Step 3: Close device
DRV_DeviceClose(&lDriverHandle);
// divide by 2 for conversion from byte to word
return (USHORT)lFifoSize / 2;
}//GetFIFOSize
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -