/*********文件名:i2c_ee.h**********/
/* Define to prevent recursive inclusion ------------------------------------ */
#ifndef __I2C_EE_H
#define __I2C_EE_H
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
/* Exported macro ------------------------------------------------------------*/
#define ADDR_24CXX 0xA0
#define SCLH GPIOB->BSRR = GPIO_Pin_6
#define SCLL GPIOB->BRR = GPIO_Pin_6
#define SDAH GPIOB->BSRR = GPIO_Pin_7
#define SDAL GPIOB->BRR = GPIO_Pin_7
#define SCLread GPIOB->IDR & GPIO_Pin_6
#define SDAread GPIOB->IDR & GPIO_Pin_7
/* Exported functions ------------------------------------------------------- */
void I2C_EE_Init(void);
uint8_t I2C_EE_BufferWrite(uint8_t *psrc_data,uint8_t adr,uint8_t nbyte);
uint8_t I2C_EE_BufferRead(uint8_t *pdin_data,uint8_t adr,uint8_t nbyte);
#endif /* __I2C_EE_H */
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/*********文件名:i2c_ee.c**********/
#include "i2c_ee.h"
enum ENUM_TWI_REPLY
{
TWI_NACK=0
,TWI_ACK=1
};
enum ENUM_TWI_BUS_STATE
{
TWI_READY=0
,TWI_BUS_BUSY=1
,TWI_BUS_ERROR=2
};
void I2C_EE_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// Configure I2C1 pins: SCL and SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void TWI_delay(void)
{
uint8_t i=10; //i=10延时1.5us//这里可以优化速度 ,经测试最低到5还能写入
while(i-

本文档提供了STM32F103通过模拟I2C与24C02 EEPROM进行读写的实现细节。包括初始化函数、延时函数以及读写函数的定义,涉及I2C总线状态判断、数据传输等关键步骤。

263

被折叠的 条评论
为什么被折叠?



