<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32 硬件I2C EEPROM命令解析

          STM32 硬件I2C EEPROM命令解析

          作者: 時(shí)間:2016-11-21 來(lái)源:網(wǎng)絡(luò) 收藏
          void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite)
          {
          u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0;
          //寫入地址是每頁(yè)的第幾位
          Addr = WriteAddr % I2C_PageSize;
          //在開始的一頁(yè)要寫入的個(gè)數(shù)
          count = I2C_PageSize - Addr;
          //要寫入的頁(yè)數(shù)
          NumOfPage = NumByteToWrite / I2C_PageSize;
          //不足一頁(yè)的個(gè)數(shù)
          NumOfSingle = NumByteToWrite % I2C_PageSize;
          //EEPROM設(shè)為待命狀態(tài)
          I2C_EE_WaitEepromStandbyState();
          //寫入地址是頁(yè)的開始
          if (Addr == 0)
          {
          //數(shù)據(jù)小于一頁(yè)
          if (NumOfPage == 0)
          {
          //寫少于一頁(yè)的數(shù)據(jù)
          I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
          //EEPROM設(shè)為待命狀態(tài)
          I2C_EE_WaitEepromStandbyState();
          }
          //數(shù)據(jù)大于等于一頁(yè)
          else
          {
          while (NumOfPage--)//要寫入的頁(yè)數(shù)
          {
          //寫一頁(yè)的數(shù)據(jù)
          I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
          //EEPROM設(shè)為待命狀態(tài)
          I2C_EE_WaitEepromStandbyState();
          WriteAddr += I2C_PageSize;
          pBuffer += I2C_PageSize;
          }
          //剩余數(shù)據(jù)小于一頁(yè)
          if (NumOfSingle!=0)
          {
          //寫少于一頁(yè)的數(shù)據(jù)
          I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
          //EEPROM設(shè)為待命狀態(tài)
          I2C_EE_WaitEepromStandbyState();
          }
          }
          }
          //寫入地址不是頁(yè)的開始
          else
          {
          //數(shù)據(jù)小于一頁(yè)
          if (NumOfPage== 0)
          {
          //寫少于一頁(yè)的數(shù)據(jù)
          I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
          //EEPROM設(shè)為待命狀態(tài)
          I2C_EE_WaitEepromStandbyState();
          }
          //數(shù)據(jù)大于等于一頁(yè)
          else
          {
          NumByteToWrite -= count;
          //重新計(jì)算要寫入的頁(yè)數(shù)
          NumOfPage = NumByteToWrite / I2C_PageSize;
          //重新計(jì)算不足一頁(yè)的個(gè)數(shù)
          NumOfSingle = NumByteToWrite % I2C_PageSize;
          if (count != 0)
          {
          //將開始的空間寫滿一頁(yè)
          I2C_EE_PageWrite(pBuffer, WriteAddr, count);
          //EEPROM設(shè)為待命狀態(tài)
          I2C_EE_WaitEepromStandbyState();
          WriteAddr += count;
          pBuffer += count;
          }
          //要寫入的頁(yè)數(shù)
          while (NumOfPage--)
          {
          //寫一頁(yè)的數(shù)據(jù)
          I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
          //EEPROM設(shè)為待命狀態(tài)
          I2C_EE_WaitEepromStandbyState();
          WriteAddr += I2C_PageSize;
          pBuffer += I2C_PageSize;
          }
          //剩余數(shù)據(jù)小于一頁(yè)
          if (NumOfSingle != 0)
          {
          //寫少于一頁(yè)的數(shù)據(jù)
          I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
          //EEPROM設(shè)為待命狀態(tài)
          I2C_EE_WaitEepromStandbyState();
          }
          }
          }
          }
          void I2C_EE_ByteWrite(u8* pBuffer, u8 WriteAddr)
          {
          //產(chǎn)生 I2C2傳輸 START條件
          I2C_GenerateSTART(I2C2, ENABLE);
          //通過START的產(chǎn)生,確定主機(jī)模式是否成功(檢測(cè)EV5)
          while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
          //發(fā)送器件地址(寫)
          I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
          //檢測(cè)主機(jī)傳輸模式是否成功(檢測(cè)EV6)
          while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
          //通過外設(shè) I2C2發(fā)送地址
          I2C_SendData(I2C2, WriteAddr);
          //檢測(cè)主機(jī)發(fā)送的字節(jié)是否成功(檢測(cè)EV8)
          while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
          //通過外設(shè) I2C2發(fā)送數(shù)據(jù)
          I2C_SendData(I2C2, *pBuffer);
          //檢測(cè)主機(jī)發(fā)送的字節(jié)是否成功(檢測(cè)EV8)
          while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
          //產(chǎn)生 I2C2傳輸 STOP條件
          I2C_GenerateSTOP(I2C2, ENABLE);
          }
          void I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)
          {
          //產(chǎn)生 I2C2傳輸 START條件
          I2C_GenerateSTART(I2C2, ENABLE);
          //通過START的產(chǎn)生,確定主機(jī)模式是否成功(檢測(cè)EV5)
          while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
          //發(fā)送器件地址(寫)
          I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
          //檢測(cè)主機(jī)傳輸模式是否成功(檢測(cè)EV6)
          while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
          //通過外設(shè) I2C2發(fā)送地址
          I2C_SendData(I2C2, WriteAddr);
          //檢測(cè)主機(jī)發(fā)送的字節(jié)是否成功(檢測(cè)EV8)
          while (! I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
          //寫數(shù)據(jù)
          while (NumByteToWrite--)
          {
          //通過外設(shè) I2C2發(fā)送數(shù)據(jù)
          I2C_SendData(I2C2, *pBuffer);
          //指針++
          pBuffer++;
          //檢測(cè)主機(jī)發(fā)送的字節(jié)是否成功(檢測(cè)EV8)
          while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
          }
          //產(chǎn)生 I2C2 傳輸 STOP條件
          I2C_GenerateSTOP(I2C2, ENABLE);
          }
          void I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
          {
          //EEPROM設(shè)為待命狀態(tài)
          I2C_EE_WaitEepromStandbyState();
          //產(chǎn)生 I2C2傳輸 START條件
          I2C_GenerateSTART(I2C2, ENABLE);
          //通過START的產(chǎn)生,確定主機(jī)模式是否成功(檢測(cè)EV5)
          while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
          //在一個(gè)單一的數(shù)據(jù)傳輸情況下讀取數(shù)據(jù)之前禁用的ACK
          if (NumByteToRead==1)
          {
          I2C_AcknowledgeConfig(I2C2, DISABLE);//失能I2C2 的應(yīng)答功能
          }
          //向指定的從 I2C設(shè)備傳送地址字,選擇發(fā)送方向
          I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
          //檢測(cè)主機(jī)傳輸模式是否成功(檢測(cè)EV6)
          while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
          //使能I2C外設(shè)
          I2C_Cmd(I2C2, ENABLE);
          //通過外設(shè) I2C2發(fā)送地址
          I2C_SendData(I2C2, ReadAddr);
          //檢測(cè)主機(jī)發(fā)送的字節(jié)是否成功(檢測(cè)EV8)
          while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
          //產(chǎn)生 I2C2 傳輸 START條件
          I2C_GenerateSTART(I2C2, ENABLE);
          //通過START的產(chǎn)生,確定主機(jī)模式是否成功(檢測(cè)EV5)
          while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
          //向指定的從 I2C設(shè)備傳送地址字,選擇接收方向
          I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Receiver);
          //檢測(cè)主機(jī)接收模式是否成功(檢測(cè)EV6)
          while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
          //讀取數(shù)據(jù)
          while (NumByteToRead)
          {
          //檢測(cè)主機(jī)接收的字節(jié)是否成功(檢測(cè)EV8)
          if (I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED))
          {
          if (NumByteToRead == 2)
          {
          //使能或者失能指定 I2C的應(yīng)答功能
          I2C_AcknowledgeConfig(I2C2, DISABLE);
          }
          if (NumByteToRead == 1)
          {
          //產(chǎn)生 I2C2 傳輸 STOP條件
          I2C_GenerateSTOP(I2C2, ENABLE);
          }
          //返回通過 I2C2最近接收的數(shù)據(jù)
          *pBuffer = I2C_ReceiveData(I2C2);
          //指向下個(gè)地址
          pBuffer++;
          NumByteToRead--;
          }
          }
          //使能I2C2 的應(yīng)答功能
          I2C_AcknowledgeConfig(I2C2, ENABLE);
          }
          void I2C_EE_WaitEepromStandbyState(void)
          {
          vu16 SR1_Tmp = 0;
          do
          {
          //產(chǎn)生 I2C2傳輸 START條件
          I2C_GenerateSTART(I2C2, ENABLE);
          //讀取指定的 I2C寄存器 I2C_SR1 并返回其值
          SR1_Tmp = I2C_ReadRegister(I2C2, I2C_Register_SR1);
          //向指定的從 I2C設(shè)備傳送地址字 ,選擇發(fā)送方向
          I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
          } while (!(I2C_ReadRegister(I2C2, I2C_Register_SR1) & 0x0002));//地址發(fā)送結(jié)束
          //清除 I2Cx的應(yīng)答錯(cuò)誤標(biāo)志位
          I2C_ClearFlag(I2C2, I2C_FLAG_AF);
          }



          關(guān)鍵詞: STM32硬件I2CEEPROM命令解

          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();