打印
[研电赛技术支持]

IIC总线操作EEPROM存储模块AT24C02

[复制链接]
757|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
janewood|  楼主 | 2023-11-21 20:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


/**
* @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("[AT24C02] Answered the device address: Error\n");
    IIC_StopSignal();       // 发送停止信号
  }
  else
  {
    printf("[AT24C02] Answered the device address: OK\n");
  }

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

  // 发送待写入的数据
  IIC_SendBytes(Data);     
  if (IIC_WaitACK() == 1)   // AT24C02没应答
  {
    printf("[AT24C02] Answered the data: Error\n");
    IIC_StopSignal();       // 发送停止信号
  }
  else
  {
    printf("[AT24C02] 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("[AT24C02] Answered the device address: Error\n");
    IIC_StopSignal();       // 发送停止信号
  }
  else
  {
    printf("[AT24C02] Answered the device address: OK\n");
  }

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

  // 循环发送数据
  while (DataLen--)
  {
    IIC_SendBytes(*buf++);  // 发送数据
    if (IIC_WaitACK() == 1) // AT24C02没应答
    {
      printf("[AT24C02] Answered the data: Error\n");
      IIC_StopSignal();     // 发送停止信号
    }
    else
    {
      printf("[AT24C02] 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("[AT24C02] Answered the device address: Error\n");
    IIC_StopSignal();       // 发送停止信号
  }
  else
  {
    printf("[AT24C02] 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的来啊

使用特权

评论回复
5
wiba| | 2024-2-1 12:44 | 只看该作者
这种通讯方式需要设置重发机制吗

使用特权

评论回复
6
coshi| | 2024-2-1 20:50 | 只看该作者
如果返回错误代码的话 后续应该如何处理呢

使用特权

评论回复
7
drer| | 2024-2-1 21:22 | 只看该作者
前期设备的地址是如何设置的呢

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

46

主题

1080

帖子

1

粉丝