#ifndef I2C_H
#define I2C_H
#include "stm32f10x.h"
typedef struct {
I2C_TypeDef* I2Cx; // 指向I2C外设的指针
uint32_t ClockSpeed; // I2C时钟速度
uint16_t OwnAddress; // I2C设备地址
void (*Init)(struct I2C* self);
void (*Write)(struct I2C* self, uint8_t addr, uint8_t reg, uint8_t data);
uint8_t (*Read)(struct I2C* self, uint8_t addr, uint8_t reg);
} I2C;
// 函数声明
void I2C_Init(I2C* self);
void I2C_Write(I2C* self, uint8_t addr, uint8_t reg, uint8_t data);
uint8_t I2C_Read(I2C* self, uint8_t addr, uint8_t reg);
#endif // I2C_H
#include "i2c.h"
// 初始化函数
void I2C_Init(I2C* self) {
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// 初始化I2C GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // I2C1_SCL 和 I2C1_SDA
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 初始化I2C
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = self->OwnAddress;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = self->ClockSpeed;
I2C_Init(self->I2Cx, &I2C_InitStructure);
I2C_Cmd(self->I2Cx, ENABLE);
}
// 写数据函数
void I2C_Write(I2C* self, uint8_t addr, uint8_t reg, uint8_t data) {
// 开始传输
I2C_GenerateSTART(self->I2Cx, ENABLE);
while (!I2C_CheckEvent(self->I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
// 发送设备地址
I2C_Send7bitAddress(self->I2Cx, addr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(self->I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
// 发送寄存器地址
I2C_SendData(self->I2Cx, reg);
while (!I2C_CheckEvent(self->I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING));
// 发送数据
I2C_SendData(self->I2Cx, data);
while (!I2C_CheckEvent(self->I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// 停止传输
I2C_GenerateSTOP(self->I2Cx, ENABLE);
}
// 读数据函数
uint8_t I2C_Read(I2C* self, uint8_t addr, uint8_t reg) {
uint8_t data;
// 开始传输
I2C_GenerateSTART(self->I2Cx, ENABLE);
while (!I2C_CheckEvent(self->I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
// 发送设备地址
I2C_Send7bitAddress(self->I2Cx, addr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(self->I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
// 发送寄存器地址
I2C_SendData(self->I2Cx, reg);
while (!I2C_CheckEvent(self->I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING));
// 重新开始传输
I2C_GenerateSTART(self->I2Cx, ENABLE);
while (!I2C_CheckEvent(self->I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
// 发送设备地址(读)
I2C_Send7bitAddress(self->I2Cx, addr, I2C_Direction_Receiver);
while (!I2C_CheckEvent(self->I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
// 接收数据
while (!I2C_CheckEvent(self->I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));
data = I2C_ReceiveData(self->I2Cx);
// 停止传输
I2C_GenerateSTOP(self->I2Cx, ENABLE);
return data;
}
// I2C对象创建函数
I2C I2C_Create(I2C_TypeDef* I2Cx, uint32_t ClockSpeed, uint16_t OwnAddress) {
I2C i2c;
i2c.I2Cx = I2Cx;
i2c.ClockSpeed = ClockSpeed;
i2c.OwnAddress = OwnAddress;
i2c.Init = I2C_Init;
i2c.Write = I2C_Write;
i2c.Read = I2C_Read;
return i2c;
}