📄 sample1dlg.cpp
字号:
// Sample1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Sample1.h"
#include "Sample1Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSample1Dlg dialog
CSample1Dlg::CSample1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CSample1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSample1Dlg)
m_DeviceAddr = _T("");
m_RegAddr = _T("");
m_Value = _T("");
m_Infor = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSample1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSample1Dlg)
DDX_Text(pDX, IDC_EDIT_DEVICE_ADDR, m_DeviceAddr);
DDX_Text(pDX, IDC_EDIT_REG_ADDR, m_RegAddr);
DDX_Text(pDX, IDC_EDIT_VALUE, m_Value);
DDX_Text(pDX, IDC_EDIT_INFOR, m_Infor);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSample1Dlg, CDialog)
//{{AFX_MSG_MAP(CSample1Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_EDIT_DEVICE_ADDR, OnChangeEditDeviceAddr)
ON_EN_CHANGE(IDC_EDIT_REG_ADDR, OnChangeEditRegAddr)
ON_EN_CHANGE(IDC_EDIT_VALUE, OnChangeEditValue)
ON_EN_CHANGE(IDC_EDIT_INFOR, OnChangeEditInfro)
ON_BN_CLICKED(IDC_BUTTON_READ, OnButtonRead)
ON_BN_CLICKED(IDC_BUTTON_WRITE, OnButtonWrite)
ON_BN_CLICKED(IDC_REMOVE, OnRemove)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSample1Dlg message handlers
BOOL CSample1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// TODO: Add extra initialization here
InitializeWinIo();
return TRUE; // return TRUE unless you set the focus to a control
}
void CSample1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CSample1Dlg::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 CSample1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSample1Dlg::OnChangeEditDeviceAddr()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(1);
if(!(((m_DeviceAddr.Right(1)>='0')&&(m_DeviceAddr.Right(1)<='9'))||((m_DeviceAddr.Right(1)>='a')&&(m_DeviceAddr.Right(1)<='f'))||((m_DeviceAddr.Right(1)>='A')&&(m_DeviceAddr.Right(1)<='F'))))
{
if(m_DeviceAddr.GetLength()==0)
return;
m_DeviceAddr.SetAt(m_DeviceAddr.GetLength()-1,NULL);
MessageBox("Please input a Hex data");
UpdateData(0);
}
}
void CSample1Dlg::OnChangeEditRegAddr()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(1);
if(!(((m_RegAddr.Right(1)>='0')&&(m_RegAddr.Right(1)<='9'))||((m_RegAddr.Right(1)>='a')&&(m_RegAddr.Right(1)<='f'))||((m_RegAddr.Right(1)>='A')&&(m_RegAddr.Right(1)<='F'))))
{
if(m_RegAddr.GetLength()==0)
return;
m_RegAddr.SetAt(m_RegAddr.GetLength()-1,NULL);
MessageBox("Please input a Hex data");
UpdateData(0);
}
}
void CSample1Dlg::OnChangeEditValue()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
UpdateData(1);
if(!(((m_Value.Right(1)>='0')&&(m_Value.Right(1)<='9'))||((m_Value.Right(1)>='a')&&(m_Value.Right(1)<='f'))||((m_Value.Right(1)>='A')&&(m_Value.Right(1)<='F'))))
{
if(m_Value.GetLength()==0)
return;
m_Value.SetAt(m_Value.GetLength()-1,NULL);
MessageBox("Please input a Hex data");
UpdateData(0);
}
}
void CSample1Dlg::OnChangeEditInfro()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
void CSample1Dlg::OnButtonRead()
{
// TODO: Add your control notification handler code here
unsigned int ideviceaddress, iregaddress;
unsigned char *preceivevalue;
int ireceivedata = 0;
int i,j=0;
/*
bool k;
UpdateData(1);
k=SetPortVal(0x378, 0xff, 1);
if(k==false)
MessageBox("aaaaaa");
UpdateData(0);
*/
UpdateData(1);
if((m_DeviceAddr.IsEmpty())||(m_RegAddr.IsEmpty()))
{
MessageBox("Please input the device and register addr.");
return;
}
preceivevalue = new unsigned char[DAT_LENGTH];
sscanf(m_DeviceAddr.GetBuffer(m_DeviceAddr.GetLength()), "%x", &ideviceaddress);
sscanf(m_RegAddr.GetBuffer(m_RegAddr.GetLength()), "%x", &iregaddress);
j=receive_data(ideviceaddress, iregaddress, DAT_LENGTH, preceivevalue);
if(j==0)
{
for(i=0; i<DAT_LENGTH; i++)
ireceivedata = ireceivedata | (preceivevalue[i])<<8*(DAT_LENGTH-1-i);
m_Value.Format("%x",ireceivedata);
m_Infor="Read Data Successful!";
}
else
{
m_Infor="Read Data Failed!";
}
UpdateData(0);
}
void CSample1Dlg::OnButtonWrite()
{
// TODO: Add your control notification handler code here
char *psendvalue;
int ideviceaddress, iregaddress;
int isenddata;
int i,j;
/*
DWORD temp_value = 0;
CString k;
GetPortVal(0x379, &temp_value, 1);
k.Format("the temp value: %x",temp_value);
MessageBox(k);
//SetPortVal(0x378, 0x02, 1);
// TODO: Add your control notification handler code here
*/
UpdateData(1);
if((m_DeviceAddr.IsEmpty())||(m_RegAddr.IsEmpty())||(m_Value.IsEmpty()))
{
MessageBox("Please input the device and register addr. also the value");
return;
}
psendvalue = new char[DAT_LENGTH];
sscanf(m_DeviceAddr.GetBuffer(m_DeviceAddr.GetLength()), "%x", &ideviceaddress);
sscanf(m_RegAddr.GetBuffer(m_RegAddr.GetLength()), "%x", &iregaddress);
sscanf(m_Value.GetBuffer(m_Value.GetLength()), "%x", &isenddata);
for(i=0; i<DAT_LENGTH; i++)
psendvalue[i] = char(isenddata>>(DAT_LENGTH-1-i)*8);
j=send_data(ideviceaddress, iregaddress, DAT_LENGTH, psendvalue);
if(j==0)
m_Infor="Write Data Successful!";
else
m_Infor="Write Data Failed!";
UpdateData(0);
}
void CSample1Dlg::myDelay(DWORD dvalue)
{
unsigned int i;
for (i=0; i<2000*dvalue; i++);
}
void CSample1Dlg::Write_SCL(BYTE value)
{
DWORD temp_value = 0;
GetPortVal(PORT_SCL, &temp_value, 1);
temp_value = temp_value & 0xFE;
/*
if(value)//reverse it
value=0x0;
else
value=0x01;
*/
temp_value = temp_value | ((value & 0x01)<<SCL_PIN);
SetPortVal(PORT_SCL, temp_value, 1);
}
void CSample1Dlg::Write_SDA(BYTE value)
{
DWORD temp_value = 0;
GetPortVal(PORT_OUT, &temp_value, 1);
temp_value = temp_value & 0xFD;
/*
if(value)//reverse it
value=0x0;
else
value=0x01;
*/
temp_value = temp_value | ((value & 0x01)<<SDA_OUT);
SetPortVal(PORT_OUT, temp_value, 1);
}
void CSample1Dlg::Get_SDA(DWORD *pvalue)
{
BYTE i;
DWORD temp_value = 0;
//pre set output 0
Write_SDA(0x01);
for(i=0;i<=20;i++)
;
GetPortVal(PORT_IN, &temp_value, 1);
*pvalue = (temp_value & 0x10)>>SDA_IN;
// Write_SDA(0x00);
}
void CSample1Dlg::I2C_stop()
{
//scl = 0, sda = 0
Write_SCL(0);
Write_SDA(SDA_ZERO);
myDelay(1);
//scl = 1, sda = 0
Write_SCL(1);
Write_SDA(SDA_ZERO);
myDelay(1);
//scl = 1, sda = 1
Write_SCL(1);
Write_SDA(SDA_ONE);
myDelay(1);
//scl = 1, sda = 1
Write_SCL(1);
Write_SDA(SDA_ONE);
myDelay(1);
}
void CSample1Dlg::I2C_start()
{
//scl = 0, sda = 1
Write_SCL(0);
Write_SDA(SDA_ONE);
myDelay(1);
//scl = 1, sda = 1
Write_SCL(1);
Write_SDA(SDA_ONE);
myDelay(1);
//scl = 1, sda = 0
Write_SCL(1);
Write_SDA(SDA_ZERO);
myDelay(1);
//scl = 0, sda = 0
Write_SCL(0);
Write_SDA(SDA_ZERO);
myDelay(1);
}
void CSample1Dlg::Send_noanswer()
{
//scl = 0, sda =1
Write_SCL(0);
Write_SDA(SDA_ONE);
myDelay(1);
//scl = 1, sda = 1
Write_SCL(1);
Write_SDA(SDA_ONE);
myDelay(1);
//scl = 1, sda = 1
Write_SCL(1);
Write_SDA(SDA_ONE);
myDelay(1);
//scl = 0, sda = 1
Write_SCL(0);
Write_SDA(SDA_ONE);
myDelay(1);
}
void CSample1Dlg::Send_answer()
{
//scl = 0, sda = 0
Write_SCL(0);
Write_SDA(SDA_ZERO);
myDelay(1);
//scl = 1, sda = 0
Write_SCL(1);
Write_SDA(SDA_ZERO);
myDelay(1);
//scl = 1, sda = 0
Write_SCL(1);
Write_SDA(SDA_ZERO);
myDelay(1);
//scl = 0, sda = 1
Write_SCL(0);
Write_SDA(SDA_ONE);
myDelay(1);
}
bool CSample1Dlg::Check_answer()
{
DWORD read_value;
//scl = 0
Write_SCL(0);
myDelay(1);
//scl = 1, read ack
Write_SCL(1);
Get_SDA(&read_value);
myDelay(1);
//scl = 1
Write_SCL(1);
myDelay(1);
//scl = 0
Write_SCL(0);
myDelay(1);
if ((read_value & SDA_ONE) ==0)
return true;
return false;
}
void CSample1Dlg::Send_Byte(int send_value)
{
int i;
for(i=8; i>0; i--)
{
//scl = 0
Write_SCL(0);
Write_SDA(send_value>>(i-1));
myDelay(1);
//scl = 1, send data
Write_SCL(1);
myDelay(1);
//scl = 1
Write_SCL(1);
myDelay(1);
//scl = 0
Write_SCL(0);
myDelay(1);
}
Write_SDA(SDA_ONE);
}
DWORD CSample1Dlg::Receive_Byte()
{
DWORD temp;
DWORD receive_value = 0;
int i;
//CString k;
for(i=8; i>0; i--)
{
//scl = 0
Write_SCL(0);
myDelay(1);
//scl = 1, receive data
Write_SCL(1);
Get_SDA(&temp);
receive_value = receive_value | ((temp & SDA_ONE)<<(i-1));
myDelay(1);
//scl = 1
Write_SCL(1);
myDelay(1);
//scl = 0
Write_SCL(0);
myDelay(1);
}
//k.Format("%x",receive_value);
//MessageBox(k);
return receive_value;
}
int CSample1Dlg::send_data(int address, int regaddress, int length, char *pbuff)
{
int i;
bool answer_check;
I2C_start();
Send_Byte(address & 0xfe);
answer_check = Check_answer();
if (!answer_check)
{
I2C_stop();
return -1;
}
Send_Byte(regaddress);
answer_check = Check_answer();
if (!answer_check)
{
I2C_stop();
return -1;
}
for (i=0; i<length; i++)
{
Send_Byte(pbuff[i]);
answer_check = Check_answer();
if (!answer_check)
{
I2C_stop();
return -1;
}
}
I2C_stop();
return 0;
}
int CSample1Dlg::receive_data(int address, int regaddress, int length, unsigned char *pbuff)
{
int i;
bool answer_check;
I2C_start();
Send_Byte((address & 0xfe));
answer_check = Check_answer();
if (!answer_check)
{
I2C_stop();
return -1;
}
Send_Byte(regaddress);
answer_check = Check_answer();
if (!answer_check)
{
I2C_stop();
return -1;
}
I2C_stop();
I2C_start();
Send_Byte(address | 0x01);
answer_check = Check_answer();
if (!answer_check)
{
I2C_stop();
return -1;
}
for (i=0; i<length; i++)
{
pbuff[i] = unsigned char(Receive_Byte());
if( i==length-1)
Send_noanswer();
else
Send_answer();
}
// Send_noanswer();
I2C_stop();
return 0;
}
void CSample1Dlg::OnRemove()
{
// TODO: Add your control notification handler code here
ShutdownWinIo();
//RemoveWinIoDriver();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -