// AI1216Dg.cpp : implementation file // #include "stdafx.h" #include "PAI1216.h" #include "AI1216Dg.h" #include "ACCES32.h" #define InsCR(x) x += 0x0d; x += 0x0a; #define MyKey "Software\\PCIFIND\\NTioPCI\\Parameters" TPCI_COMMON_CONFIG buf[64]; bool RunFlag, Testing; unsigned char WhichCard[64]; unsigned long WhichAddress[64]; const char *Cards = "PCI-AI12-16"; CString *VoltsDisplay[16], *CountsDisplay[16]; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif //--------------------------------------------------------------------------- void CtrMode(unsigned int addr, char cntr, char mode) { OutPortB(addr+3, (cntr << 6) | 0x30 | (mode << 1)); } //--------------------------------------------------------------------------- void CtrLoad(unsigned int addr ,int c,int val) { OutPortB(addr+c,val & 0x00FF); OutPortB(addr+c,(val>>8) & 0x00FF); } ///////////////////////////////////////////////////////////////////////////// // CPAI1216Dlg dialog CPAI1216Dlg::CPAI1216Dlg(CWnd* pParent /*=NULL*/) : CDialog(CPAI1216Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CPAI1216Dlg) m_CardCombo = _T(""); m_Channel1 = _T(""); m_Channel10 = _T(""); m_Channel11 = _T(""); m_Channel12 = _T(""); m_Channel13 = _T(""); m_Channel14 = _T(""); m_Channel15 = _T(""); m_Channel16 = _T(""); m_Channel2 = _T(""); m_Channel3 = _T(""); m_Channel4 = _T(""); m_Channel5 = _T(""); m_Channel6 = _T(""); m_Channel7 = _T(""); m_Channel8 = _T(""); m_Channel9 = _T(""); m_Status = _T(""); m_Channel0 = _T(""); m_Channel17 = _T(""); m_Channel18 = _T(""); m_Channel19 = _T(""); m_Channel20 = _T(""); m_Channel21 = _T(""); m_Channel22 = _T(""); m_Channel23 = _T(""); m_Channel24 = _T(""); m_Channel25 = _T(""); m_Channel26 = _T(""); m_Channel27 = _T(""); m_Channel28 = _T(""); m_Channel29 = _T(""); m_Channel30 = _T(""); m_Channel31 = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); VoltsDisplay[0] = &m_Channel0; VoltsDisplay[1] = &m_Channel1; VoltsDisplay[2] = &m_Channel2; VoltsDisplay[3] = &m_Channel3; VoltsDisplay[4] = &m_Channel4; VoltsDisplay[5] = &m_Channel5; VoltsDisplay[6] = &m_Channel6; VoltsDisplay[7] = &m_Channel7; VoltsDisplay[8] = &m_Channel8; VoltsDisplay[9] = &m_Channel9; VoltsDisplay[10] = &m_Channel10; VoltsDisplay[11] = &m_Channel11; VoltsDisplay[12] = &m_Channel12; VoltsDisplay[13] = &m_Channel13; VoltsDisplay[14] = &m_Channel14; VoltsDisplay[15] = &m_Channel15; CountsDisplay[0] = &m_Channel16; CountsDisplay[1] = &m_Channel17; CountsDisplay[2] = &m_Channel18; CountsDisplay[3] = &m_Channel19; CountsDisplay[4] = &m_Channel20; CountsDisplay[5] = &m_Channel21; CountsDisplay[6] = &m_Channel22; CountsDisplay[7] = &m_Channel23; CountsDisplay[8] = &m_Channel24; CountsDisplay[9] = &m_Channel25; CountsDisplay[10] = &m_Channel26; CountsDisplay[11] = &m_Channel27; CountsDisplay[12] = &m_Channel28; CountsDisplay[13] = &m_Channel29; CountsDisplay[14] = &m_Channel30; CountsDisplay[15] = &m_Channel31; } void CPAI1216Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CPAI1216Dlg) DDX_Control(pDX, IDOK, m_DataButton); DDX_Control(pDX, IDC_STATUS, m_StatusControl); DDX_Control(pDX, IDC_CARDCOMBO, m_CardComboControl); DDX_Control(pDX, IDC_CHANNELCOMBO, m_ChannelCombo); DDX_CBString(pDX, IDC_CARDCOMBO, m_CardCombo); DDX_Text(pDX, IDC_CHANNEL1, m_Channel1); DDX_Text(pDX, IDC_CHANNEL10, m_Channel10); DDX_Text(pDX, IDC_CHANNEL11, m_Channel11); DDX_Text(pDX, IDC_CHANNEL12, m_Channel12); DDX_Text(pDX, IDC_CHANNEL13, m_Channel13); DDX_Text(pDX, IDC_CHANNEL14, m_Channel14); DDX_Text(pDX, IDC_CHANNEL15, m_Channel15); DDX_Text(pDX, IDC_CHANNEL16, m_Channel16); DDX_Text(pDX, IDC_CHANNEL2, m_Channel2); DDX_Text(pDX, IDC_CHANNEL3, m_Channel3); DDX_Text(pDX, IDC_CHANNEL4, m_Channel4); DDX_Text(pDX, IDC_CHANNEL5, m_Channel5); DDX_Text(pDX, IDC_CHANNEL6, m_Channel6); DDX_Text(pDX, IDC_CHANNEL7, m_Channel7); DDX_Text(pDX, IDC_CHANNEL8, m_Channel8); DDX_Text(pDX, IDC_CHANNEL9, m_Channel9); DDX_Text(pDX, IDC_STATUS, m_Status); DDX_Text(pDX, IDC_CHANNEL0, m_Channel0); DDX_Text(pDX, IDC_CHANNEL17, m_Channel17); DDX_Text(pDX, IDC_CHANNEL18, m_Channel18); DDX_Text(pDX, IDC_CHANNEL19, m_Channel19); DDX_Text(pDX, IDC_CHANNEL20, m_Channel20); DDX_Text(pDX, IDC_CHANNEL21, m_Channel21); DDX_Text(pDX, IDC_CHANNEL22, m_Channel22); DDX_Text(pDX, IDC_CHANNEL23, m_Channel23); DDX_Text(pDX, IDC_CHANNEL24, m_Channel24); DDX_Text(pDX, IDC_CHANNEL25, m_Channel25); DDX_Text(pDX, IDC_CHANNEL26, m_Channel26); DDX_Text(pDX, IDC_CHANNEL27, m_Channel27); DDX_Text(pDX, IDC_CHANNEL28, m_Channel28); DDX_Text(pDX, IDC_CHANNEL29, m_Channel29); DDX_Text(pDX, IDC_CHANNEL30, m_Channel30); DDX_Text(pDX, IDC_CHANNEL31, m_Channel31); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CPAI1216Dlg, CDialog) //{{AFX_MSG_MAP(CPAI1216Dlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDCANCEL, OnExit) ON_BN_CLICKED(IDOK, OnGetData) ON_WM_SHOWWINDOW() ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CPAI1216Dlg message handlers BOOL CPAI1216Dlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon if (InPortB(0x61) == 0xAA55) { MessageBox("ACCESNT.SYS not detected. Please copy ACCESNT.SYS into [NT]/system32/drivers and re-run this sample.", "Warning", MB_OK); } // TODO: Add extra initialization here 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 CPAI1216Dlg::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(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CPAI1216Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } //--------------------------------------------------------------------------- void CPAI1216Dlg::OnExit() { exit(0); } //--------------------------------------------------------------------------- void CPAI1216Dlg::OnGetData() { if (!RunFlag) { m_Status += "Cannot run without card!"; InsCR(m_Status); UpdateData(false); m_StatusControl.LineScroll(999); } else if (Testing) { Testing = false; m_DataButton.SetWindowText("&Get Data"); KillTimer(0); } else { Testing = true; m_DataButton.SetWindowText("&Stop Data"); SetTimer(0, 250, NULL); } } //--------------------------------------------------------------------------- void CPAI1216Dlg::GetCardInfo() { HKEY Key; DWORD DataType, DataSize, num, i, n; RegOpenKeyEx(HKEY_LOCAL_MACHINE, MyKey, 0, KEY_ALL_ACCESS, &Key); DataSize = sizeof(num); RegQueryValueEx(Key, "NumDevices", NULL, &DataType, (PUCHAR)&num, &DataSize); if (num > 0) { DataSize = num * sizeof(TPCI_COMMON_CONFIG); RegQueryValueEx(Key, "PCICommonConfig", NULL, &DataType, (PUCHAR)buf, &DataSize); } n = 0; for (i=0; i 0) { m_Status += "NOTE: A card was found, but it is not the "; m_Status += (CString)Cards; m_Status += " card."; InsCR(m_Status); } m_Status += "Make sure you have run PCIFIND.EXE."; InsCR(m_Status); m_StatusControl.LineScroll(999); RunFlag = false; } else m_CardComboControl.SetCurSel(0); UpdateData(false); RegCloseKey(Key); } //--------------------------------------------------------------------------- void CPAI1216Dlg::OnShowWindow(BOOL bShow, UINT nStatus) { CDialog::OnShowWindow(bShow, nStatus); GetCardInfo(); m_ChannelCombo.SetCurSel(0); } //--------------------------------------------------------------------------- void CPAI1216Dlg::Fifo(unsigned long BASE) { unsigned char chan, numchan; unsigned short Value; short counts; double volts; char text[255]; numchan = m_ChannelCombo.GetCurSel() == 0 ? 8 : 16; ResetFifos(BASE);//clear channel and data fifos SetFifoChannels(BASE); CtrMode(BASE+8, 1, 2); CtrLoad(BASE+8, 1, 0x2); CtrMode(BASE+8, 2, 2); CtrLoad(BASE+8, 2, 0xFF); EnableCounters(BASE);//conversions started once counters are enabled CheckFifo(BASE); //check fifo bit to see if it's half full DisableCounters(BASE);//stop conversions while((InPortB(BASE+4) & 0x02) == 0x02) //bit goes low when fifo is empty { Value = InPort(BASE); counts = Value & 0xFFF; if (counts > 0x7FF) counts |= 0xF000; chan = Value >> 12; volts = 20.0 * (counts / 4095.0);//convert to volts: counts*max volt span/max counts if (chan >= numchan) { *(VoltsDisplay[chan]) = ""; *(CountsDisplay[chan]) = ""; } else { sprintf(text, "%2.2f", volts); *(VoltsDisplay[chan]) = text; sprintf(text, "%d", counts); *(CountsDisplay[chan]) = text; } } UpdateData(false); } //--------------------------------------------------------------------------- void CPAI1216Dlg::NoFifo(unsigned long BASE) { unsigned char chan, numchan; short counts; double volts; char text[255]; DisableCounters(BASE); numchan = m_ChannelCombo.GetCurSel() == 0 ? 8 : 16; for (chan = 0; chan < 16; chan++) { if (chan >= numchan) { *(VoltsDisplay[chan]) = ""; *(CountsDisplay[chan]) = ""; } else { SetChannel(BASE, chan); // write channel, range, SE or diff unsigned long Ticks = GetTickCount(); while (Ticks + 2 > GetTickCount()); StartConversion(BASE); // start conversion if (WaitForEOC(BASE) == 0) //WAITFOREOC returns zero if it times out { m_Status += "A/D Timed Out"; InsCR(m_Status); } else { counts = (short)((short)((short)(RetrieveADConversion(BASE+0)<<4)))>>4; //sign extend result volts = (counts * 20.0)/4095.0; //convert to volts: counts*max volt span/max counts sprintf(text, "%2.2f", volts); *(VoltsDisplay[chan]) = text; sprintf(text, "%d", counts); *(CountsDisplay[chan]) = text; } } } UpdateData(false); } //--------------------------------------------------------------------------- void CPAI1216Dlg::SetChannel(unsigned long BASE, unsigned char channel) { OutPortB(BASE+2, channel << 4); //channel selected in upper nibble at base+2 // and single-ended } //--------------------------------------------------------------------------- void CPAI1216Dlg::StartConversion(unsigned long BASE) { OutPortB(BASE+0, 0); //write anything to base+0 to start conversion } //--------------------------------------------------------------------------- unsigned short CPAI1216Dlg::WaitForEOC(unsigned long BASE) { unsigned short timeout=0xFFFF; while(((InPortB(BASE+4) & 0x80) == 0) && timeout--); return timeout; //0==error } //--------------------------------------------------------------------------- short CPAI1216Dlg::RetrieveADConversion(unsigned long BASE) { return (InPort(BASE+0) & 0x0FFF);//mask upper nibble; } //--------------------------------------------------------------------------- void CPAI1216Dlg::CheckFifo(unsigned long BASE) { while((InPortB(BASE+4) & 0x04) == 0x04);//bit goes low when fifo half full } //--------------------------------------------------------------------------- void CPAI1216Dlg::ResetFifos(unsigned long BASE) { OutPortB(BASE+4, 0x48);//0x40 resets channel fifo, 0x08 resets data fifo for (int i=0; i<1024; i++) InPortB(BASE); InPortB(BASE+6); } //--------------------------------------------------------------------------- void CPAI1216Dlg::SetFifoChannels(unsigned long BASE) { OutPort(BASE+2, 0x0000); OutPort(BASE+2, 0x1010); OutPort(BASE+2, 0x2020); OutPort(BASE+2, 0x3030); OutPort(BASE+2, 0x4040); OutPort(BASE+2, 0x5050); OutPort(BASE+2, 0x6060); OutPort(BASE+2, 0x7070); OutPort(BASE+2, 0x8080); OutPort(BASE+2, 0x9090); OutPort(BASE+2, 0xA0A0); OutPort(BASE+2, 0xB0B0); OutPort(BASE+2, 0xC0C0); OutPort(BASE+2, 0xD0D0); OutPort(BASE+2, 0xE0E0); OutPort(BASE+2, 0xF0F0); InPort(BASE+2); } //--------------------------------------------------------------------------- void CPAI1216Dlg::EnableCounters(unsigned long BASE) { OutPortB(BASE+4, 0x01); //set counter enable bit, starts conversions } //--------------------------------------------------------------------------- void CPAI1216Dlg::DisableCounters(unsigned long BASE) { OutPortB(BASE+4, 0); //set counter enable bit, starts conversions } //--------------------------------------------------------------------------- void CPAI1216Dlg::OnTimer(UINT nIDEvent) { UpdateData(true); if (WhichCard[m_CardComboControl.GetCurSel()] == 1) Fifo(WhichAddress[m_CardComboControl.GetCurSel()]); else NoFifo(WhichAddress[m_CardComboControl.GetCurSel()]); CDialog::OnTimer(nIDEvent); }