⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 at24c16_i2c1.c

📁 源码
💻 C
字号:
#include "at24c16.h"
#include "CommonDefine.h"

#define I2C_SCL 0x0004    //p0.2
#define I2C_SDA 0x0008    //p0.3

/* i2C eeprom address example */
/*#define I2CEEPROMADDRESS0 0xA0       //第0页 256bytes
#define I2CEEPROMADDRESS1 0xA2
#define I2CEEPROMADDRESS2 0xA4
#define I2CEEPROMADDRESS3 0xA6
*/
/* If the user wants to use a specific I2C eeprom, he has to define below
 in the commented line its address and uncomment the line before.*/
//#define I2CEEPROMADDRESS0 xxxxxxx0 /* xxxxxxx depends on the selected memory */

//#define STRING "STR7 I2C EEPROM"
#define STRING "5555555555555555"
//receives a data word, it responds with an acknowledge. As long as the E2PROM receives an acknowledge, it will continue to increment"
//the data word address and serially clock out sequential data words. When the memory address limit is reached, the data word address will “roll over” and the sequential read will continue.The sequential read operation is terminated when the microcontroller does not respond with a zero but does generate a following stop condition (refer to Figure 6)."
#define Buffer_Size (sizeof(STRING)-1)

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
char *Tx_Data = STRING;
//char  Rx_Data[Buffer_Size] = "";
char  Rx_Data[256] = "";
u8 i;
u32 I2CStatus;
/* Private function prototypes -----------------------------------------------*/
void EEPROM_Receive(I2C_TypeDef *I2Cx, char *PtrToBuffer,u8 NbOfBytes,
                    u8 InternalAddress,u8 I2CAddr);
void EEPROM_WaitForLastTask(u8 I2CAddr);
void EEPROM_Send(I2C_TypeDef *I2Cx,char *PtrToBuffer, u8 NbOfBytes,
                 u8 InternalAddress, u8 I2CAddr);
void I2C1_Config(void);
/* Interface functions -------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


void At24c16ReadAndWrite(void)
{
  /* Configure I2C1 interface */
  I2C1_Config();
  /* Send Buffer to the M24C08 */
  //EEPROM_Send(I2C1, Tx_Data, Buffer_Size, 0xf0,I2CEEPROMADDRESS2);  //按16字节存储
  /* Wait until the EEPROM completes the last operation */
  //EEPROM_WaitForLastTask(I2CEEPROMADDRESS2);

  while(1)
  {
    /* Read Buffer from the M24C08 */
    //EEPROM_Receive(I2C1, Rx_Data,Buffer_Size, 0x00, I2CEEPROMADDRESS0);
    EEPROM_Receive(I2C1, Rx_Data, 1, 0x00, I2CEEPROMADDRESS0);
  }
}
/**************************************************************************************************
** 函数名: GS_StoreContrast
** 输 入: void
** 输 出: void
** 功能描述:  本函数的功能是存储 LCD 对比度参数值;
** 说  明:	存储位置:page 0, addr 0x00, length 1;
** 作 者:	Simple
** 日 期: 	2004/11/25
** 修 改:
** 日 期:
** 版  本:  V0.0
**************************************************************************************************/
void GS_StoreContrast(u8 Data)
{
  char temp;
  temp = Data;
  I2C1_Config();
  EEPROM_Send(I2C1, &temp, 1, 0x00 ,I2CEEPROMADDRESS0);
  EEPROM_WaitForLastTask(I2CEEPROMADDRESS0);
}

/**************************************************************************************************
** 函数名: GS_ReadContrast
** 输 入: void
** 输 出: void
** 功能描述:  本函数的功能是读 LCD 对比度参数值; Eeprom按16字节存储数据.
** 说  明:	存储位置:page 0, addr 0x00, length 0x10; 取第一个字节就是存储的Contrast
** 作 者:	Simple
** 日 期: 	2004/11/25
** 修 改:
** 日 期:
** 版  本:  V0.0
**************************************************************************************************/
u8 GS_ReadContrast(void)
{
  char Data;
  // Configure I2C1 interface 
  I2C1_Config();
  EEPROM_Receive(I2C1, &Data, 0x4, 0x00 ,I2CEEPROMADDRESS0);  //取4个字节,其实只用第一个字节
  //if((Data > 0x32)||(Data <0x11))
  if((Data > 0x22)||(Data <0x01))  
  {
    //Data = 0x14+(Data%25);
    Data = 0x4+(Data%17); 
    GS_StoreContrast(Data); 	
  }
  return Data;
}

/**************************************************************************************************
** 函数名: GS_StoreLCDlight
** 输 入: void
** 输 出: void
** 功能描述:  本函数的功能是存储 LCD 对比度参数值;
** 说  明:	存储位置:page 0, addr 0x00, length 1;
** 作 者:	Simple
** 日 期: 	2004/11/25
** 修 改:
** 日 期:
** 版  本:  V0.0
**************************************************************************************************/
void GS_StoreLCDlight(u8 Data)
{
  char temp;
  temp = Data;
  I2C1_Config();
  EEPROM_Send(I2C1, &temp, 1, 0x10 ,I2CEEPROMADDRESS0);
  EEPROM_WaitForLastTask(I2CEEPROMADDRESS0);
}

u8 GS_ReadLCDlight(void)
{
  char Data;
  // Configure I2C1 interface 
  I2C1_Config();
  EEPROM_Receive(I2C1, &Data, 0x4, 0x10 ,I2CEEPROMADDRESS0);  //取4个字节,其实只用第一个字节
  return Data;
}
/*******************************************************************************
* Function Name  : EEPROM_WaitForLastTask
* Description    : Wait for I2C EEPROM to terminate the last task.
* Input          : None.
* Return         : None.
*******************************************************************************/
void EEPROM_WaitForLastTask(u8 I2CAddr)
{
  do
  {
    I2C_STARTGenerate( I2C1, ENABLE );
    while( I2C_FlagStatus( I2C1, DIRECT, I2C_SB) == RESET );
    I2C_AddressSend( I2C1, I2CAddr, I2C_Mode7, I2C_TX );
    while(!((I2CStatus = I2C_GetStatus( I2C1 ))& I2C_EVF));
    while( I2C_FlagStatus( I2C1, DIRECT, I2C_ENDAD ) ==RESET );
    I2C_FlagClear( I2C1, I2C_ENDAD );
  }while( I2C_FlagStatus( I2C1, INDIRECT, I2C_AF, I2CStatus ) == SET );
}

/*******************************************************************************
* Function Name  : EEPROM_Receive
* Description    : Read a buffer from the I2C EEPROM.
* Input          : I2Cx (I2C0 or I2C1).
*                  Buffer where read data are put.
*                  Buffer size.
*                  EEPROM internal address from which data will be read.
* Return         : None.
*******************************************************************************/

void EEPROM_Receive(I2C_TypeDef *I2Cx, char *PtrToBuffer,u8 NbOfBytes,
                    u8 InternalAddress,u8 I2CAddr)
{

  /* Generate the START condition */
  I2C_STARTGenerate (I2Cx, ENABLE);
  /* Wait until SB bit is set */
  while (I2C_FlagStatus (I2Cx, DIRECT, I2C_SB )== RESET);
  /* Send the EEPROM address with LSB bit reset */
  I2C_AddressSend (I2Cx, I2CAddr, I2C_Mode7, I2C_TX);
  /* Wait until ENDAD bit is set */
  while (I2C_FlagStatus (I2Cx, DIRECT, I2C_ENDAD )== RESET);
  /* Clear ENDAD bit */
  I2C_FlagClear (I2Cx, I2C_ENDAD);
  /* Send the EEPROM's internal address to read from */
  I2C_ByteSend (I2Cx, InternalAddress);
  /* Wait until BTF bit is set */
  while (I2C_FlagStatus (I2Cx, DIRECT, I2C_BTF )==RESET);
  /* Generate the RE-START condition */
  I2C_STARTGenerate (I2Cx, ENABLE);
  /* Wait until SB bit is set */
  while (I2C_FlagStatus (I2Cx, DIRECT, I2C_SB )==RESET);
  /* Send the EEPROM address with LSB bit set */
  I2C_AddressSend (I2Cx, I2CAddr, I2C_Mode7, I2C_RX);
  /* Wait until ENDAD bit is set */
  while (I2C_FlagStatus (I2Cx, DIRECT, I2C_ENDAD)==RESET);
  /* Clear ENDAD bit */
  I2C_FlagClear (I2Cx, I2C_ENDAD);

/* Read 'NbOfBytes' bytes from the EEPROM starting from address 'InternalAddress'
of Block3 and place them in PtrToBuffer[] array */

while(NbOfBytes)
{
   /* Wait until the byte is received */
   while (I2C_FlagStatus (I2Cx, DIRECT, I2C_BTF )==RESET);

  if(NbOfBytes==2)
  { /* Disable the ACK generation */
    I2C_AcknowledgeConfig (I2Cx, DISABLE);
  }

  if (NbOfBytes==1)
    {
  /* Generate STOP condition to close the communication after the
     next byte reception */
      I2C_STOPGenerate (I2Cx, ENABLE);
    }

  *PtrToBuffer=I2C_ByteReceive (I2Cx);
   PtrToBuffer++;
   NbOfBytes--;
}

  /* Enable the ACK generation */
  I2C_AcknowledgeConfig (I2Cx, ENABLE);
}


