📄 adcsampledlg.cpp
字号:
// ADCSampleDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ADCSample.h"
#include "ADCSampleDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CADCSampleDlg dialog
CADCSampleDlg::CADCSampleDlg(CWnd* pParent /*=NULL*/)
: CDialog(CADCSampleDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CADCSampleDlg)
m_DispAIN0 = 0;
m_DispAIN1 = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CADCSampleDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CADCSampleDlg)
DDX_Text(pDX, IDC_DISP_AIN0, m_DispAIN0);
DDX_Text(pDX, IDC_DISP_AIN1, m_DispAIN1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CADCSampleDlg, CDialog)
//{{AFX_MSG_MAP(CADCSampleDlg)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_OPEN_ADC, OnOpenAdc)
ON_BN_CLICKED(IDC_CLOSE_ADC, OnCloseAdc)
ON_BN_CLICKED(IDC_SAMPLE, OnSample)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CADCSampleDlg message handlers
BOOL CADCSampleDlg::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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
/***************************************************
实 验 代 码
****************************************************/
#include "adc.h" /* 包含 ADC 命令代码头文件 */
HANDLE hFile = INVALID_HANDLE_VALUE; /* 驱动程序设备句柄 */
// "打开驱动" 按键单击事件代码
void CADCSampleDlg::OnOpenAdc()
{
// 打开 ADC 驱动
hFile = CreateFile(TEXT("ADC1:"), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(_T("打开 ADC 驱动失败!"));
return;
}
else
MessageBox(_T("打开 ADC 驱动成功!!"));
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_ADC); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_ADC);
pOpenButton->EnableWindow(FALSE); /* 禁止按键 */
pCloseButton->EnableWindow(TRUE); /* 使能按键 */
}
// "关闭驱动" 按键单击事件代码
void CADCSampleDlg::OnCloseAdc()
{
if (hFile != INVALID_HANDLE_VALUE)
{
// 关闭 ADC 驱动
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
CButton *Key;
Key = (CButton *)GetDlgItem(IDC_SAMPLE);
if ((Key->GetState() & 0x0003) == 1) /* 按键按下 */
{
Key->SetCheck(0);
Key->SetWindowText(_T("开始采样")); /* 按键标题复原 */
KillTimer(1); /* 取消定时 */
}
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_ADC); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_ADC);
pOpenButton->EnableWindow(TRUE); /* 使能按键 */
pCloseButton->EnableWindow(FALSE); /* 禁止按键 */
}
// 对话框关闭退出处理函数
void CADCSampleDlg::OnDestroy()
{
CDialog::OnDestroy();
OnCloseAdc();
}
// "开始采样" 按键单击事件代码
void CADCSampleDlg::OnSample()
{
CButton *Key;
Key = (CButton *)GetDlgItem(IDC_SAMPLE);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(_T("ADC 驱动未打开!"));
Key->SetWindowText(_T("开始采样")); /* 按键标题保持不变 */
Key->SetCheck(0); /* 按键保持为未按下 */
return;
}
UINT state = Key->GetState();
if ((state & 0x0003) == 1) /* 按键按下 */
{
Key->SetWindowText(_T("停止采样")); /* 按键标题改变, 提示下次操作 */
SetTimer(1, 20, NULL); /* 启动定时器, 进行采样 */
}
else /* 按键弹起 */
{
Key->SetWindowText(_T("开始采样")); /* 按键标题复原 */
KillTimer(1); /* 取消定时 */
}
}
// 定时器服务程序
void CADCSampleDlg::OnTimer(UINT nIDEvent)
{
DWORD actlen;
BOOL ret;
BYTE channel = 0;
float v0,v1;
// 采样 W1(AIN0) 电压值
ret = ::DeviceIoControl(hFile, IOCTL_SET_ADC_CHANNEL,
&channel, 1, NULL, 0, NULL, NULL); /* 设置当前转换通道为0 */
if (ret != TRUE)
{
KillTimer(1);
MessageBox(_T("设置ADC通道0失败 !"));
goto END;
}
ret = ::ReadFile(hFile, &m_DispAIN0, 1, &actlen, NULL); /* 读取通道0采样值 */
if (ret != TRUE)
{
KillTimer(1);
MessageBox(_T("读取ADC通道0失败 !"));
goto END;
}
// 采样 W2(AIN1) 电压值
channel = 1;
ret = ::DeviceIoControl(hFile, IOCTL_SET_ADC_CHANNEL,
&channel, 1, NULL, 0, NULL, NULL); /* 设置当前转换通道为1 */
if (ret != TRUE)
{
KillTimer(1);
MessageBox(_T("设置ADC通道1失败 !"));
goto END;
}
ret = ::ReadFile(hFile, &m_DispAIN1, 1, &actlen, NULL); /* 读取通道1采样值 */
if (ret != TRUE)
{
KillTimer(1);
MessageBox(_T("读取ADC通道1失败 !"));
goto END;
}
v0 = (float)(m_DispAIN0) / 1024; /* 转换为小数值 */
v1 = (float)(m_DispAIN1) / 1024;
m_DispAIN0 = (DWORD)(v0 * 3300); /* 计算出电压值 */
m_DispAIN1 = (DWORD)(v1 * 3300);
UpdateData(FALSE); /* 更新显示 */
END:
CDialog::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -