ARM编程:ARM普通GPIO口线模拟I2C

请教个问题:
因为需要很多EEPROM进行点对点控制,所以我现在要用ARM的GPIO模拟I2C,管脚方向我设
置的是向外的。我用网上的RW24C08的万能程序修改了一下,先进行两根线的模拟,SDA6,
SCL6,但是读出来的数不对。我做了一个简单的实验,模拟SDA6,SCL6输出方波,在示波
器上看到正确方波,也就是说,我的输出控制是没问题的。
哪位大哥能指点一下,是否在接收时管脚方向要设为向内?(不过IOPIN不管什么方向都可
以读出当前状态值的阿)

附修改的RW24C08()程序:
#define SomeNOP() delay(300);

/**//********************************** RW24C08
*****************************************/

/**//*-----------------------------------------------------------------------------
---
调用方式:void I2CInit(void)
函数说明:私有函数,I2C专用
-------------------------------------------------------------------------------
--*/
void I2CInit(void)

...{
IO0CLR = SCL6; //初始状态关闭总线
SomeNOP(); //延时
I2CStop(); //确保初始化,此时数据线是高电平
}


/**//*----------------------------------------------------------------------------
----
调用方式:void I2CStart(void)
函数说明:私有函数,I2C专用
-------------------------------------------------------------------------------
--*/
void I2CStart(void)

...{

SomeNOP();
IO0SET = SCL6;
SomeNOP();//INI
IO2CLR = SDA6;
SomeNOP(); //START
IO0CLR = SCL6;
SomeNOP();
}

/**//*-----------------------------------------------------------------------------
---
调用方式:void I2CStop(void)
函数说明:私有函数,I2C专用
------------------------------------------------------------------------------
---*/
void I2CStop(void)

...{
IO2CLR = SDA6;
SomeNOP(); //INI
IO0SET = SCL6;
SomeNOP();
IO2SET = SDA6;
SomeNOP();
IO0CLR = SCL6;
}


/**//*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
I2CClock: 发送总线时钟信号,并返回时钟电平为高期间SDA上的状态,低为 ACK,高失败
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

INT8U I2CClock(void)

...{
INT8U sample;
SomeNOP();
IO0SET = SCL6;
delay(180);
sample = (INT8U)((IO2PIN & 0x2000)>>13);
delay(120);
IO0CLR = SCL6;
SomeNOP();
//return (sample);
if(sample==1) return(1);
else return(0);
}

/**//*-----------------------------------------------------------------------------
---
调用方式:void SendAck(void)
函数说明:私有函数,I2C专用,主器件为接收方,从器件为发送方时,应答信号。
------------------------------------------------------------------------------
---*/
void SendAck(void)

...{
IO2CLR = SDA6;
I2CClock();
IO2SET = SDA6;
}


/**//*----------------------------------------------------------------------------
----
调用方式:void SendAck(void)
函数说明:私有函数,I2C专用,主器件为接收方,从器件为发送方时,非应答信号。
**-----------------------------------------------------------------------------
---*/
void SendNotAck(void)

...{
IO2SET = SDA6;
I2CClock();
}


/**//*----------------------------------------------------------------------------
----
调用方式:void I2CSend(uchar ch)
函数说明:私有函数,I2C专用
------------------------------------------------------------------------------
---*/
INT8U I2CSendByte(INT8U ch)

...{
register INT8U i;
INT8U statue;

for (i=0; i<8; i++)...{
statue = (INT8U) ((ch&0x80)>>7);
if(statue==1)
IO2SET = SDA6;
else
IO2CLR = SDA6;
ch <<= 1;
I2CClock();
}
IO2SET = SDA6;
return (~I2CClock());
}


/**//*----------------------------------------------------------------------------
----
调用方式:uchar I2CReceive(void)
函数说明:私有函数,I2C专用
------------------------------------------------------------------------------
---*/
INT8U I2CReceiveByte(void)

...{
register INT8U i;
INT8U ddata=0;

for (i=0;i<8;i++)...{
ddata<<=1;
if(I2CClock()) ddata++;
}
return (ddata);
}


/**//*------------------------------------------------
RW2408 Function
------------------------------------------------*/
//DataBuff 为读写数据输入/输出缓冲区的首址
//Length 为要读写数据的字节数量
//Address 为EEPROM的片内地址
//ControlByte 为EEPROM的控制字节,具体形式为(1)(0)(1)(0)(A2)(A1)(A0)(R/W),
// 其中R/W=1,表示读操作,R/W=0为写操作,A2,A1,A0为EEPROM的页选或片选
地址;
void RW2408(INT8U *DataBuff,INT8U Length,INT32U Addr,INT8U Wr)