/*******************************************************************************
* Function Name  : EEPROM_Send
* Description    : Send data to the I2C EEPROM.
* Input          : I2Cx (I2C0 or I2C1).
*                  Buffer containing data to be written in the EEPROM.
*                  Buffer size.
*                  EEPROM internal address in which data will be written.
* Return         : None.
*******************************************************************************/
void EEPROM_Send(I2C_TypeDef *I2Cx, char *PtrToBuffer, u8 NbOfBytes,
                u8 InternalAddress ,u8 I2CAddr)
{

u8 SentBytes =0;

  /* Generate the START condition */
  I2C_STARTGenerate (I2Cx, ENABLE);
  /* Wait until SB bit is set */
  while (I2C_FlagStatus (I2Cx, DIRECT, I2C_SB)==RESET);
  /* Send the EEPROM's address with LSB bit reset */
  I2C_AddressSend (I2Cx, I2CAddr, I2C_Mode7, I2C_TX);
  /* Wait until ENDAD bit is set */
  while (I2C_FlagStatus (I2Cx, DIRECT, I2C_ENDAD)==RESET);
  /* Clear ENDAD bit */
  I2C_FlagClear (I2Cx, I2C_ENDAD);
  /* Send the EEPROM's internal address to write to */
  I2C_ByteSend (I2Cx,InternalAddress);
  /* Wait until BTF bit is set */
  while (I2C_FlagStatus (I2Cx, DIRECT, I2C_BTF )==RESET);
  /* Write 'PtrToBuffer' buffer contents in the EEPROM starting from address 'InternalAddress' of
  Block3 */

   while (SentBytes<NbOfBytes)
  {
    I2Cx->DR= *(PtrToBuffer+SentBytes);
    /* Wait till I2C_BTF bit is set */
    while (!(I2Cx->SR1 & 0x08));
    SentBytes++;
   }
   /* Generate the stop condition */
  I2C_STOPGenerate (I2Cx, ENABLE);

 }

/*******************************************************************************
* Function Name  : I2C1_Config
* Description    : Configure the I2C1 interface
* Input          : None
*******************************************************************************/

void I2C1_Config(void)
{
  /* Configure the SDA and the SCL lines to alternate functions Open Drain */
  GPIO_Config (GPIO0, I2C_SCL|I2C_SDA, GPIO_AF_OD);
  /* Initialize the I2C1 peripheral */
  I2C_Init (I2C1);
  /* Configure frequency bits */
  I2C_FCLKConfig (I2C1);
  /* Enable I2C1 peripheral */
  I2C_OnOffConfig (I2C1, ENABLE);
  /* Configure I2C1 clock speed */
  I2C_SpeedConfig (I2C1, 100000);
  //I2C_SpeedConfig (I2C1, 200000);
  /* Enable Acknowledge */
  I2C_AcknowledgeConfig (I2C1, ENABLE);
}

/******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -