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

📄 wdadmabm.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 ************************************************************************
 *  Program     : WDADMABM.C                                            *
 *  Description : Demo program for Windows with bus master DMA waveform *
 *                output of PCI-1721                                    *
 *  Revision    : 1.00                                                  *
 *  Date        : 10/17/2003                  Advantech Co., Ltd.       *
 ************************************************************************
 */

#include <windows.h>
#include <stdio.h>
#include <math.h>
#include "..\..\..\..\include\driver.h"

enum WAVE_TYPE {WAVE_SINE, WAVE_TRIANGLE, WAVE_SQUARE, SINE_TRIANGLE, NO_WAVE};

typedef struct {
   WAVE_TYPE wWaveform;
   float fMagnitude;
   float fOffset;
   int wPeriod;
} SWAVE, FAR *LPSWAVE;

static  SWAVE sWaveCh0 = {WAVE_SINE,        // output waveform A index
                          2.00f,            // the magnitude of output waveform A
                          2.00f,            // the offset of output waveform A
                          2048},            // the points of one period
              sWaveCh1 = {WAVE_TRIANGLE,    // output waveform B index
                          1.00f,            // the magnitude of output waveform B
                          3.00f,            // the offset of output waveform B
                          2048},            // the points of one period
              sWaveCh2 = {WAVE_SQUARE,      // output waveform C index
                          2.00f,            // the magnitude of output waveform C
                          2.00f,            // the offset of output waveform C
                          2048},            // the points of one period
              sWaveCh3 = {SINE_TRIANGLE,    // output waveform D index
                          2.00f,            // the magnitude of output waveform D
                          2.00f,            // the offset of output waveform D
                          2048};            // the points of one period

static  LONG   DriverHandle = NULL;        // driver handle

HGLOBAL hBuf[4]={NULL,NULL,NULL,NULL};
HGLOBAL hVoltageBuf[4]={NULL,NULL,NULL,NULL};
HGLOBAL hCommonBuf=NULL;
USHORT far * lpBuf[4]={NULL,NULL,NULL,NULL};
FLOAT  far * lpVoltageBuf[4]={NULL,NULL,NULL,NULL};
USHORT far * lpCommonBuf=NULL;

BOOL bThreadloop = FALSE;

void SetRealBuffer(float far *, long, LPSWAVE);
void SetMultiToOneBuffer(USHORT, int);
void MyFreeBuffer();

void UserThread();
void adInterruptEvent();
void adBufChangeEvent();
void adOverrunEvent();
void adTerminateEvent();

void ErrorHandler(DWORD);

