并口模拟IIC的经验之谈
//////////////////////////////////
//在定时器里接收数据按钮
/////////////////////////////////
void Cport2iicdlg1::OnTimer(UINT nIDEvent)
{
// TOD Add your message handler code here and/or call default
port2iicbase port2iic;
DWORD m_nValue = 0;
CString temp1,temp2;
CEdit* pReadEdit;
unsigned short buff[100];
unsigned short a,b;
char d;
int i;
pReadEdit = (CEdit*) GetDlgItem(IDC_EDIT_READ);
memset(buff,0x0,100);
#ifdef _PORT2IIC
//任意读;
#if 0
//读写入的数据
b = 0;
a = 5;
i = 0;
while(a > 0)
{
//任意读;
port2iic.i2c_start();
port2iic.i2c_writebyte('\xa0');
d = char(b);
port2iic.i2c_writebyte(d);
port2iic.i2c_start();
port2iic.i2c_writebyte('\xa1');
d = port2iic.i2c_readbyte();
port2iic.i2c_stop();
b= b+1;
a = a - 1;
buff[i] = (unsigned short)d;
buff[i] = buff [i] 0x00ff;
temp1 = " ";
temp2.Format(_T("%.2x"),buff[i]);
if(m_read != _T(""))
m_read = m_read + temp1;
m_read = m_read + temp2;
i++;
SetDlgItemText(IDC_EDIT_READ,m_read);
DWORD dwSel = pReadEdit->GetSel();
pReadEdit->SetSel(HIWORD(dwSel), -1);
}
#endif
#if 1
//顺序读
//读写入的数据
b = 0;
a = 5;
i = 0;
port2iic.i2c_start();
port2iic.i2c_writebyte('\xa0');
d = char(b);
port2iic.i2c_writebyte(d);
port2iic.i2c_start();
port2iic.i2c_writebyte('\xa1');
while(a > 0)
{
d = port2iic.i2c_readbyte();
a = a - 1;
if(a > 0)
port2iic.i2c_ask();
buff[i] = (unsigned short)d;
buff[i] = buff [i] 0x00ff;
temp1 = " ";
temp2.Format(_T("%.2x"),buff[i]);
if(m_read != _T(""))
m_read = m_read + temp1;
m_read = m_read + temp2;
i++;
SetDlgItemText(IDC_EDIT_READ,m_read);
DWORD dwSel = pReadEdit->GetSel();
pReadEdit->SetSel(HIWORD(dwSel), -1);
}
port2iic.i2c_stop();
#endif
#else
GetPortVal(0x378, m_nValue,1);
m_nValue = m_nValue 0x000000ff;
temp1 = " ";
temp2.Format(_T("%.2x"),m_nValue);
if(m_read != _T(""))
m_read = m_read + temp1;
m_read = m_read + temp2;
SetDlgItemText(IDC_EDIT_READ,m_read);
DWORD dwSel = pReadEdit->GetSel();
pReadEdit->SetSel(HIWORD(dwSel), -1);
#endif
CDialog::OnTimer(nIDEvent);
return;
}
以上程序已经经过了验证能够正确运行。
模拟IIC 相关文章:
- 8051模拟IIC操作EEPROM(12-03)
- S3C2440模拟IIC方式操作EEPROM(12-03)
- STM32使用模拟IIC操作EEPROM(12-03)
- MSP430非模拟IIC总线控制程序(12-02)
- STM32 模拟IIC协议驱动TEA5767(12-02)
- msp430单片机模拟IIC总线读取MMA7660三轴加速度传感器(11-27)