janewood 发表于 2023-11-21 20:23

IIC总线操作EEPROM存储模块AT24C02



/**
* @brief AT24C02初始化
*
*/
void AT24C02_Init(void)
{
IIC_Init();// 初始化IIC总线
}

/**
* @brief AT24C02字节写入
*
* @param Address: 字节地址
* @param Data: 待写入的数据
*/
void AT24C02_ByteWrite(uint8_t Address, uint8_t Data)
{
IIC_StartSignal();      // 发送开始信号

// 发送设备地址,写操作
IIC_SendBytes(0xA0);   
if (IIC_WaitACK() == 1)   // AT24C02没应答
{
    printf(" Answered the device address: Error\n");
    IIC_StopSignal();       // 发送停止信号
}
else
{
    printf(" Answered the device address: OK\n");
}

// 发送字地址
IIC_SendBytes(Address);   
if (IIC_WaitACK() == 1)   // AT24C02没应答
{
    printf(" Answered the word address: Error\n");
    IIC_StopSignal();       // 发送停止信号
}
else
{
    printf(" Answered the word address: OK\n");
}

// 发送待写入的数据
IIC_SendBytes(Data);   
if (IIC_WaitACK() == 1)   // AT24C02没应答
{
    printf(" Answered the data: Error\n");
    IIC_StopSignal();       // 发送停止信号
}
else
{
    printf(" Answered the data: OK\n");
}

IIC_StopSignal();         // 发送停止信号
}


/**
* @brief AT24C02页编程(8字节/页)
*
* @param Address: 页地址
* @param buf: 待写入的数据
* @param DataLen: 待写入的数据长度
*/
void AT24C02_PageWrite(uint32_t Address, uint8_t *buf, uint8_t DataLen)
{
IIC_StartSignal();      // 发送开始信号

// 发送设备地址,写操作
IIC_SendBytes(0xA0);
if (IIC_WaitACK() == 1)   // AT24C02没应答
{
    printf(" Answered the device address: Error\n");
    IIC_StopSignal();       // 发送停止信号
}
else
{
    printf(" Answered the device address: OK\n");
}

// 发送页地址
IIC_SendBytes(Address);
if (IIC_WaitACK() == 1)   // AT24C02没应答
{
    printf(" Answered the page address: Error\n");
    IIC_StopSignal();       // 发送停止信号
}
else
{
    printf(" Answered the page address: OK\n");
}

// 循环发送数据
while (DataLen--)
{
    IIC_SendBytes(*buf++);// 发送数据
    if (IIC_WaitACK() == 1) // AT24C02没应答
    {
      printf(" Answered the data: Error\n");
      IIC_StopSignal();   // 发送停止信号
    }
    else
    {
      printf(" Answered the data: OK\n");
    }
}

IIC_StopSignal();         // 发送停止信号
}


/**
* @brief AT24C02当前地址读
*
* @return uint8_t 当前地址的数据
*/
uint8_t AT24C02_CurrentAddressRead(void)
{
uint8_t Data;

IIC_StartSignal(); // 发送开始信号

// 发送设备地址,读操作
IIC_SendBytes(0xA1);   
if (IIC_WaitACK() == 1)   // AT24C02没应答
{
    printf(" Answered the device address: Error\n");
    IIC_StopSignal();       // 发送停止信号
}
else
{
    printf(" Answered the device address: OK\n");
}

// 读取1字节数据
Data = IIC_ReadBytes();

// 发送应答信号
IIC_MasterACK(1); // 不应答

IIC_StopSignal(); // 发送停止信号
return Data;      // 返回接收到的数据
}


uint8_t data;

AT24C02_Init()

AT24C02_PageWrite(0x00, "0123456", 6);
HAL_Delay(100);

AT24C02_ByteWrite(0x07, '7');
HAL_Delay(100);

data = AT24C02_CurrentAddressRead();
printf("AT24C02_CurrentAddressRead: %c", data);



tpgf 发表于 2024-2-1 10:08

如果iic通讯发生错误 如何进行动态更新啊

qcliu 发表于 2024-2-1 11:33

为什么现在大部分的存储芯片都是iic通讯或者spi通讯方式呢

kxsi 发表于 2024-2-1 12:10

这种存储模块的读写操作是不是不用按照flash的来啊

wiba 发表于 2024-2-1 12:44

这种通讯方式需要设置重发机制吗

coshi 发表于 2024-2-1 20:50

如果返回错误代码的话 后续应该如何处理呢

drer 发表于 2024-2-1 21:22

前期设备的地址是如何设置的呢
页: [1]
查看完整版本: IIC总线操作EEPROM存储模块AT24C02