面向对象iic

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值