IIC操作EEPROM

[复制链接]
4537|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:


uint8_t I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)
{
        uint32_t timeout;
        
        timeout=0;
        while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
        {
                if(timeout++>=I2C_TIMEOUT)
                {
                        return 0;
                }
        }
        
        /* Send START condition */
        I2C_GenerateSTART(I2C1, ENABLE);
        /* Test on EV5 and clear it */
        timeout=0;
        while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
        {
                if(timeout++>=I2C_TIMEOUT)
                {
                        return 0;
                }
        }
        
        /* Send EEPROM address for write */
        I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter);
        /* Test on EV6 and clear it */
        timeout=0;
        while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
        {
                if(timeout++>=I2C_TIMEOUT)
                {
                        return 0;
                }
        }
        
        /* Send the EEPROM's internal address to write to */   
        I2C_SendData(I2C1, WriteAddr);  
        /* Test on EV8 and clear it */
        timeout=0;
        while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
        {
                if(timeout++>=I2C_TIMEOUT)
                {
                        return 0;
                }
        }
        
        /* While there is data to be written */
        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 */
                timeout=0;
                while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
                {
                        if(timeout++>=I2C_TIMEOUT)
                        {
                                return 0;
                        }
                }
        }
        
        /* Send STOP condition */
        I2C_GenerateSTOP(I2C1, ENABLE);
        timeout=0;
        while(I2C1->CR1&0x0200)
        {
                if(timeout++>=I2C_TIMEOUT)
                {
                        return 0;
                }
        }

        return 1;
}




判断EEPROM处于StandBy状态:

uint8_t I2C_EE_WaitEepromStandbyState(void)      
{
uint32_t timeout;
        
        while(1)
        {
                timeout=0;
                while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
                {
                        if(timeout++>=I2C_TIMEOUT)
                        {
                                return 0;
                        }
                }
               
                /* Send START condition */
                I2C_GenerateSTART(I2C1, ENABLE);
                /* Test on EV5 and clear it */
                timeout=0;
                while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
                {
                        if(timeout++>=I2C_TIMEOUT)
                        {
                                return 0;
                        }
                }
               
                /* Send EEPROM address for write */
                I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter);
                /* Test on EV6 and clear it */
                timeout=0;
                while( (!I2C_GetFlagStatus(I2C1, I2C_FLAG_ADDR)) && (!I2C_GetFlagStatus(I2C1, I2C_FLAG_AF)) );
                {
                        if(timeout++>=I2C_TIMEOUT)
                        {
                                return 0;
                        }
                }
                if(I2C_GetFlagStatus(I2C1, I2C_FLAG_ADDR))
                {
                        break;
                }
                if(I2C_GetFlagStatus(I2C1, I2C_FLAG_AF))
                {
                        I2C_ClearFlag(I2C1,I2C_FLAG_AF);
                        I2C_GenerateSTOP(I2C1, ENABLE);
                        timeout=0;
                        while(I2C1->CR1&0x0200)
                        {
                                if(timeout++>=I2C_TIMEOUT)
                                {
                                        return 0;
                                }
                        }
                }
        }

        timeout=0;
        while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
        {
                if(timeout++>=I2C_TIMEOUT)
                {
                        return 0;
                }
        }

        I2C_GenerateSTOP(I2C1, ENABLE);
        timeout=0;
        while(I2C1->CR1&0x0200)
        {
                if(timeout++>=I2C_TIMEOUT)
                {
                        return 0;
                }
        }

        return 1;
}



IIC-EEPROM.rar

5.48 KB

tennis| | 2013-6-20 16:22 | 显示全部楼层
沙发

使用特权

评论回复
shenpingbing| | 2013-8-28 19:53 | 显示全部楼层
不错不错  留着以后备用了

使用特权

评论回复
qin552011373| | 2013-8-30 21:12 | 显示全部楼层
一般情况下  比较喜欢耍模拟的IIC   因为通用型比较好一点   要求速度才搞起硬件的IIC

使用特权

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

本版积分规则

1

主题

9

帖子

0

粉丝