将以上代码修改成直接操作寄存器版本 // 配置I2C1寄存器 I2C_InitTypeDef I2C_InitStruct; I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStruct.I2C_OwnAddress1 = 0x00; I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStruct.I2C_ClockSpeed = 100000; I2C_Init(I2C1, &I2C_InitStruct);
时间: 2023-05-31 10:02:31 浏览: 335
// 定义寄存器地址
#define I2C1_CR1_ADDR ((uint32_t)0x40005400)
#define I2C1_CR2_ADDR ((uint32_t)0x40005404)
#define I2C1_OAR1_ADDR ((uint32_t)0x40005408)
#define I2C1_OAR2_ADDR ((uint32_t)0x4000540C)
#define I2C1_DR_ADDR ((uint32_t)0x40005410)
#define I2C1_SR1_ADDR ((uint32_t)0x40005414)
#define I2C1_SR2_ADDR ((uint32_t)0x40005418)
#define I2C1_CCR_ADDR ((uint32_t)0x4000541C)
#define I2C1_TRISE_ADDR ((uint32_t)0x40005420)
// 配置I2C1寄存器
*((uint32_t*)I2C1_CR1_ADDR) = 0x00000000;
*((uint32_t*)I2C1_CR2_ADDR) = 0x00000000;
*((uint32_t*)I2C1_OAR1_ADDR) = 0x00000000;
*((uint32_t*)I2C1_OAR2_ADDR) = 0x00000000;
// 配置I2C_CR1寄存器
*((uint32_t*)I2C1_CR1_ADDR) &= ~(1 << 15);
*((uint32_t*)I2C1_CR1_ADDR) &= ~(1 << 10);
*((uint32_t*)I2C1_CR1_ADDR) &= ~(1 << 9);
*((uint32_t*)I2C1_CR1_ADDR) &= ~(1 << 8);
*((uint32_t*)I2C1_CR1_ADDR) |= (1 << 0);
// 配置I2C_CR2寄存器
*((uint32_t*)I2C1_CR2_ADDR) &= ~(0xFFFF << 0);
*((uint32_t*)I2C1_CR2_ADDR) |= (0x08 << 0);
// 配置I2C_OAR1寄存器
*((uint32_t*)I2C1_OAR1_ADDR) &= ~(0x7F << 1);
*((uint32_t*)I2C1_OAR1_ADDR) |= (0x00 << 1);
*((uint32_t*)I2C1_OAR1_ADDR) &= ~(1 << 14);
// 配置I2C_CCR寄存器
*((uint32_t*)I2C1_CCR_ADDR) &= ~(0xFFFF << 0);
*((uint32_t*)I2C1_CCR_ADDR) |= (0x50 << 0);
*((uint32_t*)I2C1_CCR_ADDR) &= ~(0xF << 12);
*((uint32_t*)I2C1_CCR_ADDR) &= ~(0x3 << 14);
*((uint32_t*)I2C1_CCR_ADDR) |= (0x2 << 14);
// 配置I2C_TRISE寄存器
*((uint32_t*)I2C1_TRISE_ADDR) &= ~(0xFF << 0);
*((uint32_t*)I2C1_TRISE_ADDR) |= (0x09 << 0);
阅读全文