// Sample3Dlg.cpp : implementation file // #include "stdafx.h" #include "Sample3.h" #include "Sample3Dlg.h" #include #include "AIOUSB.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif const double Pi = 3.1415926535897932384626433832795; unsigned long DeviceIndex; ///////////////////////////////////////////////////////////////////////////// // CSample3Dlg dialog CSample3Dlg::CSample3Dlg(CWnd* pParent /*=NULL*/) : CDialog(CSample3Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CSample3Dlg) m_BlurbText = _T(""); //}}AFX_DATA_INIT m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSample3Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSample3Dlg) DDX_Control(pDX, IDC_GOBUTTON, m_GoButton); DDX_Text(pDX, IDC_BLURBSTATIC, m_BlurbText); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSample3Dlg, CDialog) //{{AFX_MSG_MAP(CSample3Dlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_GOBUTTON, OnGoButtonClick) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSample3Dlg message handlers BOOL CSample3Dlg::OnInitDialog() { CDialog::OnInitDialog(); SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon unsigned long Status, PID; //This simply tries diOnly, instead of trying to detect a board by using GetDevices() and looping over QueryDeviceInfo(). DeviceIndex = diOnly; Status = QueryDeviceInfo(DeviceIndex, &PID, NULL, NULL, NULL, NULL); if ( Status == ERROR_FILE_NOT_FOUND ) m_BlurbText = "No device found. Make sure the board is plugged in, and check Device Manager to see if it has an error or is stuck with a (D15 Lo) tag."; else if (Status == ERROR_DUP_NAME ) m_BlurbText = "Multiple devices found. This sample only works with one."; else if ( Status != ERROR_SUCCESS ) { char Buf[256]; sprintf(Buf, "Error %d detecting devices.", Status); m_BlurbText = Buf; } else if ( ! (PID == 0x4002) ) m_BlurbText = "A device was detected, but not one supported by this sample."; else { m_BlurbText = "This sample uses a single DACOutputProcess() call to control the arbitrary waveform generator. " "The data loops sine waves, with a different frequency on each DAC, from 10 Hz to 80 Hz. " "Click Go when ready.\r\n\r\n" ; m_GoButton.EnableWindow(TRUE); } UpdateData(FALSE); return TRUE; // return TRUE unless you set the focus to a control } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CSample3Dlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } HCURSOR CSample3Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSample3Dlg::OnGoButtonClick() { const DACDataLen = 80000; PWORD DACData; unsigned long Status; int I, Channel; double ClockHz, F; int ChannelTheta[8]; ClockHz = 10000; //Each tick of the clock outputs all DAC channels in parallel. //At 10000 Hz, DAC 0 will be 10 Hz, DAC 1 will be 20 Hz, DAC 2 will be 30 Hz, etc. for ( I = 0; I <= 7; ++I ) ChannelTheta[I] = 0; DACData = PWORD(malloc(80000 * sizeof(DACData[0]))); for ( I = 0; I < DACDataLen; ++I ) { Channel = I % 8; ChannelTheta[Channel] = (ChannelTheta[Channel] + (1 + Channel)) % 1000; F = Pi * 2 / 1000.0 * ChannelTheta[Channel]; //Convert theta from integer permille to float radians. F = 0xFFF / 2.0 * (sin(F) + 1); //Convert from ±1 to 0-FFF. DACData[I] = WORD(F); //Round and store. if ( Channel == 7 ) DACData[I] |= 0x2000; //If it's the last channel, set the End-Of-DACs bit. } I = DACDataLen - 1; DACData[I] |= 0x1000; //Set the Loop bit on the last sample of the whole pattern. m_BlurbText += "Loading...\r\n"; UpdateData(FALSE); Status = DACOutputProcess(DeviceIndex, &ClockHz, DACDataLen, &DACData[0]); if ( Status == ERROR_SUCCESS ) m_BlurbText += "Now running; you can close this sample and it will keep running. Check it out with a scope.\r\n"; else m_BlurbText += "Error ' + IntToStr(Status) + ' from DACOutputProcess.\r\n" ; UpdateData(FALSE); }