...{
INT8U j; // 发送字节索引
// EA=0; // 发送期间禁止中断,防止
干扰
I2CInit();
I2CStart(); // 启动总线
I2CSendByte(0xa0); // 向IIC总线发送2408写地址
I2CSendByte(Addr); // 向IIC总线发送要操作的RAM
地址
if(Wr==0) // 如果是写操作

...{
for(j=0;j<Length;j++)

...{
I2CSendByte(*DataBuff++); // 每个循环送1个数据
}
}

/**//*---------------------------如果是读操作 ----------------------*/
else if(Wr==1)

...{
I2CStart(); // 启动总线
I2CSendByte(0xa1); // 向IIC总线发送2408读地址

for(j=0;j<Length-1;j++)

...{
IO2SET = SDA6; // 写之前把数
据线置高
*DataBuff++=I2CReceiveByte(); // 每次读1个字节
SendAck(); // 发送应答信号
}
*DataBuff=I2CReceiveByte(); // 读最后一个数据
SendNotAck(); // 发送非应答信号
}
I2CStop(); // 停止IIC总线
// EA=1;
}


//*************回答*************
你一定要改变GPIO的方向,ARM我还不太熟,不过我用过类似的MCU,要不读到的不是GPIO的外部电平,好像是别外一个REGISTER的东西,你好好看看吧,你在
读写的时候改变GPIO的方向就应该没有错了


发送时SDA设为输出,接收时将SDA设为输入



我也是碰到了这个情况啊,也是不能读写数据啊,我也是用8位机的修改的



那意思就是我sendbyte的时候GPIO是向外,receivebyte的时候GPIO是向内,也就是我要写
两个I2CCLOCK函数,一个用在发送,一个用在接收,分别对应不同的GPIO方向设置?



读IOPIN的时候是不管方向的,这是书上这么写的



没有错啊,可是你的SDA是用什么定义的 如果是用IO0PIN吗,还是用IO0SET或是IO0CLR?后面的两个只能反应你上次对这两个寄存器操作的值,不能反应其IO口的状态的,只能有用IO0PIN吗,那么好象不用定义方向,请指教!谢谢

----------------------------------------------
转自:http://www.c51bbs.com/c51bbs/topic/c51bbs781049.htm
AMBA 简介 随着深亚微米工艺技术日益成熟,集成电路芯片的规模越来越大。数字IC从基于时序驱动的设计方法,发展到基于IP复用的设计方法,并在SOC设计中得到了广泛应用。在基于IP复用的SoC设计中,片上总线设计是最关键的问题。为此,业界出现了很多片上总线标准。其中,由ARM公司推出的AMBA片上总线受到了广大IP开发商和SoC系统集成者的青睐,已成为一种流行的工业标准片上结构。AMBA规范主要包括了AHB(Advanced High performance Bus)系统总线和APB(Advanced Peripheral Bus)外围总线。 AMBA 片上总线 AMBA 2.0 规范包括四个部分:AHB、ASB、APB和Test Methodology。AHB的相互连接采用了传统的带有主模块和从模块的共享总线,接口与互连功能分离,这对芯片上模块之间的互连具有重要意义。AMBA已不仅是一种总线,更是一种带有接口模块的互连体系。下面将简要介绍比较重要的AHB和APB总线。 基于 AMBA 的片上系统 一个典型的基于AMBA总线的系统框图如图3所示。 大多数挂在总线上的模块(包括处理器)只是单一属性的功能模块:主模块或者从模块。主模块是向从模块发出读写操作的模块,如CPU,DSP等;从模块是接受命令并做出反应的模块,如片上的RAM,AHB/APB 桥等。另外,还有一些模块同时具有两种属性,例如直接存储器存取(DMA)在被编程时是从模块,但在系统读传输数据时必须是主模块。如果总线上存在多个主模块,就需要仲裁器来决定如何控制各种主模块对总线的访问。虽然仲裁规范是AMBA总线规范中的一部分,但具体使用的算法由RTL设计工程师决定,其中两个最常用的算法是固定优先级算法和循环制算法。AHB总线上最多可以有16个主模块和任意多个从模块,如果主模块数目大于16,则需再加一层结构(具体参阅ARM公司推出的Multi-layer AHB规范)。APB 桥既是APB总线上唯一的主模块,也是AHB系统总线上的从模块。其主要功能是锁存来自AHB系统总...
评论
发表评论