// Sample 0Dlg.cpp : implementation file // #include "stdafx.h" #include "Sample 0.h" #include "Sample 0Dlg.h" #include "ACCES32.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define InsCR(x) x += 0x0d; x += 0x0a; int base = 0x300; ///////////////////////////////////////////////////////////////////////////// // CSample0Dlg dialog CSample0Dlg::CSample0Dlg(CWnd* pParent /*=NULL*/) : CDialog(CSample0Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CSample0Dlg) m_BaseEdit = _T("300"); m_Instructions = _T("Sample 0"); m_CardTitle = _T("AD12-16"); m_ChannelLabel = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSample0Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSample0Dlg) DDX_Control(pDX, IDOK, m_TestButton); DDX_Text(pDX, IDC_BASE_EDIT, m_BaseEdit); DDX_Text(pDX, IDC_INSTRUCT_TEXT, m_Instructions); DDX_Text(pDX, IDC_TITLE_LABEL, m_CardTitle); DDX_Text(pDX, IDC_ChannelLabel0, m_ChannelLabel); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSample0Dlg, CDialog) //{{AFX_MSG_MAP(CSample0Dlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_TIMER() ON_BN_DOUBLECLICKED(IDCANCEL, OnExitButtonClicked) ON_BN_CLICKED(IDOK, OnTestButtonClicked) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSample0Dlg message handlers BOOL CSample0Dlg::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 InsCR(m_Instructions); m_Instructions += _T("This sample reads the sixteen analog inputs and displays them for you."); InsCR(m_Instructions); m_Instructions += _T("Board Configuration Requirements:"); InsCR(m_Instructions); m_Instructions += _T("RANGE: 5 Volt Range"); InsCR(m_Instructions); m_Instructions += _T("POLARITY: Bipolar"); InsCR(m_Instructions); m_Instructions += _T("Please enter the base address (in hex) in the text box below, and click the 'Start Test' button to begin."); CString ThisLabel; int i = 0; for (i = 0; i <= 15; i++) { ThisLabel.Format("Channel %1X", i); m_ChannelLabel += ThisLabel; InsCR(m_ChannelLabel); } 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); } UpdateData(false); // 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 CSample0Dlg::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 CSample0Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSample0Dlg::OnTimer(UINT nIDEvent) { int channel, counts, i; double volts; CString ThisLabel = ""; sscanf(m_BaseEdit.GetBuffer(0), "%x", &base); OutPortB(base+2, 0xF0); // Set scan from Ch0 to Ch15 do { OutPortB(base, 0); // Start A/D conversion i = 0; // Wait for EOC or timeout while (((InPortB(base+8) & 0x80) == 0x80) & (i < 32000)) i++; if (i >= 32000) { // if timeout, display error & quit m_ChannelLabel.Format("Timeout on channel %1X", InPortB(base) & 0x0F); UpdateData(false); break; } // end if channel = InPortB(base) & 0x0F; // Get channel number counts = InPort(base) >> 4; // Get channel counts // Scaling 5v range on 12 bit device = 0.00244v/count volts = (counts-2048)*0.00244; // Display results if (channel == 0) m_ChannelLabel.Format("Channel %1X %d %g",channel,counts,volts); else { ThisLabel.Format("Channel %1X %d %g",channel,counts,volts); InsCR(m_ChannelLabel); m_ChannelLabel += ThisLabel; } // end else } while ((InPort(base) & 0x0F) != 0x0F); // end do-while UpdateData(false); CDialog::OnTimer(nIDEvent); } void CSample0Dlg::OnExitButtonClicked() { exit(0); } void CSample0Dlg::OnTestButtonClicked() { static bool TimerEnabled = false; if (TimerEnabled) { TimerEnabled = false; int i = 0; CString ThisLabel = ""; m_ChannelLabel.Empty(); m_TestButton.SetWindowText("Start Test"); for (i = 0; i <= 15; i++) { ThisLabel.Format("Channel %1X 0 0", i); m_ChannelLabel += ThisLabel; InsCR(m_ChannelLabel); } UpdateData(false); KillTimer(0); } else { TimerEnabled = true; m_TestButton.SetWindowText("Abort Test"); SetTimer(0, 100, NULL); } } void CSample0Dlg::OnShowWindow(BOOL bShow, UINT nStatus) { CDialog::OnShowWindow(bShow, nStatus); }