IIC操作EEPROM

[复制链接]
5641|3
 楼主| GDCM3OS 发表于 2013-6-20 10:32 | 显示全部楼层 |阅读模式
本帖最后由 GDCM3OS 于 2013-6-20 10:34 编辑

看到经常讨论使用IIC操作EEPROM经常遇到问题,根据实操,总结注意以下个问题。

1、IIC有4种工作模式。Slave transmitter,Slave receiver,Master transmitter,Master receiver。
2、每种工作模式对应若干事件标志,可以猜想芯片设计内部对应若干状态机。
3、每种事件标志的相应处理动作序列,要求有比较严密的步骤和时序。
4、具体应用还需注意处理意外情况。

程序实例,标的EE芯片AT24C02C。

写数据到EEPROM:


  1. uint8_t I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)
  2. {
  3.         uint32_t timeout;
  4.         
  5.         timeout=0;
  6.         while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
  7.         {
  8.                 if(timeout++>=I2C_TIMEOUT)
  9.                 {
  10.                         return 0;
  11.                 }
  12.         }
  13.         
  14.         /* Send START condition */
  15.         I2C_GenerateSTART(I2C1, ENABLE);
  16.         /* Test on EV5 and clear it */
  17.         timeout=0;
  18.         while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
  19.         {
  20.                 if(timeout++>=I2C_TIMEOUT)
  21.                 {
  22.                         return 0;
  23.                 }
  24.         }
  25.         
  26.         /* Send EEPROM address for write */
  27.         I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter);
  28.         /* Test on EV6 and clear it */
  29.         timeout=0;
  30.         while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  31.         {
  32.                 if(timeout++>=I2C_TIMEOUT)
  33.                 {
  34.                         return 0;
  35.                 }
  36.         }
  37.         
  38.         /* Send the EEPROM's internal address to write to */   
  39.         I2C_SendData(I2C1, WriteAddr);  
  40.         /* Test on EV8 and clear it */
  41.         timeout=0;
  42.         while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  43.         {
  44.                 if(timeout++>=I2C_TIMEOUT)
  45.                 {
  46.                         return 0;
  47.                 }
  48.         }
  49.         
  50.         /* While there is data to be written */
  51.         while(NumByteToWrite--)  
  52.         {
  53.                 /* Send the current byte */
  54.                 I2C_SendData(I2C1, *pBuffer);
  55.                
  56.                 /* Point to the next byte to be written */
  57.                 pBuffer++;
  58.                
  59.                 /* Test on EV8 and clear it */
  60.                 timeout=0;
  61.                 while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  62.                 {
  63.                         if(timeout++>=I2C_TIMEOUT)
  64.                         {
  65.                                 return 0;
  66.                         }
  67.                 }
  68.         }
  69.         
  70.         /* Send STOP condition */
  71.         I2C_GenerateSTOP(I2C1, ENABLE);
  72.         timeout=0;
  73.         while(I2C1->CR1&0x0200)
  74.         {
  75.                 if(timeout++>=I2C_TIMEOUT)
  76.                 {
  77.                         return 0;
  78.                 }
  79.         }

  80.         return 1;
  81. }




判断EEPROM处于StandBy状态:

  1. uint8_t I2C_EE_WaitEepromStandbyState(void)      
  2. {
  3. uint32_t timeout;
  4.         
  5.         while(1)
  6.         {
  7.                 timeout=0;
  8.                 while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
  9.                 {
  10.                         if(timeout++>=I2C_TIMEOUT)
  11.                         {
  12.                                 return 0;
  13.                         }
  14.                 }
  15.                
  16.                 /* Send START condition */
  17.                 I2C_GenerateSTART(I2C1, ENABLE);
  18.                 /* Test on EV5 and clear it */
  19.                 timeout=0;
  20.                 while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
  21.                 {
  22.                         if(timeout++>=I2C_TIMEOUT)
  23.                         {
  24.                                 return 0;
  25.                         }
  26.                 }
  27.                
  28.                 /* Send EEPROM address for write */
  29.                 I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter);
  30.                 /* Test on EV6 and clear it */
  31.                 timeout=0;
  32.                 while( (!I2C_GetFlagStatus(I2C1, I2C_FLAG_ADDR)) && (!I2C_GetFlagStatus(I2C1, I2C_FLAG_AF)) );
  33.                 {
  34.                         if(timeout++>=I2C_TIMEOUT)
  35.                         {
  36.                                 return 0;
  37.                         }
  38.                 }
  39.                 if(I2C_GetFlagStatus(I2C1, I2C_FLAG_ADDR))
  40.                 {
  41.                         break;
  42.                 }
  43.                 if(I2C_GetFlagStatus(I2C1, I2C_FLAG_AF))
  44.                 {
  45.                         I2C_ClearFlag(I2C1,I2C_FLAG_AF);
  46.                         I2C_GenerateSTOP(I2C1, ENABLE);
  47.                         timeout=0;
  48.                         while(I2C1->CR1&0x0200)
  49.                         {
  50.                                 if(timeout++>=I2C_TIMEOUT)
  51.                                 {
  52.                                         return 0;
  53.                                 }
  54.                         }
  55.                 }
  56.         }

  57.         timeout=0;
  58.         while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  59.         {
  60.                 if(timeout++>=I2C_TIMEOUT)
  61.                 {
  62.                         return 0;
  63.                 }
  64.         }

  65.         I2C_GenerateSTOP(I2C1, ENABLE);
  66.         timeout=0;
  67.         while(I2C1->CR1&0x0200)
  68.         {
  69.                 if(timeout++>=I2C_TIMEOUT)
  70.                 {
  71.                         return 0;
  72.                 }
  73.         }

  74.         return 1;
  75. }



IIC-EEPROM.rar

5.48 KB, 下载次数: 66

tennis 发表于 2013-6-20 16:22 | 显示全部楼层
shenpingbing 发表于 2013-8-28 19:53 | 显示全部楼层
不错不错  留着以后备用了
qin552011373 发表于 2013-8-30 21:12 | 显示全部楼层
一般情况下  比较喜欢耍模拟的IIC   因为通用型比较好一点   要求速度才搞起硬件的IIC
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

9

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部