环境为KEIL5
.h文件
#ifndef _I2C_EE_24C256_H
#define _I2C_EE_24C256_H
#include "stm32f4xx_i2c.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include <stdio.h>
#define I2C_OWNADDR 0X0A
#define I2C_CLOCK_SPEED 400000
#define I2C_PageSize 64
//定义用哪个I2C
#define EEPROM_I2C1 I2C1
#define EEPROM_I2C1_CLK RCC_APB1Periph_I2C1
//定义GPIO相关
//SDA PB7 165
#define EEPROM_I2C1_SDA_GPIO_PORT GPIOB
#define EEPROM_I2C1_SDA_PIN GPIO_Pin_7
#define EEPROM_I2C1_SDA_GPIO_CLK RCC_AHB1Periph_GPIOB
#define EEPROM_I2C1_SDA_GPIO_SOURCE GPIO_PinSource7
#define EEPROM_I2C1_SDA_AF GPIO_AF_I2C1
//SCL PB8 167
#define EEPROM_I2C1_SCL_GPIO_PORT GPIOB
#define EEPROM_I2C1_SCL_PIN GPIO_Pin_8
#define EEPROM_I2C1_SCL_GPIO_CLK RCC_AHB1Periph_GPIOB
#define EEPROM_I2C1_SCL_GPIO_SOURCE GPIO_PinSource8
#define EEPROM_I2C1_SCL_AF GPIO_AF_I2C1
#define EEPROM_ADDRESS 0XA0
/*等待超时时间*/
#define I2CT_FLAG_TIMEOUT ((uint32_t)0x1000)
#define I2CT_LONG_TIMEOUT ((uint32_t)(10 * I2CT_FLAG_TIMEOUT))
/*信息输出,可设置输出模式以及debug开关*/
#define EEPROM_DEBUG_ON 0
#define EEPROM_INFO(fmt,arg...) printf("<<-EEPROM-INFO->> "fmt"\n",##arg)
#define EEPROM_ERROR(fmt,arg...) printf("<<-EEPROM-ERROR->> "fmt"\n",##arg)
#define EEPROM_DEBUG(fmt,arg...) do{\
if(EEPROM_DEBUG_ON)\
printf("<<-EEPROM-DEBUG->> [%d]"fmt"\n",__LINE__, ##arg);\
}while(0)
void EEPROM_GPIO_Init(void);
uint32_t I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite);
void I2C_EE_WaitEepromStandbyState(void) ;
uint32_t I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead);
uint32_t I2C_EE_ByteWrite(u8* pBuffer, u8 WriteAddr);
void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite);
uint8_t I2C_Test(void);
#endif
.c文件
#include "i2c_ee_24c256.h"
uint8_t I2c_Buf_Write[128];
uint8_t I2c_Buf_Read[128];
uint8_t I2c_Buf_Read1[128];
uint8_t EEP_Firstpage =0x00;
uint8_t EEP_SecondPage =0x40;
static __IO uint32_t I2CTimeout = I2CT_LONG_TIMEOUT;
static uint32_t I2C_TIMEOUT_UserCallback(uint8_t errorCode);
//gpio初始化
void EEPROM_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//使能I2C1的GPIO PB7 PB8时钟
//GPIO时钟
RCC_AHB1PeriphClockCmd(EEPROM_I2C1_SDA_GPIO_CLK|EEPROM_I2C1_SCL_GPIO_CLK,ENABLE);
//连接PB7到SDA-----复用功能
GPIO_PinAFConfig(EEPROM_I2C1_SDA_GPIO_PORT,EEPROM_I2C1_SDA_GPIO_SOURCE,EEPROM_I2C1_SDA_AF);
//连接PB8到SCL
GPIO_PinAFConfig(EEPROM_I2C1_SCL_GPIO_PORT,EEPROM_I2C1_SCL_GPIO_SOURCE,EEPROM_I2C1_SCL_AF);
//GPIO初始化
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;//上拉
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;//推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
//配置SDA为复用功能
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用
GPIO_InitStruct.GPIO_Pin = EEPROM_I2C1_SDA_PIN;
GPIO_Init(EEPROM_I2C1_SDA_GPIO_PORT,&GPIO_InitStruct);//初始化配置
//配置SCL为复用功能
GPIO_InitStruct.GPIO_Pin = EEPROM_I2C1_SCL_PIN;
GPIO_Init(EEPROM_I2C1_SCL_GPIO_PORT,&GPIO_InitStruct);
I2C_InitTypeDef I2C_InitStruct;
//I2C时钟
RCC_APB1PeriphClockCmd(EEPROM_I2C1_CLK,ENABLE);
I2C_InitStruct.I2C_Mode=I2C_Mode_I2C;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_ClockSpeed = I2C_CLOCK_SPEED;
I2C_InitStruct.I2C_OwnAddress1 = I2C_OWNADDR; //I2C1自己的地址
I2C_InitStruct.I2C_Ack = I2C_Ack_Enabl