微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 并口模拟IIC的经验之谈

并口模拟IIC的经验之谈

时间:11-25 来源:互联网 点击:

程序代码:

/***************************************/
//控制的地址是0x378
//SDA信号: PIN13 模拟作为主机的输入;
// PIN3 模拟作为主机的输出;
//SCL信号: PIN15 模拟作为主机的输入;
// PIN5 模拟作为主机的输出;
/***************************************/
/*************************************/
//函数名称:IIC start 函数
//开始信号:在时钟线(SCL)为高电平其间,
//数据线(SDA)由高变低,将产生一个开始信号。
/*************************************/
void port2iicbase::i2c_start()
{
SetPortVal(PORT1,0x07,1);/*scl 0, sda 1*/
delay(1);
SetPortVal(PORT1,0x0f,1);/*scl 1, sda 1*/
delay(1);
SetPortVal(PORT1,0x0d,1);/*scl 1, sda 0*/
delay(1);
SetPortVal(PORT1,0x05,1);/*scl 0, sda 0*/
return;
}
/*************************************/
//函数名称:IIC stop 函数
//停止信号:在时钟线(SCL)为高电平其间,
//数据线(SDA)由低变高,将产生一个停止信号
/*************************************/
void port2iicbase::i2c_stop()
{
SetPortVal(PORT1,0x07,1);/*scl 0, sda 1*/
delay(1);
SetPortVal(PORT1,0x0d,1);/*scl 1, sda 0*/
delay(1);/***/
SetPortVal(PORT1,0x0f,1);/*scl 1, sda 1*/
delay(1);/**/
SetPortVal(PORT1,0x05,1);/*scl 0, sda 0*/
}
/*************************************/
//函数名称:IIC 写一个字节函数
/*************************************/
int port2iicbase::i2c_writebyte(char c)
{
short int count=7;
char temp;
DWORD dwPortVal;
char e;

while(count>=0)
{
temp=c>>count;
temp=temp7; //确定传输的字节
if (temp=='\x80') //传输"1"
{
SetPortVal(PORT1,0x05,1);/*scl 0, sda 0*/
delay(1);
SetPortVal(PORT1,0x07,1);/*scl 0, sda 1*/
delay(1);
SetPortVal(PORT1,0x0f,1);/*scl 1, sda 1*/
delay(1);
SetPortVal(PORT1,0x07,1);/*scl 0, sda 1*/
delay(1);
}
else //传输"0"
{
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);
}
count--;
}
/**ask**/
SetPortVal(PORT1,0x07,1);/*scl 0, sda 1*/
delay(1);/***/
SetPortVal(PORT1,0x0f,1);/*scl 1, sda 1*/
delay(1);
// SetPortVal(PORT1,0x05,1);/*scl 0, sda 0*/
// delay(1);/***/

GetPortVal(PORT2, dwPortVal, 1);
e = (char)dwPortVal;
temp=e>>4;
temp=temp7;

if (temp=='\x0')
return 0;
else
MessageBox(NULL,"Not Acknowledge!!","发送错误",MB_OKCANCEL);
return 1;
}
/*************************************/
//函数名称:IIC 读一个字符函数
/*************************************/
char port2iicbase::i2c_readbyte()
{
unsigned short count=8;
char d,e,f='\x0';
DWORD dwPortVal;

while(count>0)
{
SetPortVal(PORT1,0x07,1);/*scl 0, sda 1*/
delay(1);/***/
SetPortVal(PORT1,0x0f,1);/*scl 1, sda 1*/
delay(1);

GetPortVal(PORT2, dwPortVal, 1);
e = (char)dwPortVal;
d=e>>4;
d=d7;

if(d=='\x80')
d='\x1';
f=f1;
f=(f+d); //组合成字节
count--;
}
return f;
}
/*************************************/
//函数名称:主机(并口)的发送应答
/*************************************/
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;
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top