使用stm32f103c8t6标准库写一个多个INA3221模块使用IIC
时间: 2024-01-30 13:03:19 浏览: 158
好的,下面是使用STM32F103C8T6标准库控制多个INA3221模块的IIC通信的示例代码:
首先,需要定义IIC通信所需的引脚和地址:
```c
#define INA3221_I2C I2C1
#define INA3221_I2C_RCC RCC_APB1Periph_I2C1
#define INA3221_I2C_GPIO GPIOB
#define INA3221_I2C_SCL_PIN GPIO_Pin_6
#define INA3221_I2C_SDA_PIN GPIO_Pin_7
#define INA3221_I2C_SPEED 100000
#define INA3221_ADDRESS 0x40
```
然后,需要定义用于INA3221通信的函数:
```c
void INA3221_I2C_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
/* Enable I2C and GPIO clocks */
RCC_APB1PeriphClockCmd(INA3221_I2C_RCC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure I2C pins: SCL and SDA */
GPIO_InitStructure.GPIO_Pin = INA3221_I2C_SCL_PIN | INA3221_I2C_SDA_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(INA3221_I2C_GPIO, &GPIO_InitStructure);
/* Configure I2C */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = INA3221_I2C_SPEED;
I2C_Init(INA3221_I2C, &I2C_InitStructure);
/* Enable I2C */
I2C_Cmd(INA3221_I2C, ENABLE);
}
uint8_t INA3221_I2C_ReadReg(uint8_t reg)
{
uint8_t data = 0;
/* Send start condition */
I2C_GenerateSTART(INA3221_I2C, ENABLE);
/* Wait until start condition is sent */
while (!I2C_CheckEvent(INA3221_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send INA3221 address for writing */
I2C_Send7bitAddress(INA3221_I2C, INA3221_ADDRESS, I2C_Direction_Transmitter);
/* Wait until address is sent */
while (!I2C_CheckEvent(INA3221_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Send register address */
I2C_SendData(INA3221_I2C, reg);
/* Wait until data is sent */
while (!I2C_CheckEvent(INA3221_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send repeated start condition */
I2C_GenerateSTART(INA3221_I2C, ENABLE);
/* Wait until start condition is sent */
while (!I2C_CheckEvent(INA3221_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send INA3221 address for reading */
I2C_Send7bitAddress(INA3221_I2C, INA3221_ADDRESS, I2C_Direction_Receiver);
/* Wait until address is sent */
while (!I2C_CheckEvent(INA3221_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* Wait until data is received */
while (!I2C_CheckEvent(INA3221_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED));
/* Read data */
data = I2C_ReceiveData(INA3221_I2C);
/* Send stop condition */
I2C_GenerateSTOP(INA3221_I2C, ENABLE);
return data;
}
void INA3221_I2C_WriteReg(uint8_t reg, uint8_t data)
{
/* Send start condition */
I2C_GenerateSTART(INA3221_I2C, ENABLE);
/* Wait until start condition is sent */
while (!I2C_CheckEvent(INA3221_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send INA3221 address for writing */
I2C_Send7bitAddress(INA3221_I2C, INA3221_ADDRESS, I2C_Direction_Transmitter);
/* Wait until address is sent */
while (!I2C_CheckEvent(INA3221_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Send register address */
I2C_SendData(INA3221_I2C, reg);
/* Wait until data is sent */
while (!I2C_CheckEvent(INA3221_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send data */
I2C_SendData(INA3221_I2C, data);
/* Wait until data is sent */
while (!I2C_CheckEvent(INA3221_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send stop condition */
I2C_GenerateSTOP(INA3221_I2C, ENABLE);
}
```
最后,可以使用上述函数来读取或写入多个INA3221模块的寄存器。
例如,如果有两个INA3221模块,地址分别为0x40和0x41,则可以使用以下代码来读取第一个模块的电流寄存器:
```c
uint16_t current = (INA3221_I2C_ReadReg(0x40) << 8) | INA3221_I2C_ReadReg(0x41);
```
或者使用以下代码来写入第二个模块的电压警报上限寄存器:
```c
INA3221_I2C_WriteReg(0x45, 0x12);
```
希望这可以帮助你控制多个INA3221模块的IIC通信。
阅读全文