我想开启I2C1,使用PF6和PF7两个引脚,我参照I2C0配置了引脚,代码如下:
- void i2c_gpio_config(void)
- {
- /* enable GPIOB clock 启用GPIOB时钟*/
- rcu_periph_clock_enable(RCU_GPIOB);
- /* enable I2C0 clock 启用I2c0时钟*/
- rcu_periph_clock_enable(RCU_I2C0);
- /* connect PB6 to I2C0_SCL 将PB6连接到I2c0_SCL*/
- gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
- /* connect PB7 to I2C0_SDA 将PB7连接到I2c0_SDA*/
- gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
- /* configure GPIO pins of I2C0 配置I2c0的GPIO管脚*/
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_6);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
- gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_7);
- gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
- rcu_periph_clock_enable(RCU_GPIOF); //配置I2C1引脚
- rcu_periph_clock_enable(RCU_I2C1);
- gpio_af_set(GPIOF, GPIO_AF_0, GPIO_PIN_6); //将PF6连接到I2C1_SCL
- gpio_af_set(GPIOF, GPIO_AF_0, GPIO_PIN_7); //将PF7连接到I2C1_SDA
- gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_6); //配置I2c1的GPIO管脚
- gpio_output_options_set(GPIOF, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
- gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_7);
- gpio_output_options_set(GPIOF, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_7);
- }
复制代码
I2C的配置代码如下,现在就是不清楚I2C1的i2c_mode_addr_config()函数的最后一个参数是起什么作用,该如何取值?固件库中的说明是I2C地址,原代码I2C0的设置是0xA0,那I2C1的地址是多少呢?
- void i2c_config(void)
- {
- /* enable I2C clock 启用I2c时钟*/
- rcu_periph_clock_enable(RCU_I2C0);
- /* configure I2C clock 配置I2c时钟*/
- i2c_clock_config(I2C0,I2C0_SPEED,I2C_DTCY_2);
- /* configure I2C address 配置I2c地址*/
- i2c_mode_addr_config(I2C0,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,I2C0_SLAVE_ADDRESS7);
- /* enable I2C0 启用I2C0*/
- i2c_enable(I2C0);
- /* enable acknowledge 启用确认*/
- i2c_ack_config(I2C0,I2C_ACK_ENABLE);
- rcu_periph_clock_enable(RCU_I2C1); //启用I2C1时钟
- i2c_clock_config(I2C1,I2C0_SPEED,I2C_DTCY_2); //配置I2C1时钟,速度400000,占空比2
- i2c_mode_addr_config(I2C1,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,0xA0);//配置I2c地址,I2C模式0x82
- i2c_enable(I2C1); //启用I2C1
- i2c_ack_config(I2C1,I2C_ACK_ENABLE); //启用确认
- }
复制代码
|
|