// 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 "System\\CurrentControlSet\\Services\\NTioPCI\\Parameters" TPCI_COMMON_CONFIG buf[64]; bool RunFlag, Testing; unsigned char WhichCard[64]; unsigned long WhichAddress[64]; const char *Cards[2] = {"PCI-A12-16", "PCI-A12-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_Status = _T(""); m_CountsEdit0 = _T(""); m_CountsEdit1 = _T(""); m_CountsEdit10 = _T(""); m_CountsEdit11 = _T(""); m_CountsEdit12 = _T(""); m_CountsEdit13 = _T(""); m_CountsEdit14 = _T(""); m_CountsEdit15 = _T(""); m_CountsEdit2 = _T(""); m_CountsEdit3 = _T(""); m_CountsEdit4 = _T(""); m_CountsEdit5 = _T(""); m_CountsEdit6 = _T(""); m_CountsEdit7 = _T(""); m_CountsEdit8 = _T(""); m_CountsEdit9 = _T(""); m_VoltsEdit0 = _T(""); m_VoltsEdit1 = _T(""); m_VoltsEdit10 = _T(""); m_VoltsEdit11 = _T(""); m_VoltsEdit12 = _T(""); m_VoltsEdit13 = _T(""); m_VoltsEdit14 = _T(""); m_VoltsEdit15 = _T(""); m_VoltsEdit2 = _T(""); m_VoltsEdit3 = _T(""); m_VoltsEdit4 = _T(""); m_VoltsEdit5 = _T(""); m_VoltsEdit6 = _T(""); m_VoltsEdit7 = _T(""); m_VoltsEdit8 = _T(""); m_VoltsEdit9 = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); VoltsDisplay[0] = &m_VoltsEdit0; VoltsDisplay[1] = &m_VoltsEdit1; VoltsDisplay[2] = &m_VoltsEdit2; VoltsDisplay[3] = &m_VoltsEdit3; VoltsDisplay[4] = &m_VoltsEdit4; VoltsDisplay[5] = &m_VoltsEdit5; VoltsDisplay[6] = &m_VoltsEdit6; VoltsDisplay[7] = &m_VoltsEdit7; VoltsDisplay[8] = &m_VoltsEdit8; VoltsDisplay[9] = &m_VoltsEdit9; VoltsDisplay[10] = &m_VoltsEdit10; VoltsDisplay[11] = &m_VoltsEdit11; VoltsDisplay[12] = &m_VoltsEdit12; VoltsDisplay[13] = &m_VoltsEdit13; VoltsDisplay[14] = &m_VoltsEdit14; VoltsDisplay[15] = &m_VoltsEdit15; CountsDisplay[0] = &m_CountsEdit0; CountsDisplay[1] = &m_CountsEdit1; CountsDisplay[2] = &m_CountsEdit2; CountsDisplay[3] = &m_CountsEdit3; CountsDisplay[4] = &m_CountsEdit4; CountsDisplay[5] = &m_CountsEdit5; CountsDisplay[6] = &m_CountsEdit6; CountsDisplay[7] = &m_CountsEdit7; CountsDisplay[8] = &m_CountsEdit8; CountsDisplay[9] = &m_CountsEdit9; CountsDisplay[10] = &m_CountsEdit10; CountsDisplay[11] = &m_CountsEdit11; CountsDisplay[12] = &m_CountsEdit12; CountsDisplay[13] = &m_CountsEdit13; CountsDisplay[14] = &m_CountsEdit14; CountsDisplay[15] = &m_CountsEdit15; } 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_STATUS, m_Status); DDX_Text(pDX, IDC_COUNTSEDIT0, m_CountsEdit0); DDX_Text(pDX, IDC_COUNTSEDIT1, m_CountsEdit1); DDX_Text(pDX, IDC_COUNTSEDIT10, m_CountsEdit10); DDX_Text(pDX, IDC_COUNTSEDIT11, m_CountsEdit11); DDX_Text(pDX, IDC_COUNTSEDIT12, m_CountsEdit12); DDX_Text(pDX, IDC_COUNTSEDIT13, m_CountsEdit13); DDX_Text(pDX, IDC_COUNTSEDIT14, m_CountsEdit14); DDX_Text(pDX, IDC_COUNTSEDIT15, m_CountsEdit15); DDX_Text(pDX, IDC_COUNTSEDIT2, m_CountsEdit2); DDX_Text(pDX, IDC_COUNTSEDIT3, m_CountsEdit3); DDX_Text(pDX, IDC_COUNTSEDIT4, m_CountsEdit4); DDX_Text(pDX, IDC_COUNTSEDIT5, m_CountsEdit5); DDX_Text(pDX, IDC_COUNTSEDIT6, m_CountsEdit6); DDX_Text(pDX, IDC_COUNTSEDIT7, m_CountsEdit7); DDX_Text(pDX, IDC_COUNTSEDIT8, m_CountsEdit8); DDX_Text(pDX, IDC_COUNTSEDIT9, m_CountsEdit9); DDX_Text(pDX, IDC_VOLTSEDIT0, m_VoltsEdit0); DDX_Text(pDX, IDC_VOLTSEDIT1, m_VoltsEdit1); DDX_Text(pDX, IDC_VOLTSEDIT10, m_VoltsEdit10); DDX_Text(pDX, IDC_VOLTSEDIT11, m_VoltsEdit11); DDX_Text(pDX, IDC_VOLTSEDIT12, m_VoltsEdit12); DDX_Text(pDX, IDC_VOLTSEDIT13, m_VoltsEdit13); DDX_Text(pDX, IDC_VOLTSEDIT14, m_VoltsEdit14); DDX_Text(pDX, IDC_VOLTSEDIT15, m_VoltsEdit15); DDX_Text(pDX, IDC_VOLTSEDIT2, m_VoltsEdit2); DDX_Text(pDX, IDC_VOLTSEDIT3, m_VoltsEdit3); DDX_Text(pDX, IDC_VOLTSEDIT4, m_VoltsEdit4); DDX_Text(pDX, IDC_VOLTSEDIT5, m_VoltsEdit5); DDX_Text(pDX, IDC_VOLTSEDIT6, m_VoltsEdit6); DDX_Text(pDX, IDC_VOLTSEDIT7, m_VoltsEdit7); DDX_Text(pDX, IDC_VOLTSEDIT8, m_VoltsEdit8); DDX_Text(pDX, IDC_VOLTSEDIT9, m_VoltsEdit9); //}}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[0]; 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); }