我正在努力写入或读取 AT24C256 I2C EEPROM。我正在使用 STM32F0 发现板读取/写入 EEPROM。
我正在使用 HAL 库和 CUBEMX 作为基本结构。我编写了小代码来测试读写功能。在调试时,Test 的值始终为“2”,而如果成功写入内存,则应该为“1”。这里是 :-
#define ADDR_24LCxx_Write 0x50
#define ADDR_24LCxx_Read 0x50
#define BufferSize 5
uint8_t WriteBuffer[BufferSize],ReadBuffer[BufferSize],Test;
uint16_t i;
I2C_HandleTypeDef hi2c1;
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
for(i=0; i<5; i++)
{
WriteBuffer[i]=i;
}
if(HAL_I2C_Mem_Write(&hi2c1, ADDR_24LCxx_Write, 0, I2C_MEMADD_SIZE_8BIT,WriteBuffer,BufferSize, 0x10) == HAL_OK)
{
Test = 1;
}
else
{
Test = 2;
}
HAL_I2C_Mem_Read(&hi2c1, ADDR_24LCxx_Read, 0, I2C_MEMADD_SIZE_8BIT,ReadBuffer,BufferSize, 0x10);
if(memcmp(WriteBuffer,ReadBuffer,BufferSize) == 0 ) /* check date */
{
Test = 3;
}
else
{
Test = 4;
}
}