This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

css6中的I2C中程序来理解



Uint16 I2CA_WriteData(struct I2CMSG *msg)
{
   Uint16 i;
   // Wait until the STP bit is cleared from any previous master communication.
   // Clearing of this bit by the module is delayed until after the SCD bit is
   // set. If this bit is not checked prior to initiating a new message, the
   // I2C could get confused.
   if (I2caRegs.I2CMDR.bit.STP == 1)  
   {
      return I2C_STP_NOT_READY_ERROR;
 }
   // Setup slave address
   I2caRegs.I2CSAR = msg->SlaveAddress;   //这代表I2C为主机模式
   // Check if bus busy
   if (I2caRegs.I2CSTR.bit.BB == 1)  //代表接受到了起始信号
   {
      return I2C_BUS_BUSY_ERROR;
   }
   // Setup number of bytes to send
   // MsgBuffer + Address
   I2caRegs.I2CCNT = msg->NumOfBytes+2;   // 为一个设备地址,一个是数据地址
   // Setup data to send
   I2caRegs.I2CDXR = msg->MemoryHighAddr;
   I2caRegs.I2CDXR = msg->MemoryLowAddr;
// for (i=0; i<msg->NumOfBytes-2; i++)
   for (i=0; i<msg->NumOfBytes; i++)
   {
      I2caRegs.I2CDXR = *(msg->MsgBuffer+i); //msg->MsgBuffer 是数组的首地址,则加1是下一个数组
   }
   // Send start as master transmitter
   I2caRegs.I2CMDR.all = 0x6E20;   //0x0110 1110 0010 0000   理解位启动I2C
   return I2C_SUCCESS;     //0x00
}
为什么写数据之前要判断stp==1? 状态,而不是首先发出start信号
还有就是,I2C例程里,怎么要加入这么多不同的状态——黄色部分
  • 请问您现在使用的是哪款芯片?以28035为例,在其用户指南中有对应的说明

    In the master mode, the RM, STT, and STP bits determine when the I2C module starts and stops data transmissions

    在master 模式下,RM STT和STP位确定I2C模块何时启动和停止数据传输

    而黄色部分的状态就是为了使I2C通信更加准确的

  • 想坠问一个关于I2C的
    void WriteData(Uint16 addr,Uint16 data) //向EEPROM 指定地址写入一个字节的数据
    {
    begintrans(); //开始
    bytein(0xA0 + ((addr & 0x0300) >> 7)); //写入写控制字0xA0
    bytein(addr); //写入指定地址
    bytein(data); //写入待写入EEPROM 的数据
    stoptrans(); //停止
    delay1(8000);
    }
    中的bytein(0xA0 + ((addr & 0x0300) >> 7))地址和0x0300与,这有什么意义
    其中0xA0为EEPROM的设备地址



    void bytein(Uint16 ch) //向EEPROM 写入一个字节
    {
    Uint16 i; //变量定义
    SCL_0; //SCL=0
    delay1(delay1_UNIT * 2); //延时
    SDA_WRITE(); //SDA方向为输出到EEPROM
    delay1(delay1_UNIT); //延时
    for(i=8;i>0;i--)
    {
    if ((ch & 0x80)== 0)
    {
    SDA_W0; //数据通过SDA 串行移入EEPROM
    delay1(delay1_UNIT); //延时
    }
    else
    {
    SDA_W1;
    delay1(delay1_UNIT); //延时
    }
    SCL_1; //SCL=1
    delay1(delay1_UNIT * 2); //延时
    ch <<= 1;
    SCL_0; //SCL=0
    delay1(delay1_UNIT); //延时
    }
  • 0xA0应该是写控制字而不是设备地址

    addr为EEPROM中的指定地址,应该是一个字节。它与0x0300位与之后为0 ,然后右移7位还是为0

    所以写入的还是0xA0   

  • 0xA0应该是写控制字,想问下什么叫控制字啊
  • 就是来表明这个指令是用来读还是用来写的标志字,这个和您的EEPROM有关