写字节支持一个一个写,读内存支持连续读
#define SLAVE_ADDRESS_WR 0xA0 //写内存
#define SLAVE_ADDRESS_RD 0xA1 //读内存
uint8_t data[20] = "EEPROM TEST OK";
uint8_t recv[20] = {0};
/***************写内存********/
`void IIC_WRITE_TO_EEPROM(uint16_t MEM_ADDRESS,uint8_t* data,uint16_t len)
{
uint16_t i = 0;
for(i = 0;i < len;i++)
{
while(HAL_I2C_Mem_Write(&hi2c1,SLAVE_ADDRESS_WR,MEM_ADDRESS,I2C_MEMADD_SIZE_16BIT,data,1,10) != HAL_OK);
MEM_ADDRESS++;
data++;
}
}
/****************读内存******/
void IIC_READ_FROM_EEPROM(uint16_t MEM_ADDRESS,uint8_t* recv,uint16_t len)
{
while(HAL_I2C_Mem_Read(&hi2c1,SLAVE_ADDRESS_RD,MEM_ADDRESS,I2C_MEMADD_SIZE_16BIT,recv,len,10) != HAL_OK);
}
再来看测试代码部分:
/* USER CODE BEGIN 2 */
printf("uart init ok\n");
IIC_WRITE_TO_EEPROM(0,data,20);
HAL_Delay(500);
IIC_READ_FROM_EEPROM(0,recv,20);
HAL_Delay(500);
printf("recv :%s\n",recv);
![在这里插入图片描述](
)
|