⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dlgadc.cpp

📁 周立功WinCE光盘资料
💻 CPP
字号:
// DLGADC.cpp : implementation file
//

#include "stdafx.h"
#include "Magic2410.h"
#include "DLGADC.h"
#include "ADC.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// DLGADC dialog


DLGADC::DLGADC(CWnd* pParent /*=NULL*/)
	: CDialog(DLGADC::IDD, pParent)
{
	//{{AFX_DATA_INIT(DLGADC)
	m_DispAIN0 = 0;
	m_DispAIN1 = 0;
	//}}AFX_DATA_INIT

	hFile = INVALID_HANDLE_VALUE;
}


void DLGADC::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DLGADC)
	DDX_Text(pDX, IDC_DISP_AIN0, m_DispAIN0);
	DDX_Text(pDX, IDC_DISP_AIN1, m_DispAIN1);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(DLGADC, CDialog)
	//{{AFX_MSG_MAP(DLGADC)
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_START_ADC, OnButtonStartAdc)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DLGADC message handlers

void DLGADC::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	if (hFile != INVALID_HANDLE_VALUE)
	{
		// 关闭 ADC 驱动
		CloseHandle(hFile);
		hFile = INVALID_HANDLE_VALUE;
	}
	KillTimer(1);							/* 取消定时 */
}

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;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -