跳至主要内容

【转】GPIO编程模拟I2C入门

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==1return(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

评论

此博客中的热门博文

【转】smb协议栈使用示例

/*  * * uncdownload.c  * *  * * Utility for downloading files from SMB shares using libsmbclient  * *  * * Copyright(C) 2006 Sophos Plc, Oxford, England.  * *  * * This program is free software; you can redistribute it and/or modify it under the terms of the  * * GNU General Public License Version 2 as published by the Free Software Foundation.  * *  * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without  * * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  * * See the GNU General Public License for more details.  * *  * * You should have received a copy of the GNU General Public License along with this program; if not,  * * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  * *  * */ # include < sys / types . h > # include < sys / time . h > # include ...

【转】Ether Types

Ether Types (last updated 2008-09-09) NOTE: Please see [RFC5342] for current information and registration procedures. This registry will be revised soon and will be replaced with up-to-date information. Many of the networks of all classes are Ethernets (10Mb) or Experimental Ethernets (3Mb). These systems use a message "type" field in much the same way the ARPANET uses the "link" field. If you need an Ether Type, contact: IEEE Registration Authority IEEE Standards Department 445 Hoes Lane Piscataway, NJ 08854 Phone +1 732 562 3813 Fax: +1 732 562 1571 Email: <ieee-registration-authority& ieee.org > http://standards.ieee.org/regauth/index.html The following list of EtherTypes is contributed unverified information from various sources. Another list of EtherTypes is maintained by Michael A. Patton and is accessible at: <URL: http://www.cavebear.com/CaveBear/Ethernet/ > <URL: ftp://ftp.cavebear.com/pub/Ethernet-codes > Assign...

【转】udp编程实例

UDP通讯实例 2008-04-29 15:30:05 / 个人分类: linux C编程 UDP协议的几点好处: 1.UDP不要求保持一个连接; 2.UDP没有因接收方认可收到数据包(或者当数据包没有正确抵达而自动重传)而带来的开销; 3.设计UDP的目的是用于短应用和控制信息; 4.在一个数据包接一个数据包的基础上,UDP要求的 网络 带宽比TCP更小。 UDP的几个缺点: 1.程序员必须创建代码检测传输错误并进行重传(如果应用程序要求这样做); 2.程序员必须把大数据包分片。 code: <1> /* * sender.c--UDP protocol example */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> int port = 6789; int main() {     int socket_descrīptor;     int iter = 0;     char buf[80];     struct sockaddr_in address;     /* Initialize socket address structure for Interner Protocols */     bzero(&address, sizeof(address)); // empty data structure     address.sin_family = AF_INET;     address.sin_addr.s_addr = inet_addr("127.0.0.1");     address...