搜索
bottom↓
回复: 2

关于STM8l读取AT24C512的数读取错误

[复制链接]

出0入0汤圆

发表于 2015-8-4 09:57:45 | 显示全部楼层 |阅读模式
在使用STM8L与EEPROM芯片AT24C512通信的时候,出现了一点问题
因为STM8和STM32的库函数相似,所以参考了野火教程里EEPROM的代码,修改移植到STM8L上


读函数
void EEPROM_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
{

    /*!< While the bus is busy */
    while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
    /*!< Send START condition */
    I2C_GenerateSTART(I2C1, ENABLE);
    /*!< Test on EV5 and clear it */
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
    /*!< Send EEPROM address for write */
    I2C_Send7bitAddress(I2C1, 0xa0, I2C_Direction_Transmitter);
    /*!< Test on EV6 and clear it */
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    I2C_Cmd(I2C1, ENABLE);
    I2C_SendData(I2C1, ReadAddr);
    /*!< Test on EV8 and clear it */
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    /*!< Send STRAT condition a second time */
    I2C_GenerateSTART(I2C1, ENABLE);
    /*!< Test on EV5 and clear it */
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
    /*!< Send EEPROM address for read */
    I2C_Send7bitAddress(I2C1, 0xa0, I2C_Direction_Receiver);
    /*!< Test on EV6 and clear it */
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
   
/* While there is data to be read */
    while (NumByteToRead)
    {
        if (NumByteToRead == 1)
        {
            /* Disable Acknowledgement */
            I2C_AcknowledgeConfig(I2C1, DISABLE);

            /* Send STOP Condition */
            I2C_GenerateSTOP(I2C1, ENABLE);
        }
        /* Test on EV7 and clear it */
        if (I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
        {
            /* Read a byte from the EEPROM */
            *pBuffer = I2C_ReceiveData(I2C1);
            /* Point to the next location where the byte read will be
            68 saved */
            pBuffer++;
            /* Decrement the read bytes counter */
            NumByteToRead--;
        }
    }
    I2C_AcknowledgeConfig(I2C1, ENABLE);
}

写函数
void EEPROM_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
{
    uint8_t NumOfPage = 0, NumOfSingle = 0, count = 0;
    uint16_t Addr = 0;

    Addr = WriteAddr % I2C_PageSize;
    count = (uint8_t)(I2C_PageSize - (uint16_t)Addr);
    NumOfPage =  (uint8_t)(NumByteToWrite / I2C_PageSize);
    NumOfSingle = (uint8_t)(NumByteToWrite % I2C_PageSize);

    if (Addr == 0)
    {
        if (NumOfPage == 0)
        {
          EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)NumOfSingle);
          EEPROM_WaitEepromStandbyState();
      }
    else
    {
        while (NumOfPage--)
        {
            EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)I2C_PageSize);
            EEPROM_WaitEepromStandbyState();
            WriteAddr +=  I2C_PageSize;
            pBuffer += I2C_PageSize;
        }

        if (NumOfSingle != 0)
        {
            EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)NumOfSingle);
            EEPROM_WaitEepromStandbyState();
        }
      }
    }
  else
  {
    if (NumOfPage == 0)
    {
          EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)NumOfSingle);
          EEPROM_WaitEepromStandbyState();
    }
    else
    {
        NumByteToWrite -= count;
        NumOfPage = (uint8_t)(NumByteToWrite / I2C_PageSize);
        NumOfSingle = (uint8_t)(NumByteToWrite % I2C_PageSize);

        if (count != 0)
        {
            EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)count);
            EEPROM_WaitEepromStandbyState();
            WriteAddr += count;
            pBuffer += count;
        }

        while (NumOfPage--)
        {
            EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)I2C_PageSize);
            EEPROM_WaitEepromStandbyState();
            WriteAddr +=  I2C_PageSize;
            pBuffer += I2C_PageSize;
        }
        if (NumOfSingle != 0)
        {
            EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)NumOfSingle);
            EEPROM_WaitEepromStandbyState();
        }
    }
  }
}

写页函数

void EEPROM_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t* NumByteToWrite)
{
      while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
      /*!< Send START condition */
      I2C_GenerateSTART(I2C1, ENABLE);
      /*!< Test on EV5 and clear it */
      while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
      /*!< Send EEPROM address for write */
      I2C_Send7bitAddress(I2C1, 0xA0, I2C_Direction_Transmitter);
      /*!< Test on EV6 and clear it */
      while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
      
      I2C_SendData(I2C1, WriteAddr);
      /*!< Test on EV8 and clear it */
      while (! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
      
      while (NumByteToWrite--)
      {
          /* Send the current byte */
          I2C_SendData(I2C1, *pBuffer);
          /* Point to the next byte to be written */
          pBuffer++;
          /* Test on EV8 and clear it */
          while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
      }
      /*!< Send STOP condition */
      I2C_GenerateSTOP(I2C1, ENABLE);
  
}

测试函数
void EEPROM_Test()
{
    uint16_t i = 0;
   
    for(i = 0; i < 5; i++)
    {
        printf("The Write Data is %d\n", test_Write);
    }
   
    EEPROM_WriteBuffer(test_Write, EEPROM_Firstpage, 5);
    printf("Write success!\r\n");
    delay_ms(50);
    printf("Read Data\r\n");
    delay_ms(10);
    EEPROM_ReadBuffer(test_Read, 0x01, 5);
    delay_ms(10);
    for(i = 0; i < 5; i++)
    {
        printf("The Read Data is %d\n", test_Read);
    }
   
}

但是出现写完成后,读数始终为ff的结果
现在有点迷糊,我该如何判断是写函数出问题还是读函数出问题,或者是都有问题呢?
请各位大大指教

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

发表于 2015-8-4 12:02:23 | 显示全部楼层
不清楚STM8,反正STM32的I2C我不敢用

出0入0汤圆

发表于 2017-2-17 10:55:22 | 显示全部楼层
我正用stm32,有问题
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-9 07:17

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表