// DACDlg.cpp : implementation file // #include "stdafx.h" #include "DAC.h" #include "DACDlg.h" #include "ACCES32.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define Bits 12 #define MyKey "Software\\PCIFind\\NTioPCI\\Parameters" //"System\\CurrentControlSet\\Services\\NTioPCI\\Parameters" #define InsCR(x) x += 0x0d; x += 0x0a; #define MaxCh 2 double Span = 10.0, Offset = 5; TPCI_COMMON_CONFIG buf[64]; unsigned short WhichAddress[64][2]; int NumChannels[64]; bool RunFlag; unsigned short CalibMax[7][16], CalibMin[7][16]; ///////////////////////////////////////////////////////////////////////////// // CDACDlg dialog CDACDlg::CDACDlg(CWnd* pParent /*=NULL*/) : CDialog(CDACDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDACDlg) m_Output = _T("DACs update as you move the slider"); m_PciAddress = _T(""); m_Range = 0; m_Channel = 0; m_Status = _T(""); m_BaseAddress = _T("300"); m_ValueSlider = 0; //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDACDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDACDlg) DDX_Control(pDX, IDC_SLIDER, m_ValueSliderCtrl); DDX_Control(pDX, IDC_STATUS, m_StatusControl); DDX_Control(pDX, IDC_ISASTATIC, m_IsaStatic); DDX_Text(pDX, IDC_OUTPUT, m_Output); DDX_Radio(pDX, IDC_RANGE1, m_Range); DDX_Radio(pDX, IDC_CHANNEL0, m_Channel); DDX_Text(pDX, IDC_STATUS, m_Status); DDX_Text(pDX, IDC_ISAEDIT, m_BaseAddress); DDX_Slider(pDX, IDC_SLIDER, m_ValueSlider); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDACDlg, CDialog) //{{AFX_MSG_MAP(CDACDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDOK, OnInit) ON_BN_CLICKED(IDCANCEL, OnExit) ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER, OnSliderChange) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDACDlg message handlers BOOL CDACDlg::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 CDACDlg::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 CDACDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CDACDlg::OnExit() { exit(0); } void CDACDlg::OnInit() { unsigned data = 0; CurrentValue = 0; m_ValueSliderCtrl.SetRange(0, 4095, true); m_ValueSliderCtrl.SetTicFreq(1024); UpdateData(true); UpdateData(false); sscanf(m_BaseAddress, "%x", &data); if (data > 0x100) { BaseAddress=data; RunFlag=1; } if (RunFlag) m_ValueSliderCtrl.ShowWindow(SW_SHOW); } unsigned CDACDlg::ConvertForOutput(unsigned v) { if (m_Channel < MaxCh) OutPort(BaseAddress + (m_Channel * 2), v * 16); else { for (int i = 0; i < MaxCh; i++) OutPort(BaseAddress + (i * 2) + 4, v * 16); } return v; } CString CDACDlg::IntToBin(unsigned input) { CString temp; unsigned i; double f; char text[255]; UpdateData(true); temp = ""; for (i = Bits; i >= 1; i--) temp += (input & (1 << (i - 1)))?'1':'0'; temp += "xxxx "; f = input * (Span / (1 << Bits)) - Offset; sprintf(text, "%.*X", (Bits / 4), input); temp += text; sprintf(text, "%8.3f", f); temp += text; temp += (m_Range == 4) ? " mA" : " Volts"; return temp; } void CDACDlg::UpdateDAC() { switch(m_Range) { case 0: Offset = 10.0; Span = 20.0; break; case 1: Offset = 5.0; Span = 10.0; break; case 2: Offset = 2.5; Span = 5.0; break; case 3: Offset = 0.0; Span = 10.0; break; case 4: Offset = 0.0; Span = 5.0; break; case 5: Offset = 0.0; Span = 2.5; break; case 6: Offset = -4.0; Span = 16.0; break; } m_Output = IntToBin(ConvertForOutput(CurrentValue)); UpdateData(false); } void Delay(unsigned Duration) { unsigned long NewTicks; NewTicks = GetTickCount() + Duration; while (GetTickCount() < NewTicks); } void CDACDlg::OnSliderChange(NMHDR* pNMHDR, LRESULT* pResult) { UpdateData(true); CurrentValue = m_ValueSlider; UpdateDAC(); *pResult = 0; }