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

📄 at24c16.c

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

#define I2C_SCL 0x2000    //p1.13
#define I2C_SDA 0x4000    //p1.14

/* i2C eeprom address example */
#define I2CEEPROMADDRESS 0xA0       //第0页
#define I2CEEPROMADDRESS1 0xA2
#define I2CEEPROMADDRESS2 0xA4
#define I2CEEPROMADDRESS3 0xA6
#define I2CEEPROMADDRESS4 0xA8
#define I2CEEPROMADDRESS5 0xAA
#define I2CEEPROMADDRESS6 0xAC
#define I2CEEPROMADDRESS7 0xAE
/* 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 I2CEEPROMADDRESS xxxxxxx0 /* xxxxxxx depends on the selected memory */

//#define STRING "STR7 I2C EEPROM"
#define STRING "LVPENG52 SOHU C"
#define Buffer_Size (sizeof(STRING)-1)

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


void At24c16ReadAndWrite(void)
{
  /* Configure I2C0 interface */
  I2C0_Config();
  /* Send Buffer to the M24C08 */
  //EEPROM_Send(I2C0, Tx_Data,Buffer_Size, 0x00);
  /* Wait until the EEPROM completes the last operation */
  //EEPROM_WaitForLastTask();

  //while(1)
  //{
    /* Read Buffer from the M24C08 */
    EEPROM_Receive(I2C0, Rx_Data,Buffer_Size, 0x00);
  //}
}
/**************************************************************************************************
** 函数名: 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;
  I2C0_Config();
  EEPROM_Send(I2C0, &temp, 1, 0x00);
  EEPROM_WaitForLastTask();
}*/
/**************************************************************************************************
** 函数名: GS_ReadContrast
** 输 入: void
** 输 出: void
** 功能描述:  本函数的功能是读 LCD 对比度参数值;
** 说  明:	存储位置:page 0, addr 0x00, length 1;
** 作 者:	Simple
** 日 期: 	2004/11/25
** 修 改:
** 日 期:
** 版  本:  V0.0
**************************************************************************************************/
/*u8 GS_ReadContrast(void)
{
  char Data;
  // Configure I2C0 interface 
  I2C0_Config();
  EEPROM_Receive(I2C0, &Data, 1, 0x00);
  if((Data > 0x32)||(Data <0x11))
  {
    Data = 0x14+(Data%25); 
    GS_StoreContrast(Data); 	
  }
  return Data;
}*/

/*******************************************************************************
* Function Name  : EEPROM_WaitForLastTask
* Description    : Wait for I2C EEPROM to terminate the last task.
* Input          : None.
* Return         : None.
*******************************************************************************/
void EEPROM_WaitForLastTask(void)
{
  do
  {
    I2C_STARTGenerate( I2C0, ENABLE );
    while( I2C_FlagStatus( I2C0, DIRECT, I2C_SB) == RESET );
    I2C_AddressSend( I2C0, I2CEEPROMADDRESS, I2C_Mode7, I2C_TX );
    while(!((I2CStatus = I2C_GetStatus( I2C0 ))& I2C_EVF));
    while( I2C_FlagStatus( I2C0, DIRECT, I2C_ENDAD ) ==RESET );
    I2C_FlagClear( I2C0, I2C_ENDAD );
  }while( I2C_FlagStatus( I2C0, 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)
{

  /* 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, I2CEEPROMADDRESS, 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 (I2C0, I2CEEPROMADDRESS, 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 SentBytes =0;

  /* Generate the START condition */
  I2C_STARTGenerate (I2C0, ENABLE);
  /* Wait until SB bit is set */
  while (I2C_FlagStatus (I2C0, DIRECT, I2C_SB)==RESET);
  /* Send the EEPROM's address with LSB bit reset */
  I2C_AddressSend (I2C0, I2CEEPROMADDRESS, I2C_Mode7, I2C_TX);
  /* Wait until ENDAD bit is set */
  while (I2C_FlagStatus (I2C0, DIRECT, I2C_ENDAD)==RESET);
  /* Clear ENDAD bit */
  I2C_FlagClear (I2C0, I2C_ENDAD);
  /* Send the EEPROM's internal address to write to */
  I2C_ByteSend (I2C0,InternalAddress);
  /* Wait until BTF bit is set */
  while (I2C_FlagStatus (I2C0, 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 (I2C0, ENABLE);

 }

/*******************************************************************************
* Function Name  : I2C0_Config
* Description    : Configure the I2C0 interface
* Input          : None
*******************************************************************************/

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

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

⌨️ 快捷键说明

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