📄 并口模拟iic的总结 - smithhy的博客.htm
字号:
1, sda 1*/
<BR> delay(1);<BR> SetPortVal(PORT1,0x07,1);/*scl
0, sda 1*/ <BR> delay(1);<BR> }
<BR> else
//传输"0"<BR> {
<BR> SetPortVal(PORT1,0x05,1);/*scl 0, sda
0*/
<BR> delay(1);<BR> SetPortVal(PORT1,0x0d,1);/*scl
1, sda 0*/
<BR> delay(1);<BR> SetPortVal(PORT1,0x05,1);/*scl
0, sda 0*/ <BR> delay(1);<BR> }
<BR> count--;<BR> } <BR> /**ask**/
<BR> SetPortVal(PORT1,0x07,1);/*scl 0, sda 1*/
<BR> delay(1);/***/
<BR> SetPortVal(PORT1,0x0f,1);/*scl 1, sda 1*/
<BR> delay(1);<BR>// SetPortVal(PORT1,0x05,1);/*scl
0, sda 0*/ <BR>// delay(1);/***/ </P>
<P> GetPortVal(PORT2, &dwPortVal, 1);<BR> e =
(char)dwPortVal;<BR> temp=e>>4;
<BR> temp=temp<<7; </P>
<P> if (temp=='\x0') <BR> return 0;
<BR> else <BR> MessageBox(NULL,"Not
Acknowledge!!","发送错误",MB_OKCANCEL);<BR> return 1; <BR>}
<BR>/*************************************/<BR>//函数名称:IIC
读一个字符函数<BR>/*************************************/<BR>char
port2iicbase::i2c_readbyte()<BR>{<BR> unsigned short
count=8; <BR> char d,e,f='\x0'; <BR> DWORD
dwPortVal;</P>
<P> while(count>0)<BR> {
<BR> SetPortVal(PORT1,0x07,1);/*scl 0, sda 1*/
<BR> delay(1);/***/
<BR> SetPortVal(PORT1,0x0f,1);/*scl 1, sda 1*/
<BR> delay(1);</P>
<P> GetPortVal(PORT2, &dwPortVal,
1);<BR> e =
(char)dwPortVal;<BR> d=e>>4;
<BR> d=d<<7; </P>
<P> if(d=='\x80') <BR> d='\x1';
<BR> f=f<<1;
<BR> f=(f+d);
//组合成字节 <BR> count--;<BR> } <BR> return
f; <BR>}
<BR>/*************************************/<BR>//函数名称:主机(并口)的发送应答<BR>/*************************************/<BR>void
port2iicbase::i2c_ask()<BR>{<BR> SetPortVal(PORT1,0x05,1);/*scl
0, sda 0*/ <BR> delay(1);/**/
<BR> SetPortVal(PORT1,0x0d,1);/*scl 1, sda 0*/
<BR> delay(1);<BR> SetPortVal(PORT1,0x05,1);/*scl 0,
sda 0*/ <BR> delay(1);<BR>} </P>
<P>//////////////////////////////////<BR>//发送数据按钮<BR>/////////////////////////////////<BR>void
Cport2iicdlg1::OnWriteok() <BR>{<BR> // TODO: Add your
control notification handler code here<BR> port2iicbase
port2iic;<BR> char s[100];<BR> char
temp[3];<BR> int len;<BR> int i;<BR> char
m_cValue;<BR> unsigned int WriValue=0;<BR> int time
= 0;</P>
<P> unsigned short c;<BR> char d,e;</P>
<P>#ifdef
_PORT2IIC<BR> SetDlgItemText(IDC_WRITE_STATUS,"正在发送....");<BR> GetDlgItem(IDC_EDIT_WRITE)->GetWindowText(s,
100);<BR> len = strlen(s);<BR> c =
len/2;<BR> e='\x0';<BR> d='\x0';<BR> i =
0;<BR> c =
5;<BR> //写一页<BR> //启动开始信号;<BR> port2iic.i2c_start();<BR> //发送控制信息<BR> //"\xa0"
= 1010(A2)(A1)(A0)(R/W) 其中(R/w) = 0 写<BR> //"\xa1" =
1010(A2)(A1)(A0)(R/W) 其中(R/w) = 1
读<BR> port2iic.i2c_writebyte('\xa0');<BR> //发送地址<BR> port2iic.i2c_writebyte(e);<BR> while(c>0)<BR> {<BR> temp[0]
= s[i];<BR> temp[1] =
s[i+1];<BR> temp[2] =
0x0;<BR> sscanf(temp,"%x",&WriValue);<BR> m_cValue
=
(char)WriValue;<BR> port2iic.i2c_writebyte(m_cValue);<BR> c
=
c-1;<BR> i+=2;<BR> }<BR> port2iic.i2c_stop();<BR>#if
0<BR> //24c01
写128个字节,调试写16个字;<BR> c=20;<BR> e='\x0';<BR> d='\x0';<BR> while(c>0)<BR> {<BR> //启动开始信号;<BR> port2iic.i2c_start();<BR> //发送控制信息<BR> //"\xa0"
= 1010(A2)(A1)(A0)(R/W) 其中(R/w) = 0 写<BR> //"\xa1"
= 1010(A2)(A1)(A0)(R/W) 其中(R/w) = 1
读<BR> port2iic.i2c_writebyte('\xa0');<BR> //发送地址<BR> port2iic.i2c_writebyte(e);<BR> port2iic.i2c_writebyte(d);<BR> port2iic.i2c_stop();<BR> d
= c/5;<BR> c = c-1;<BR> e =
e+1;<BR> }<BR> Sleep(50);<BR> //任意读;<BR> //读写入的数据<BR> b
= 0;<BR> i = 0;<BR> a = 20;<BR> while(a >
0)<BR> {<BR> //任意读;<BR> port2iic.i2c_start();<BR> port2iic.i2c_writebyte('\xa0');<BR> d
=
char(b);<BR> port2iic.i2c_writebyte(d);<BR> port2iic.i2c_start();<BR> port2iic.i2c_writebyte('\xa1');<BR> d
=
port2iic.i2c_readbyte();<BR> port2iic.i2c_stop();<BR> buff[i]
= d;<BR> i++;<BR> b=
b+1;<BR> a = a -
1;<BR> }<BR>#endif<BR>#else<BR> len =
strlen(s);<BR> for(i = 0; i < len;
i+=2)<BR> {<BR> temp[0] =
s[i];<BR> temp[1] = s[i+1];<BR> temp[2]
=
0x0;<BR> sscanf(temp,"%x",&WriValue);<BR> m_nValue
=
(DWORD)WriValue;<BR> SetPortVal(0x378,m_nValue,1);<BR> }<BR>#endif<BR> SetDlgItemText(IDC_WRITE_STATUS,"发送结束");<BR> return;<BR>}</P>
<P>//////////////////////////////////<BR>//在定时器里接收数据按钮<BR>/////////////////////////////////<BR>void
Cport2iicdlg1::OnTimer(UINT nIDEvent) <BR>{<BR> // TODO:
Add your message handler code here and/or call
default<BR> port2iicbase port2iic;<BR> DWORD
m_nValue = 0;<BR> CString temp1,temp2;<BR> CEdit*
pReadEdit;<BR> unsigned short buff[100];</P>
<P> unsigned short a,b;<BR> char d;<BR> int
i;<BR> pReadEdit = (CEdit*)
GetDlgItem(IDC_EDIT_READ);<BR> memset(buff,0x0,100);<BR>#ifdef
_PORT2IIC<BR> //任意读;<BR>#if
0<BR> //读写入的数据<BR> b = 0;<BR> a = 5;<BR> i
= 0;<BR> while(a >
0)<BR> {<BR> //任意读;<BR> port2iic.i2c_start();<BR> port2iic.i2c_writebyte('\xa0');<BR> d
=
char(b);<BR> port2iic.i2c_writebyte(d);<BR> port2iic.i2c_start();<BR> port2iic.i2c_writebyte('\xa1');<BR> d
=
port2iic.i2c_readbyte();<BR> port2iic.i2c_stop();<BR> b=
b+1;<BR> a = a - 1;<BR> buff[i] =
(unsigned short)d;<BR> buff[i] = buff [i] &
0x00ff; <BR> temp1 = "
";<BR> temp2.Format(_T("%.2x"),buff[i]);<BR> if(m_read
!= _T(""))<BR> m_read = m_read +
temp1;<BR> m_read = m_read +
temp2;<BR> i++;<BR> SetDlgItemText(IDC_EDIT_READ,m_read);<BR> DWORD
dwSel =
pReadEdit->GetSel();<BR> pReadEdit->SetSel(HIWORD(dwSel),
-1);<BR> }<BR>#endif<BR>#if
1<BR> //顺序读<BR> //读写入的数据<BR> b = 0;<BR> a
= 5;<BR> i =
0;<BR> port2iic.i2c_start();<BR> port2iic.i2c_writebyte('\xa0');<BR> d
=
char(b);<BR> port2iic.i2c_writebyte(d);<BR> port2iic.i2c_start();<BR> port2iic.i2c_writebyte('\xa1');<BR> while(a
> 0)<BR> {<BR> d =
port2iic.i2c_readbyte();<BR> a = a -
1;<BR> if(a >
0)<BR> port2iic.i2c_ask();<BR> buff[i]
= (unsigned short)d;<BR> buff[i] = buff [i] &
0x00ff; <BR> temp1 = "
";<BR> temp2.Format(_T("%.2x"),buff[i]);<BR> if(m_read
!= _T(""))<BR> m_read = m_read +
temp1;<BR> m_read = m_read +
temp2;<BR> i++;<BR> SetDlgItemText(IDC_EDIT_READ,m_read);<BR> DWORD
dwSel =
pReadEdit->GetSel();<BR> pReadEdit->SetSel(HIWORD(dwSel),
-1);<BR> }<BR> port2iic.i2c_stop();<BR>#endif<BR>#else<BR> GetPortVal(0x378,
&m_nValue,1);<BR> m_nValue = m_nValue &
0x000000ff;<BR> temp1 = "
";<BR> temp2.Format(_T("%.2x"),m_nValue);<BR> if(m_read
!= _T(""))<BR> m_read = m_read +
temp1;<BR> m_read = m_read +
temp2;<BR> SetDlgItemText(IDC_EDIT_READ,m_read);<BR> DWORD
dwSel =
pReadEdit->GetSel();<BR> pReadEdit->SetSel(HIWORD(dwSel),
-1);<BR>#endif<BR> CDialog::OnTimer(nIDEvent);<BR> return;<BR>}</P>
<P> 以上程序已经经过了验证能够正确运行。<BR><BR><BR></P>
<P><BR> </P></TD></TR>
<TR>
<TD align=right><FONT color=#9c3316>smithhy 于10 : 23 发表
已被浏览90次</FONT> <A
href="http://oldblog.blogchina.com/article_236903.1255233.html">评论(0)</A>
/ <A
href="http://oldblog.blogchina.com/refer.1255233.html">引用(0)</A>
<A
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://blogmark.blogchina.com/jsp/key/quickaddkey.jsp?k='+encodeURI(d.title)+'&u='+encodeURI(d.location.href)+'&c='+encodeURI(t),'keyit','scrollbars=no,width=500,height=430,status=no,resizable=yes'));keyit.focus();">加入博采中心</A></TD></TR></TBODY></TABLE>
<TABLE>
<TBODY>
<TR>
<TD align=middle><IFRAME border=0 marginWidth=0 marginHeight=0
src="并口模拟IIC的总结 - smithhy的博客.files/ad_bloginner.htm"
frameBorder=0 width=550 scrolling=no
height=60></IFRAME></TD></TR></TBODY></TABLE>
<P></P></TD></TR>
<TR>
<TD><IMG height=25
src="并口模拟IIC的总结 - smithhy的博客.files/conner_2_1.gif" width=25></TD>
<TD align=right><IMG height=25
src="并口模拟IIC的总结 - smithhy的博客.files/conner_2_2.gif"
width=25></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD height=10></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%"
background="并口模拟IIC的总结 - smithhy的博客.files/bot_bg.gif">
<TBODY>
<TR>
<TD align=right height=30><IMG height=30
src="并口模拟IIC的总结 - smithhy的博客.files/blogchina.gif"
width=143></TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -