大家好,我想读/写一些数据到外部 EEPROM (AT24C256)。 我可以读取/写入字节、但在读取和写入字时、尤其是双字时、我面临一些问题。
1) 1)当读取一个字时、我看到第一个字节的前面为0xFFFF。 这种情况有时并非始终发生。
2) 2)我无法读取和写入双字校正。 我将写下我期望的内容和我看到的内容。
正在等待帮助。
在这里,您可以看到相关的代码段:
初始化
空 InitI2C (空)
{
SysCtlPeripheralEnable (SYSCTL_Periph_I2C5);
SysCtlPeripheralReset (SYSCTL_Periph_I2C5);
SysCtlPeripheralEnable (SYSCTL_Periph_GPIOB);
GPIOPinConfigure (GPIO_PB4_I2C5SCL);
GPIOPinConfigure (GPIO_PB5_I2C5SDA);
GPIOPinTypeI2CSCL (GPIO_PORTB_BASE、GPIO_PIN_4);
GPIOPinTypeI2C (GPIO_PORTB_BASE、GPIO_PIN_5);
I2CMasterInitExpClk (I2C5_base、SysCtlClockGet ()、false);
HWREG (I2C5_base + I2C_O_FIFOCTL)= 80008000;
I2CMasterEnable (I2C5_base);
EEPROM_WRITE_PROVE_ENABLE();
}
void EEPROM_WRITE_PROVE_ENABLE (void)
{
GPIOPinTypeGPIOOutput (GPIO_PORTB_BASE、GPIO_PIN_3);
GPIOPinWrite (GPIO_PORTB_BASE、GPIO_PIN_3、GPIO_PIN_3);
}
void EEPROM_WRITE_PROVE_DISABLE (void)
{
GPIOPinTypeGPIOOutput (GPIO_PORTB_BASE、GPIO_PIN_3);
GPIOPinWrite (GPIO_PORTB_BASE、GPIO_PIN_3、0);
}
空 Write_EEPROM_Byte (UINT16地址、Int16数据)
{
EEPROM_WRITE_PROVE_DISABLE();
I2CMasterSlaveAddrSet (I2C5_base、slave_address、false);
I2CMasterDataPut (I2C5_base、((地址>> 8)& 0x00FF);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_START);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base、(地址和0x00FF));
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_CONT);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base、data & 0xFF);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_FINISH);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
EEPROM_WRITE_PROVE_ENABLE();
}
Int16 READ_EEPROM_Byte (UINT16地址)
{
int16 temp = 0;
I2CMasterSlaveAddrSet (I2C5_base、slave_address、false);
I2CMasterDataPut (I2C5_base、((地址>> 8)& 0x00FF);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_START);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base、(地址和0x00FF));
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_CONT);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
I2CMasterSlaveAddrSet (I2C5_base、slave_address、true);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_SINGLE_Receive);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
temp =(I2CMasterDataGet (I2C5_base)& 0xFF);
return( temp );
}
空 Write_EEPROM_Word (UINT16地址、Int16数据)
{
EEPROM_WRITE_PROVE_DISABLE();
I2CMasterSlaveAddrSet (I2C5_base、slave_address、false);
I2CMasterDataPut (I2C5_base、((地址>> 8)& 0x00FF);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_START);
SysCtlDelay (1000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base、(地址和0x00FF));
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_CONT);
SysCtlDelay (1000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base、((数据>> 8)& 0x00FF);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_CONT);
SysCtlDelay (1000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base、(DATA & 0x00FF));
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_FINISH);
SysCtlDelay (1000);
while (I2CMasterBusy (I2C5_base));
EEPROM_WRITE_PROVE_ENABLE();
}
Int16 READ_EEPROM_Word (UINT16地址)
{
int16 temp = 0;
I2CMasterSlaveAddrSet (I2C5_base、slave_address、false);
I2CMasterDataPut (I2C5_base、((地址>> 8)& 0x00FF);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_START);
SysCtlDelay (1000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base、(地址和0x00FF));
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_CONT);
SysCtlDelay (1000);
while (I2CMasterBusy (I2C5_base));
I2CMasterSlaveAddrSet (I2C5_base、slave_address、true);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_Receive_start);
SysCtlDelay (1000);
while (I2CMasterBusy (I2C5_base));
temp =(I2CMasterDataGet (I2C5_base)<<8);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_Receive_CONT);
SysCtlDelay (1000);
while (I2CMasterBusy (I2C5_base));
temp +=(I2CMasterDataGet (I2C5_base));
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_Receive_finish);
return( temp );
}
空 Write_EEPROM_double_Word (UINT16地址、Int32数据)
{
EEPROM_WRITE_PROVE_DISABLE();
I2CMasterSlaveAddrSet (I2C5_base、slave_address、false);
I2CMasterDataPut (I2C5_base、((地址>> 8)& 0x00FF);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_START);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base、(地址和0x00FF));
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_CONT);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base、((数据>> 24)& 0x00FF);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_CONT);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base、((数据>> 16)& 0x00FF);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_CONT);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base、((数据>> 8)& 0x00FF);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_CONT);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base,(data & 0x00FF));
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_FINISH);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
EEPROM_WRITE_PROVE_ENABLE();
}
uint32_t READ_EEPROM_DOUBLE_Word (uint16地址)
{
uint16_t temp2、temp3、temp4、temp5;
uint32_t temp;
I2CMasterSlaveAddrSet (I2C5_base、slave_address、false);
I2CMasterDataPut (I2C5_base、((地址>> 8)& 0x00FF);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_START);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
I2CMasterDataPut (I2C5_base、(地址和0x00FF));
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_SEND_CONT);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
I2CMasterSlaveAddrSet (I2C5_base、slave_address、true);
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_Receive_start);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
TEMP2 =(I2CMasterDataGet (I2C5_base));
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_Receive_CONT);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
temp3 =(I2CMasterDataGet (I2C5_base)));
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_Receive_CONT);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
temp4=(I2CMasterDataGet (I2C5_base)));
I2CMasterControl (I2C5_base、I2C_MASTER_CMD_BURST_Receive_finish);
SysCtlDelay (10000);
while (I2CMasterBusy (I2C5_base));
temp5=(I2CMasterDataGet (I2C5_base));
temp =(temp2 << 24)|(temp3 << 16)|(temp4 << 8)| temp5;
return( temp );
}
int main{
uint16_t EEPROM_READ_array[33000]、i、j;
WRITE_EEPROM_Word (6、0x9C3A);
SysCtlDelay (100000);
WRITE_EEPROM_Word (8、0x409B);
SysCtlDelay (100000);
WRITE_EEPROM_Word (10、0x23FD);
SysCtlDelay (100000);
WRITE_EEPROM_Word (12、0x3344);
SysCtlDelay (100000);
WRITE_EEPROM_DOUBLE_Word (30、0x103080B0);
SysCtlDelay (100000);
for (i=0、j=6;i<4;i++、j+=2)
EEPROM_READ_array[i]= READ_EEPROM_Word (j);
我将视为输出 FFFF9C3A、409B、23FD、3344。 正如我之前提到的、我在第一个字节之前看到一些0xFFFF 数据、这种情况有时会发生。 有时我会正确读取它。
uint32_t temp_value_int32;
temp_value_int32 = READ_EEPROM_DOUBLE_Word (30);
我明白了 :41B03080
预期 :103080B0
我想我错过了一些关于沥青的东西。
}