#include "i2c.h" // 初始化软件 I2C void i2cInit(void){ /* 初始化需要操作的 I2C IO */ GPIO_InitTypeDef GPIO_InitStruct; // enable gpiob for rcc_ahb1 I2C_RCC_CLOCK_CMD; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN; GPIO_Init(I2C_GROUP,&GPIO_InitStruct); // 初始化 I2C 引脚为高电平 GPIO_SetBits(I2C_GROUP, I2C_SCL_PIN); GPIO_SetBits(I2C_GROUP, I2C_SDA_PIN); // tim2 init RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; TIM_TimeBaseStructure.TIM_Prescaler = 83; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_Cmd(TIM2, ENABLE); // 初始化完毕后等待 1ms 可酌情删除 //i2cDelay_us(1000); } /* us 级延时 */ void i2cDelay_us(uint32_t us){ TIM_SetCounter(TIM2, 0); // wait counter to target while(TIM_GetCounter(TIM2) < us); } /* 基本控制函数 */ // 总线电平控制 void i2cCon_SCL(uint8_t BitValue){ GPIO_WriteBit(I2C_GROUP, I2C_SCL_PIN, (BitAction)(BitValue)); i2cDelay_us(i2cSpeedDelay); } void i2cCon_SDA(uint8_t BitValue){ GPIO_WriteBit(I2C_GROUP, I2C_SDA_PIN, (BitAction)(BitValue)); i2cDelay_us(i2cSpeedDelay); } // 读取 SDA 数据 uint8_t i2cReceive_SDA(void){ uint8_t bitValue = GPIO_ReadInputDataBit(I2C_GROUP, I2C_SDA_PIN); i2cDelay_us(i2cSpeedDelay); return bitValue; } /* 基本时序单元 */ // 开始信号和结束信号 void i2cSignal_Start(void){ i2cCon_SDA(1); i2cCon_SCL(1); i2cCon_SDA(0); i2cCon_SCL(0); } void i2cSignal_Stop(void){ i2cCon_SDA(0); i2cCon_SCL(1); i2cCon_SDA(1); } /* 数据收发 */ // 发送一个字节 void i2cSend_Byte(uint8_t byte){ for(int i = 0; i < 8; i++){ i2cCon_SDA(!!(byte & (0x80 >> i))); i2cCon_SCL(1); i2cCon_SCL(0); } } // 接收一个字节 uint8_t i2cReceive_Byte(void){ uint8_t rByte = 0x00; i2cCon_SDA(1); for(int i = 0; i < 8; i++){ i2cCon_SCL(1); if(i2cReceive_SDA() == 1){ rByte |= (0x80 >> i); } i2cCon_SCL(0); } return rByte; } /* 应答位操作 */ void i2cSend_ACK(uint8_t ackBit){ i2cCon_SDA(ackBit); i2cCon_SCL(1); i2cCon_SCL(0); } uint8_t i2cReceive_ACK(void){ i2cCon_SDA(1); i2cCon_SCL(1); uint8_t ackBit = i2cReceive_SDA(); i2cCon_SCL(0); return ackBit; }