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

📄 i2cdemodlg.cpp

📁 evc开发的i2c测试测试程序 用于wince平台
💻 CPP
字号:
// I2CDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "I2CDemo.h"
#include "I2CDemoDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CI2CDemoDlg dialog

CI2CDemoDlg::CI2CDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CI2CDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CI2CDemoDlg)
	m_command = _T("");
	m_control = _T("");
	m_lsb_sensor = _T("");
	m_lsb_timer = _T("");
	m_msb_sensor = _T("");
	m_msb_timer = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CI2CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CI2CDemoDlg)
	DDX_Text(pDX, IDC_COMMAND, m_command);
	DDX_Text(pDX, IDC_CONTROL, m_control);
	DDX_Text(pDX, IDC_LSB_SENSOR, m_lsb_sensor);
	DDX_Text(pDX, IDC_LSB_TIMER, m_lsb_timer);
	DDX_Text(pDX, IDC_MSB_SENSOR, m_msb_sensor);
	DDX_Text(pDX, IDC_MSB_TIMER, m_msb_timer);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CI2CDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CI2CDemoDlg)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CI2CDemoDlg message handlers

BOOL CI2CDemoDlg::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
}



void CI2CDemoDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	
}
CString UCHAR2ASCII( UCHAR tmp)
{
	CString strtmp="00000000";
	for( int i=7; i>=0; i--)
	{
		if(tmp!='0')
		{
			if(tmp%2==0)
			{
				strtmp.SetAt(i,'0');
			}
			else
			{
				strtmp.SetAt(i, '1');
			}
			tmp=tmp/2;
		}
		else
			strtmp.SetAt(i,'0');
		
	}
	return strtmp;
}

void CI2CDemoDlg::OnStart() 
{
	DWORD dwWritten;
	DWORD dwRead;
	// TODO: Add your control notification handler code here
	//L"I2C1:",GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
	hDrv=CreateFile(L"I2C1:",GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if(INVALID_HANDLE_VALUE==hDrv)
	{
		OutputDebugString(L"Failed to open Driver...\r\n");
		return;
	}
	UpdateData(true);
	UCHAR writeBuf[2];
	
	UCHAR readBuf[6];
	//writeBuf[0]=m_command;
	//writeBuf[1]=m_control;
	int i,j;
	
	j=m_command.GetLength();
	if(j!=8)
	{
		AfxMessageBox(L"The command is bigger than 8 bit\n");
		return;
	}
	UCHAR temp = 0x00;
	for(i=0;i<j;i++)
	{
		if(m_command[i]=='1')
			temp |= 0x01;
		if(i<7)
			temp <<=1;

	}
	writeBuf[0] = temp;


	//CString temp;
	//temp.Format("%c",writeBuf[0]);
	//AfxMessageBox(L"writeBuf[0] is:"+writeBuf[0]);
	temp = 0x00;

	j=m_control.GetLength();
	if(j!=8)
	{
		MessageBox(L"The command is bigger than 8 bit\n");
		return;
	}
		for(i=0;i<j;i++)
	{
		if(m_control[i]=='1')
			temp |= 0x01;
		if(i<7)
			temp <<=1;

	}
	//temp >>=1;
	writeBuf[1]=temp;
	//temp.Format("%s",writeBuf[1]);
	//AfxMessageBox(L"writeBuf[1] is:"+writeBuf[1]);

	WriteFile(hDrv,writeBuf,2,&dwWritten,NULL);

	//for(;;)
	//{
		ReadFile(hDrv,readBuf,6,&dwRead,NULL);
		
		m_lsb_sensor=UCHAR2ASCII(readBuf[2]);
		m_msb_sensor=UCHAR2ASCII(readBuf[3]);
		m_lsb_timer=UCHAR2ASCII(readBuf[4]);
		m_msb_timer=UCHAR2ASCII(readBuf[5]);
		UpdateData(false);

	//}
CloseHandle(hDrv);

}

void CI2CDemoDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	CloseHandle(hDrv);
	return;
}

⌨️ 快捷键说明

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