I2C与EEPROM通信

问题描述 投票:0回答:1

我基本上有这个问题一直困扰着我。我有一个PIC32MX128L128H处理器和一个基本I / O防护板。引脚和所有内容都正确完成,除了我使用I2C与EEPROM通信。我将在下面复制我的代码,但之前,我有控件:

I2C1CON:bit 0 SEN:启动条件使能位bit 2 PEN:停止条件使能位bit 3 RCEN:接收使能位bit 4 ACKEN:应答序列使能位bit 5 ACKDT:应答数据位

I2C1STAT:bit 15 ACKSTAT:应答状态位(1 =未接收,0 =已接收)bit 14 TRSTAT:发送状态位bit 6 I2COV:I2C接收溢出状态位

谢谢!

void EEPROM_wait(){
  while(I2C1STAT & (1 << 15)){

    I2C1CON |= 0x1;
    while(I2C1CON & 0x1);

    I2C1TRN = 0xa0;
    while(I2C1STAT & (1 << 14));

  }
}

void i2c_wait(){
  while(I2C1CON & 0x1f || I2C1STAT & (1 << 14));
}




void writeByte(uint8_t lowByte, uint8_t highByte, uint8_t data){


  //Start Procedure
  do{
  I2C1CON |= 0x1;
  i2c_wait();
  I2C1TRN = 0xa0;
  i2c_wait();
  } while(I2C1STAT & (1 << 15));
  //Send the adress of the EEPROM and also R/W bit

  //Send highByte
  I2C1TRN = highByte;
  i2c_wait();

  //Send lowByte
  I2C1TRN = lowByte;
  i2c_wait();

  //Send actual data
  I2C1TRN = data;
  i2c_wait();

  //Stop the procedure
  I2C1CON |= 0x4;
  i2c_wait();

  EEPROM_wait();



}


uint8_t readByte(uint8_t lowByte, uint8_t highByte)
{



  //Initialize Procedure
  do{
  I2C1CON |= 0x1;
  i2c_wait();
  I2C1TRN = 0xa0;
  i2c_wait();
  }while(I2C1STAT & (1 << 15));

  //Send the address of the EEPROM
  //I2C1TRN = 0xa0;
  //i2c_wait();

  //Send highByte
  I2C1TRN = highByte;
  i2c_wait();

  //Send lowByte

  I2C1TRN = lowByte;
  i2c_wait();

  //Initialize Procedure Read
  do{
  I2C1CON |= 0x1;
  i2c_wait();
  I2C1TRN = 0xa1;
  i2c_wait();
  } while (I2C1STAT & (1 << 15));

  //Send the address of the EEPROM with Read bit
  //I2C1TRN = 0xa1;
  //i2c_wait();

  //Enable Receive Procedure
  I2C1CON |= 1 << 3;
  i2c_wait();
  I2C1STATCLR = 1 << 6;

  //Read from the EEPROM
  uint8_t rtn = I2C1RCV;

  //Acknowledge
  I2C1CON &= ~(1 << 5);
  I2C1CON |= 1 << 4;

  //Stop Procedure
  I2C1CON |= 1 << 2;
  i2c_wait();

  return rtn;

}
c pic i2c
1个回答
0
投票

我意识到问题是什么。芯片组总线时钟速率与i2c总线时钟的设置不同,后者导致读写或误读。

© www.soinside.com 2019 - 2024. All rights reserved.