void I2Cx_Init(I2C_Type* I2Cx)
{
GPIO_InitType GPIO_InitStructure;
NVIC_InitType NVIC_InitStructure;
I2C_InitType I2C_InitStructure;
/* I2C_SCL_GPIO_CLK and I2C_SDA_GPIO_CLK Periph clock enable */
RCC_APB2PeriphClockCmd(I2C_SCL_GPIO_CLK | I2C_SDA_GPIO_CLK, ENABLE);
/* I2C Periph clock enable */
RCC_APB1PeriphClockCmd(I2C_CLK, ENABLE);
/* Enable the DMA clock */
RCC_AHBPeriphClockCmd(I2Cx_DMA_CLK, ENABLE);
/* GPIO configuration */
/* Configure I2C pins: SCL */
GPIO_InitStructure.GPIO_Pins = I2C_SCL_PIN;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(I2C_SCL_GPIO_PORT, &GPIO_InitStructure);
/* Configure I2C pins: SDA */
GPIO_InitStructure.GPIO_Pins = I2C_SDA_PIN;
GPIO_Init(I2C_SDA_GPIO_PORT, &GPIO_InitStructure);
/* Configure and enable I2C DMA TX Channel interrupt */
NVIC_InitStructure.NVIC_IRQChannel = I2Cx_DMA_TX_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = I2Cx_DMA_TX_PREPRIO;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = I2Cx_DMA_TX_SUBPRIO;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Configure and enable I2C DMA RX Channel interrupt */
NVIC_InitStructure.NVIC_IRQChannel = I2Cx_DMA_RX_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = I2Cx_DMA_RX_PREPRIO;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = I2Cx_DMA_RX_SUBPRIO;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = I2Cx_EV_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = I2Cx_EV_PREPRIO;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = I2Cx_EV_SUBPRIO;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = I2Cx_ER_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = I2Cx_ER_PREPRIO;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = I2Cx_ER_SUBPRIO;
NVIC_Init(&NVIC_InitStructure);
/* I2C configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2CDevice;
I2C_InitStructure.I2C_FmDutyCycle = I2C_FmDutyCycle_2_1;
I2C_InitStructure.I2C_OwnAddr1 = I2C_OWN_ADDRESS7;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AddrMode = I2C_AddrMode_7bit;
I2C_InitStructure.I2C_BitRate = I2C_SPEED;
/* Apply I2C configuration after enabling it */
I2C_Init(I2Cx, &I2C_InitStructure);
/* I2C Peripheral Enable */
I2C_Cmd(I2Cx, ENABLE);
I2C_INTConfig(I2Cx,I2C_INT_EVT | I2C_INT_BUF | I2C_INT_ERR,ENABLE);
}
想学习下I2C部分,一直有问题,请各位有时间,帮忙看看初始化部分是否有问题。
应为是临时搭的电路,i2c飞线了,不知道有没有影响。
F403a
|