与 Tiva TM4C123GXL 的 I2C 通信

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

我正在尝试使用 I2C 连接 Tiva TM4C123GXL。我认为我正在正确写入数据寄存器,但是每次我从 I2C 读取时,我总是得到 0xFF。我已连接到 EEPROM。

我不知道问题出在哪里。我不允许使用任何库,因此我直接访问寄存器。任何帮助将非常感激。我的读取、写入和初始化i2c模块的代码如下所示:

void write_i2c(unsigned int data) {
int i;
I2C3_MSA_R = 0xA0;
I2C3_MDR_R = 0x00000000;
I2C3_MCS_R = 0b00000011;

while((I2C3_MCS_R & BIT_0) != 0);

I2C3_MDR_R = 0x00000000;
I2C3_MCS_R = 0b00000001;
while((I2C3_MCS_R & BIT_0) != 0);

I2C3_MDR_R = data;
I2C3_MCS_R = 0b00000101;

while((I2C3_MCS_R & BIT_0) != 0);
for(i = 0; i < 10000; i++);
}

unsigned int read_i2c() {
I2C3_MSA_R = 0xA0;
I2C3_MDR_R = 0x00000000;
I2C3_MCS_R = 0b00000011;
while((I2C3_MCS_R & BIT_0) != 0);
I2C3_MDR_R = 0x00000000;
I2C3_MCS_R = 0b00000001;

I2C3_MSA_R = 0xA1;
unsigned int data;
// 0xA0 is EEPROM address shifted by 1
I2C3_MCS_R = 0b00000101;

// I2C3_MCS_R = 0b00000101;
while((I2C3_MCS_R & BIT_0) != 0);

data = I2C3_MDR_R;
return data;
}

void init_i2c() {
//address of eeprom is 1010000
// A0 is PB0, A1 is PB1, A2 is PB2
SYSCTL_RCGCI2C_R = BIT_3;

GPIO_PORTD_PUR_R |= BIT_0 | BIT_1;

// Port D Alternate Function
// PD0 is SCL, PD1 is SDA
GPIO_PORTD_AFSEL_R |= BIT_0 | BIT_1;
GPIO_PORTD_PCTL_R |= 0x33;
GPIO_PORTD_ODR_R |= BIT_1;
GPIO_PORTD_DEN_R |= BIT_0 | BIT_1;

// Enable I2C3 Master
I2C3_MCR_R = BIT_4;
I2C3_MTPR_R = 0x64;
}
c i2c eeprom
1个回答
0
投票

我找到了解决方案。我的代码无法运行有两个主要原因:

  1. 同时使用端口 B 和 D 会导致问题。它们在板上以某种方式连接,所以由于我已经在另一个功能中使用端口 B,所以读数被搞乱了

  2. 我发送到 I2CMCS 寄存器的值以及它们发送的顺序对于读取功能来说是错误的

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