void main()
{
   DWORD  dwErrCde;
   ULONG  lDevNum;
   int    i, iSamples = 2048;
   USHORT usEnabledChannel = 0;
   USHORT usChannelCount = 0;
   HANDLE hThreadHandle;
   DWORD  dwThreadID;
   PT_FAOScale         ptFAOScale;          // FAOScale table
   PT_EnableEvent      ptEnableEvent;       // Enable event
   PT_FAOWaveFormStart ptFAOWaveFormStart;  // FAODMAStart table
   
   //Step 1: Display hardware and software settings for running this example
   printf("*****************************************************************\n");
   printf("*  Advantech Driver Demo : Waveform output with bus master DMA  *\n");
   printf("*****************************************************************\n");
   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("Input Conversion Number in one cycle (>=512): ");
   // scanf("%d", &iSamples);

   fflush(stdin);   // clear standard input buffer

   //Step 3: Open device
   dwErrCde = DRV_DeviceOpen(lDevNum, &DriverHandle);
   if (dwErrCde != SUCCESS)
   {
      ErrorHandler(dwErrCde);
      printf("Program terminated!\n");
      printf("Press any key to exit....");
      getchar();
      return;
   }

   // Step 4: Allocate buffer and set data to buffer
	// Process 4 channel
	usChannelCount = 0;    // reset channel count
	usEnabledChannel = 0;
   
	for(i = 0; i < 4; i++)
	{
	   // Allocate memory used by driver
		if((hBuf[i] = (USHORT far *)GlobalAlloc(GPTR,
			sizeof(USHORT) * iSamples)) == 0)
		{
			printf("Not enough memory for buffer");
         MyFreeBuffer();
			DRV_DeviceClose((LONG far *)&DriverHandle);
         printf("Program terminated!\n");
         printf("Press any key to exit....");
         getchar();
         return;
		}

		// Allocate memory for real voltage
		if((hVoltageBuf[i] = (FLOAT far *)GlobalAlloc(GPTR,
			sizeof(FLOAT) * iSamples)) == 0)
		{
         printf("Not enough memory for buffer");
         MyFreeBuffer();
         DRV_DeviceClose((LONG far *)&DriverHandle);
         printf("Program terminated!\n");
         printf("Press any key to exit....");
         getchar();
         return;
		}

		lpBuf[i] = (USHORT far *)hBuf[i];
		lpVoltageBuf[i] = (FLOAT far  *)hVoltageBuf[i];

		switch(i)
		{
		case 0 : 
				 if(sWaveCh0.wWaveform != NO_WAVE)
				 {
					 usEnabledChannel |= ADV_CHANNEL0;
					 // set real voltage to hVoltageBuf
					 SetRealBuffer(lpVoltageBuf[0], iSamples, &sWaveCh0);
					 usChannelCount++;
				 }
				 else
					 continue;

				 break;
		case 1 : 
				 if(sWaveCh1.wWaveform != NO_WAVE)
				 {
					 usEnabledChannel |= ADV_CHANNEL1;
					 SetRealBuffer(lpVoltageBuf[1], iSamples, &sWaveCh1);
					 usChannelCount++;
				 }
				 else
					 continue;

				 break;
		case 2 : 
				 if(sWaveCh2.wWaveform != NO_WAVE) 
				 {
					 usEnabledChannel |= ADV_CHANNEL2;
					 SetRealBuffer(lpVoltageBuf[2], iSamples, &sWaveCh2);
					 usChannelCount++;
				 }
				 else
					 continue;

				 break;
		case 3 : 
				 if(sWaveCh3.wWaveform != NO_WAVE) 
				 {
					 usEnabledChannel |= ADV_CHANNEL3;
					 SetRealBuffer(lpVoltageBuf[3], iSamples, &sWaveCh3);
					 usChannelCount++;
				 }
				 else
					 continue;

				 break;
		}
	
		// call FAOScale for transfer voltage to binary data
		ptFAOScale.VoltArray = lpVoltageBuf[i];
		ptFAOScale.BinArray  = lpBuf[i];
		ptFAOScale.chan      = i;
		ptFAOScale.count     = iSamples;
		if ((dwErrCde = DRV_FAOScale(DriverHandle,
			(LPT_FAOScale)&ptFAOScale)) != 0)
		{
         ErrorHandler(dwErrCde);
         MyFreeBuffer();
         DRV_DeviceClose((LONG far *)&DriverHandle);
         printf("Program terminated!\n");
         printf("Press any key to exit....");
         getchar();
         return;
		}
	}

	// Allocate memory for common buffer of 4 channel
	if((hCommonBuf = (USHORT far *)GlobalAlloc(GHND,
			sizeof(USHORT) * iSamples * usChannelCount)) == 0)
	{
      printf("Not enough memory for buffer");
      MyFreeBuffer();
      DRV_DeviceClose((LONG far *)&DriverHandle);
      printf("Program terminated!\n");
      printf("Press any key to exit....");
      getchar();
      return;
	}

	 // Lock down buffer
	 if((lpCommonBuf = (USHORT far *)GlobalLock(hCommonBuf)) == 0)
	{
      printf("Not enough memory for buffer");
      MyFreeBuffer();
      GlobalFree(hCommonBuf);
      DRV_DeviceClose((LONG far *)&DriverHandle);
      printf("Program terminated!\n");
      printf("Press any key to exit....");
      getchar();
      return;
	}

	// set 4 buffer to 1 common buffer
	SetMultiToOneBuffer(usEnabledChannel, iSamples);

   // Step 5: Enable event feature
   ptEnableEvent.EventType = ADS_EVT_INTERRUPT  |
                             ADS_EVT_BUFCHANGE  |
                             ADS_EVT_TERMINATED |
                             ADS_EVT_OVERRUN;
   ptEnableEvent.Enabled = 1;
   ptEnableEvent.Count   = 1;
   if ((dwErrCde = DRV_EnableEvent(DriverHandle,
      (LPT_EnableEvent)&ptEnableEvent)) != 0)
   {
      ErrorHandler(dwErrCde);
      MyFreeBuffer();
      GlobalUnlock(hCommonBuf);
      GlobalFree(hCommonBuf);
      DRV_DeviceClose((LONG far *)&DriverHandle);
      printf("Program terminated!\n");
      printf("Press any key to exit....");
      getchar();
      return;
   }
   
   // Step 6: call FAOWaveFormStart for start action
   // Default steting - 
   // conversion number = 2048 * 4
   // Enabled Channel = 0,1,2,3
   // pacer rate = 1000 Hz
   // output waveform number = 30
   
   ptFAOWaveFormStart.TrigSrc    = 0;			// external or internal trigger
   ptFAOWaveFormStart.SampleRate = 10000;		// pacer rate = 10K
   ptFAOWaveFormStart.Count      = iSamples * usChannelCount; // DA conversion number
   ptFAOWaveFormStart.WaveCount  = 10;			// Waveform number
   ptFAOWaveFormStart.Buffer     = (USHORT far *)lpCommonBuf;  // analog output data

⌨️ 快捷键说明

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