电子文章 | 电子资料下载 | 家电维修 | 维修资料下载 | 加入收藏 | 全站地图
您现在所在位置:电子爱好者电子文章电子基础知识并口模拟IIC的经验之谈

并口模拟IIC的经验之谈

08-09 23:01:54 | http://www.5idzw.com | 电子基础知识 | 人气:319
标签:电子基础,电子基础知识应用,电工电子技术基础,http://www.5idzw.com 并口模拟IIC的经验之谈,http://www.5idzw.com
}
/*************************************/
//函数名称:主机(并口)的发送应答
/*************************************/
void port2iicbase::i2c_ask()
{
SetPortVal(PORT1,0x05,1);/*scl 0, sda 0*/
delay(1);/**/
SetPortVal(PORT1,0x0d,1);/*scl 1, sda 0*/
delay(1);
SetPortVal(PORT1,0x05,1);/*scl 0, sda 0*/
delay(1);
}

//////////////////////////////////
//发送数据按钮
/////////////////////////////////
void Cport2iicdlg1::OnWriteok()
{
// TOD Add your control notification handler code here
port2iicbase port2iic;
char s[100];
char temp[3];
int len;
int i;
char m_cValue;
unsigned int WriValue=0;
int time = 0;

unsigned short c;
char d,e;

#ifdef _PORT2IIC
SetDlgItemText(IDC_WRITE_STATUS,"正在发送....");
GetDlgItem(IDC_EDIT_WRITE)->GetWindowText(s, 100);
len = strlen(s);
c = len/2;
e='\x0';
d='\x0';
i = 0;
c = 5;
//写一页
//启动开始信号;
port2iic.i2c_start();
//发送控制信息
//"\xa0" = 1010(A2)(A1)(A0)(R/W) 其中(R/w) = 0 写
//"\xa1" = 1010(A2)(A1)(A0)(R/W) 其中(R/w) = 1 读
port2iic.i2c_writebyte('\xa0');
//发送地址
port2iic.i2c_writebyte(e);
while(c>0)
{
   temp[0] = s[i];
   temp[1] = s[i+1];
   temp[2] = 0x0;
   sscanf(temp,"%x",&WriValue);
   m_cValue = (char)WriValue;
   port2iic.i2c_writebyte(m_cValue);
   c = c-1;
   i+=2;
}
port2iic.i2c_stop();
#if 0
//24c01 写128个字节,调试写16个字;
c=20;
e='\x0';
d='\x0';
while(c>0)
{
   //启动开始信号;
   port2iic.i2c_start();
   //发送控制信息
   //"\xa0" = 1010(A2)(A1)(A0)(R/W) 其中(R/w) = 0 写
   //"\xa1" = 1010(A2)(A1)(A0)(R/W) 其中(R/w) = 1 读
   port2iic.i2c_writebyte('\xa0');
   //发送地址
   port2iic.i2c_writebyte(e);
   port2iic.i2c_writebyte(d);
   port2iic.i2c_stop();
   d = c/5;
   c = c-1;
   e = e+1;
}
Sleep(50);
//任意读;
//读写入的数据
b = 0;
i = 0;
a = 20;
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();
   buff[i] = d;
   i++;
   b= b+1;
   a = a - 1;
}
#endif
#else
len = strlen(s);
for(i = 0; i < len; i+=2)
{
   temp[0] = s[i];
   temp[1] = s[i+1];
   temp[2] = 0x0;
   sscanf(temp,"%x",&WriValue);
   m_nValue = (DWORD)WriValue;
   SetPortVal(0x378,m_nValue,1);
}
#endif
SetDlgItemText(IDC_WRITE_STATUS,"发送结束");
return;
}

//////////////////////////////////
//在定时器里接收数据按钮
/////////////////////////////////
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;
}

    以上程序已经经过了验证能够正确运行。

上一页  [1] [2] [3] 

,并口模拟IIC的经验之谈
关于《并口模拟IIC的经验之谈》的更多文章