|
void DLGADC::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
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);
}
void DLGADC::OnButtonStartAdc()
{
// TODO: Add your control notification handler code here
CButton *Key;
Key = (CButton *)GetDlgItem(IDC_BUTTON_START_ADC);
UINT state = Key->GetState();
if ((state & 0x0003) == 1) /* 按键按下 */
{
if(IDYES==(AfxMessageBox(_T("启动ADC之后将无法使用触模屏,是否继续!"),MB_YESNO)))
{
// Yes
if (hFile == INVALID_HANDLE_VALUE)
{
hFile = CreateFile(TEXT("ADC1:"), GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(_T("ADC 驱动打开失败!"));
return;
}
}
Key->SetWindowText(_T("停止采样")); /* 按键标题改变, 提示下次操作 */
SetTimer(1, 20, NULL); /* 启动定时器, 进行采样 */
}
else
{
Key->SetCheck(0); /* 按键保持为未按下 */
UpdateData(FALSE);
}
}
else /* 按键弹起 */
{
Key->SetWindowText(_T("开始采样")); /* 按键标题复原 */
Key->SetCheck(0); /* 按键保持为未按下 */
KillTimer(1); /* 取消定时 */
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
}
|
|