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

2019-12-31 19:17发布

在使用STM8L与EEPROM芯片AT24C512通信的时候,出现了一点问题
因为STM8和STM32的库函数相似,所以参考了野火教程里EEPROM的代码,修改移植到STM8L上
STM8L_I2C.zip (2.69 KB, 下载次数: 4) 2015-8-4 09:54 上传 点击文件名下载附件

读函数
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 ", test_Write);
    }
   
    EEPROM_WriteBuffer(test_Write, EEPROM_Firstpage, 5);
    printf("Write success! ");
    delay_ms(50);
    printf("Read Data ");
    delay_ms(10);
    EEPROM_ReadBuffer(test_Read, 0x01, 5);
    delay_ms(10);
    for(i = 0; i < 5; i++)
    {
        printf("The Read Data is %d ", test_Read);
    }
   
}

但是出现写完成后,读数始终为ff的结果
现在有点迷糊,我该如何判断是写函数出问题还是读函数出问题,或者是都有问题呢?
请各位大大指教
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。