打印

关于STM32 I2C读写外设EEPROM器件AT24C02的问题

[复制链接]
306|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
香菇选手|  楼主 | 2018-7-3 21:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有ISO例程或者《STM32库开发实战指南》的朋友可以一起来看一下。我在运行自己写的程序或者例程时都会出现一个奇怪的现象。
程序有点长,看过书或者看过例程的朋友主要看重点就可以了,以下是例程:
#include "stm32f10x_conf.h"   
#include "stdio.h"

#define EEPROM_Block0_ADDRESS 0xA0;
#define I2C1_OWN_ADDRESS7 0x0A
#define EEP_Fristpage 0x00
#define I2C_Speed 400000
#define I2C_PageSize 8

uint16_t EEPORM_ADDRESS;

u8 I2C_Buf_Write[256];
u8 I2C_Buf_Read[256];

static void I2C_GPIO_Config(void)//端口配置,这不是重点,可以跳过
{
  GPIO_InitTypeDef  GPIO_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);  

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
}

static void I2C_Mode_Configu(void)//配置I2C,这也不是重点,可以跳过
{
  I2C_InitTypeDef  I2C_InitStructure;

  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
        
  I2C_InitStructure.I2C_OwnAddress1 =I2C1_OWN_ADDRESS7;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable ;

  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  I2C_InitStructure.I2C_ClockSpeed = I2C_Speed;

  I2C_Init(I2C1, &I2C_InitStructure);

  I2C_Cmd(I2C1, ENABLE);   
}

void I2C_EE_Init(void)//I2C初始化,不是问题重点
{

  I2C_GPIO_Config();

  I2C_Mode_Configu();
}

void I2C_EE_ByteWrite(u8* pBuffer, u8 WriteAddr)
{
  I2C_GenerateSTART(I2C1, ENABLE);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));  

  I2C_Send7bitAddress(I2C1,EEPORM_ADDRESS, I2C_Direction_Transmitter);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

  I2C_SendData(I2C1, WriteAddr);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  I2C_SendData(I2C1, *pBuffer);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  I2C_GenerateSTOP(I2C1, ENABLE);
}

void I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)//写一页数据,问题在这个程序中
{
  while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));

  I2C_GenerateSTART(I2C1, ENABLE);

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

  I2C_Send7bitAddress(I2C1,EEPORM_ADDRESS, I2C_Direction_Transmitter);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //仿真时,程序一直停在这里执行不下去

  I2C_SendData(I2C1, WriteAddr);  

  while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  while(NumByteToWrite--)  
  {
    I2C_SendData(I2C1, *pBuffer);

    pBuffer++;

    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
  }

  I2C_GenerateSTOP(I2C1, ENABLE);
}

void I2C_EE_WaitEepromStandbyState(void) //这段程序没问题     
{
  vu16 SR1_Tmp = 0;
  do
  {
    I2C_GenerateSTART(I2C1, ENABLE);

    SR1_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1);
    I2C_Send7bitAddress(I2C1,EEPORM_ADDRESS, I2C_Direction_Transmitter);
  }
        while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002));

  I2C_ClearFlag(I2C1, I2C_FLAG_AF);
  I2C_GenerateSTOP(I2C1, ENABLE);
}


//这段程序执行不下去的原因是因为里面调用了void I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)程序。
void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite)
{
  u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0;

  Addr = WriteAddr % I2C_PageSize;
  count = I2C_PageSize - Addr;
  NumOfPage =  NumByteToWrite / I2C_PageSize;
  NumOfSingle = NumByteToWrite % I2C_PageSize;

  if(Addr == 0)
  {
    if(NumOfPage == 0)
    {
      I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
      I2C_EE_WaitEepromStandbyState();
    }
    else  
    {
      while(NumOfPage--)
      {
        I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
        I2C_EE_WaitEepromStandbyState();
        WriteAddr +=  I2C_PageSize;
        pBuffer += I2C_PageSize;
      }

      if(NumOfSingle!=0)
      {
        I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
        I2C_EE_WaitEepromStandbyState();
      }
    }
  }
  else
  {
    if(NumOfPage== 0)
    {
      I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
      I2C_EE_WaitEepromStandbyState();
    }
    else
    {
      NumByteToWrite -= count;
      NumOfPage =  NumByteToWrite / I2C_PageSize;
      NumOfSingle = NumByteToWrite % I2C_PageSize;        

      if(count != 0)
      {  
        I2C_EE_PageWrite(pBuffer, WriteAddr, count);
        I2C_EE_WaitEepromStandbyState();
        WriteAddr += count;
        pBuffer += count;
      }

      while(NumOfPage--)
      {
        I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
        I2C_EE_WaitEepromStandbyState();
        WriteAddr +=  I2C_PageSize;
        pBuffer += I2C_PageSize;  
      }
      if(NumOfSingle != 0)
      {
        I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
        I2C_EE_WaitEepromStandbyState();
      }
    }
  }  
}

void I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{  
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));   

  I2C_GenerateSTART(I2C1, ENABLE);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

  I2C_Send7bitAddress(I2C1,EEPORM_ADDRESS, I2C_Direction_Transmitter);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

  I2C_Cmd(I2C1, ENABLE);

  I2C_SendData(I2C1, ReadAddr);  

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  I2C_GenerateSTART(I2C1, ENABLE);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

  I2C_Send7bitAddress(I2C1,EEPORM_ADDRESS, I2C_Direction_Receiver);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

  while(NumByteToRead)  
  {
    if(NumByteToRead == 1)
    {
      I2C_AcknowledgeConfig(I2C1, DISABLE);

      I2C_GenerateSTOP(I2C1, ENABLE);
    }

    if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))  
    {      

      *pBuffer = I2C_ReceiveData(I2C1);

      pBuffer++;

      NumByteToRead--;        
    }   
  }

  I2C_AcknowledgeConfig(I2C1, ENABLE);
}

void I2C_Test(void)
{
  u16 i;

  printf("写入数据

");

  for ( i=0; i<=255; i++ )
  {   
    I2C_Buf_Write = i;

    printf("0x%02X ", I2C_Buf_Write);
    if(i%16 == 15)   
        printf("

");   
   }

  I2C_EE_BufferWrite( I2C_Buf_Write,EEP_Fristpage, 256);

  printf("

读出数据

");

  I2C_EE_BufferRead(I2C_Buf_Read,EEP_Fristpage, 256);

  for (i=0; i<256; i++)
  {        
     if(I2C_Buf_Read != I2C_Buf_Write)
     {
        printf("0x%02X ", I2C_Buf_Read);
        printf("错误:I2C EEPROMD写入与读出的数据不一样

");
        return;
     }
    printf("0x%02X ", I2C_Buf_Read);
    if(i%16 == 15) printf("

");
  }
  printf("I2C(AT24C02)测试成功

");
}

哪位高手可以帮忙看一下吗,我现在很迷惘找不出问题所在,能不能知道我一下。我会很感谢你的。

使用特权

评论回复

相关帖子

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

本版积分规则

450

主题

462

帖子

0

粉丝