我创建了函数、但我不知道代码为什么不起作用。 我的 I2C 协议是否错误? 我的 EEPROM 为24LC43A
void I2C_setup(){
RCGCGPIO |= 0x8;//端口 E 时钟使能
RCGCI2C |= 0x8;//I2C 模块3使能
GPIODEN_PORTD = 0x3;//为端口 D0和 D1 => 00000011启用数字功能
GPIOAFSEL_PORTD |= 0x3;// D0和 D1有一个替代函数
GPIOPCTL_PORTD |= 0x33;//针对 D0和 D1 (即 SCL 和 SDA)使用函数3
GPIOODR_PORTD |= 0x2;//DRAIN 表示 D1 00000010
I2CMCR_3 = 0x10;//这是主机
I2C_SET_SCL_PEiiod (0.0001);
}
void I2C_SET_SCL_peiiod (double SCL_period){
I2CMTPR_3 = SCL_PERIOD * 800000 + 1;
}
void I2Ceprom_byte_write (char data、int address)
{
char upper_address =(unsigned) address >> 8;
char lower_address =(unsigned) address & 0xff;
//发送启动条件
I2CMSA_3 = 0xA0;//1010 000 0发送
I2CMCS_3 = 0x3;//00011生成 START 位和 I2C 使能
I2C_EEPROM_WAIT ();
I2CMDR_3 = UP_ADDRESS;//发送地址(1/2)
I2CMCS_3 = 0x1;
I2C_EEPROM_WAIT ();
I2CMDR_3 = LOWER_ADDRESS;//发送地址(2/2)
I2CMCS_3 = 0x1;
I2C_EEPROM_WAIT ();
I2CMDR_3 =数据;
I2CMCS_3 = 0x5;//生成 STOP
I2C_EEPROM_WAIT ();
I2C_EEPROM_BUS_WAIT ();
}
int I2Ceprom_byte_read (int 地址)
{
char upper_address =(unsigned) address >> 8;
char lower_address =(unsigned) address & 0xff;
Int 数据;
I2CMSA_3 = 0xA0;
I2CMCS_3 = 0x3;
I2C_EEPROM_WAIT ();
I2CMDR_3 = UP_ADDRESS;
I2CMCS_3 = 0x1;
I2C_EEPROM_WAIT ();
I2CMDR_3 = LOWER_ADDRESS;
I2CMCS_3 = 0x1;
I2C_EEPROM_WAIT ();
I2CMSA_3 = 0xA1;
I2CMCS_3 = 7;
I2C_EEPROM_WAIT ();
I2C_EEPROM_BUS_WAIT ();
数据= I2CMDR_3;
返回数据;
}
void I2C_EEPROM_WAIT (){
while (I2CMCS_3和0x1);
}
void I2C_EEPROM_BUS_WAIT (){
while (I2CMCS_3和0x40);
}