STM32HAL库硬件I2C驱动INA226保姆级教程

STM32HAL库硬件I2C驱动INA226保姆级教程

废话不多说直接上代码

ina226.h

/**


  • @brief STM32 HAL Library for INA226 Current/Power Monitor
  • @date Feb 2016
  • @version 1.0
  • @author George Christidis

  • @details
    This library contains the necessary functions to initialize, read and
    write data to the TI INA226 Current/Power Monitor using the I2C
    protocol.
    */

#ifndef F7_INA226_H
#define F7_INA226_H

#include “stm32l4xx_hal.h”
#include “stm32l4xx_hal_i2c.h”//根据芯片型号,自主更改

#ifndef INA226_ADDRESS
#define INA226_ADDRESS 0x80
#endif

#define INA226_CALIB_VAL 1024
#define INA226_CURRENTLSB 0.5F // mA/bit
#define INA226_CURRENTLSB_INV 1/INA226_CURRENTLSB // bit/mA
#define INA226_POWERLSB_INV 1/(INA226_CURRENTLSB*25) // bit/mW
#define INA226_I2CTIMEOUT 10

#define INA226_CONFIG 0x00 // Configuration Register (R/W)
#define INA226_SHUNTV 0x01 // Shunt Voltage ®
#define INA226_BUSV 0x02 // Bus Voltage ®
#define INA226_POWER 0x03 // Power ®
#define INA226_CURRENT 0x04 // Current ®
#define INA226_CALIB 0x05 // Calibration (R/W)
#define INA226_MASK 0x06 // Mask/Enable (R/W)
#define INA226_ALERTL 0x07 // Alert Limit (R/W)
#define INA226_MANUF_ID 0xFE // Manufacturer ID ®
#define INA226_DIE_ID 0xFF // Die ID ®

#define INA226_MODE_POWER_DOWN (0<<0) // Power-Down
#define INA226_MODE_TRIG_SHUNT_VOLTAGE (1<<0) // Shunt Voltage, Triggered
#define INA226_MODE_TRIG_BUS_VOLTAGE (2<<0) // Bus Voltage, Triggered
#define INA226_MODE_TRIG_SHUNT_AND_BUS (3<<0) // Shunt and Bus, Triggered
#define INA226_MODE_POWER_DOWN2 (4<<0) // Power-Down
#define INA226_MODE_CONT_SHUNT_VOLTAGE (5<<0) // Shunt Voltage, Continuous
#define INA226_MODE_CONT_BUS_VOLTAGE (6<<0) // Bus Voltage, Continuous
#define INA226_MODE_CONT_SHUNT_AND_BUS (7<<0) // Shunt and Bus, Continuous

// Shunt Voltage Conversion Time
#define INA226_VSH_140uS (0<<3)
#define INA226_VSH_204uS (1<<3)
#define INA226_VSH_332uS (2<<3)
#define INA226_VSH_588uS (3<<3)
#define INA226_VSH_1100uS (4<<3)
#define INA226_VSH_2116uS (5<<3)
#define INA226_VSH_4156uS (6<<3)
#define INA226_VSH_8244uS (7<<3)

// Bus Voltage Conversion Time (VBUS CT Bit Settings[6-8])
#define INA226_VBUS_140uS (0<<6)
#define INA226_VBUS_204uS (1<<6)
#define INA226_VBUS_332uS (2<<6)
#define INA226_VBUS_588uS (3<<6)
#define INA226_VBUS_1100uS (4<<6)
#define INA226_VBUS_2116uS (5<<6)
#define INA226_VBUS_4156uS (6<<6)
#define INA226_VBUS_8244uS (7<<6)

// Averaging Mode (AVG Bit Settings[9-11])
#define INA226_AVG_1 (0<<9)
#define INA226_AVG_4 (1<<9)
#define INA226_AVG_16 (2<<9)
#define INA226_AVG_64 (3<<9)
#define INA226_AVG_128 (4<<9)
#define INA226_AVG_256 (5<<9)
#define INA226_AVG_512 (6<<9)
#define INA226_AVG_1024 (7<<9)

// Reset Bit (RST bit [15])
#define INA226_RESET_ACTIVE (1<<15)
#define INA226_RESET_INACTIVE (0<<15)

// Mask/Enable Register
#define INA226_MER_SOL (1<<15) // Shunt Voltage Over-Voltage
#define INA226_MER_SUL (1<<14) // Shunt Voltage Under-Voltage
#define INA226_MER_BOL (1<<13) // Bus Voltagee Over-Voltage
#define INA226_MER_BUL (1<<12) // Bus Voltage Under-Voltage
#define INA226_MER_POL (1<<11) // Power Over-Limit
#define INA226_MER_CNVR (1<<10) // Conversion Ready
#define INA226_MER_AFF (1<<4) // Alert Function Flag
#define INA226_MER_CVRF (1<<3) // Conversion Ready Flag
#define INA226_MER_OVF (1<<2) // Math Overflow Flag
#define INA226_MER_APOL (1<<1) // Alert Polarity Bit
#define INA226_MER_LEN (1<<0) // Alert Latch Enable

#define INA226_MANUF_ID_DEFAULT 0x5449
#define INA226_DIE_ID_DEFAULT 0x2260

float INA226_getBusV(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);
float INA226_getCurrent(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);
float INA226_getPower(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);

uint8_t INA226_setConfig(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress, uint16_t ConfigWord);
uint16_t INA226_getConfig(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);
uint16_t INA226_getShuntV(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);
uint16_t INA226_getBusVReg(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);
uint16_t INA226_getPowerReg(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);
uint8_t INA226_setCalibrationReg(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress, uint16_t ConfigWord);
uint16_t INA226_getCalibrationReg(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);
uint16_t INA226_getCurrentReg(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);
uint16_t INA226_getManufID(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);
uint16_t INA226_getDieID(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);
uint8_t INA226_setMaskEnable(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress, uint16_t ConfigWord);
uint16_t INA226_getMaskEnable(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);
uint8_t INA226_setAlertLimit(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress, uint16_t ConfigWord);
uint16_t INA226_getAlertLimit(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress);

#endif

ina226.c

/**


  • @brief STM32 HAL Library for INA226 Current/Power Monitor
  • @date Feb 2016
  • @version 1.0
  • @author George Christidis

  • @details
    This library contains the necessary functions to initialize, read and
    write data to the TI INA226 Current/Power Monitor using the I2C
    protocol.
    */

#include “ina226.h”

float INA226_getBusV(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
return (INA226_getBusVReg(I2CHandler, DevAddress));
}

float INA226_getCurrent(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
return (INA226_getCurrentReg(I2CHandler, DevAddress)*INA226_CURRENTLSB_INV);
}

float INA226_getPower(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
return (INA226_getPowerReg(I2CHandler, DevAddress)*INA226_POWERLSB_INV);
}

uint8_t INA226_setConfig(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress, uint16_t ConfigWord) {
uint8_t SentTable[3];
SentTable[0] = INA226_CONFIG;
SentTable[1] = (ConfigWord & 0xFF00) >> 8;
SentTable[2] = (ConfigWord & 0x00FF);
return HAL_I2C_Master_Transmit(I2CHandler, DevAddress, SentTable, 3, INA226_I2CTIMEOUT);
}

uint16_t INA226_getConfig(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
uint8_t SentTable[1] = {INA226_CONFIG};
uint8_t ReceivedTable[2];
HAL_I2C_Master_Transmit(I2CHandler,DevAddress, SentTable, 1, INA226_I2CTIMEOUT);
if (HAL_I2C_Master_Receive(I2CHandler,DevAddress, ReceivedTable, 2, INA226_I2CTIMEOUT) != HAL_OK) return 0xFF;
else return ((uint16_t)ReceivedTable[0]<<8 | ReceivedTable[1]);
}

uint16_t INA226_getShuntV(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
uint8_t SentTable[1] = {INA226_SHUNTV};
uint8_t ReceivedTable[2];
HAL_I2C_Master_Transmit(I2CHandler,DevAddress, SentTable, 1, INA226_I2CTIMEOUT);
if (HAL_I2C_Master_Receive(I2CHandler,DevAddress, ReceivedTable, 2, INA226_I2CTIMEOUT) != HAL_OK) return 0xFF;
else return ((uint16_t)ReceivedTable[0]<<8 | ReceivedTable[1]);
}

uint16_t INA226_getBusVReg(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
uint8_t SentTable[1] = {INA226_BUSV};
uint8_t ReceivedTable[2];
HAL_I2C_Master_Transmit(I2CHandler,DevAddress, SentTable, 1, INA226_I2CTIMEOUT);
if (HAL_I2C_Master_Receive(I2CHandler,DevAddress, ReceivedTable, 2, INA226_I2CTIMEOUT) != HAL_OK) return 0xFF;
else return ((uint16_t)ReceivedTable[0]<<8 | ReceivedTable[1]);
}

uint8_t INA226_setCalibrationReg(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress, uint16_t ConfigWord) {
uint8_t SentTable[3];
SentTable[0] = INA226_CALIB;
SentTable[1] = (ConfigWord & 0xFF00) >> 8;
SentTable[2] = (ConfigWord & 0x00FF);
return HAL_I2C_Master_Transmit(I2CHandler, DevAddress, SentTable, 3, INA226_I2CTIMEOUT);
}

uint16_t INA226_getCalibrationReg(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
uint8_t SentTable[1] = {INA226_CALIB};
uint8_t ReceivedTable[2];
HAL_I2C_Master_Transmit(I2CHandler,DevAddress, SentTable, 1, INA226_I2CTIMEOUT);
if (HAL_I2C_Master_Receive(I2CHandler,DevAddress, ReceivedTable, 2, INA226_I2CTIMEOUT) != HAL_OK) return 0xFF;
else return ((uint16_t)ReceivedTable[0]<<8 | ReceivedTable[1]);
}

uint16_t INA226_getPowerReg(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
uint8_t SentTable[1] = {INA226_POWER};
uint8_t ReceivedTable[2];
HAL_I2C_Master_Transmit(I2CHandler,DevAddress, SentTable, 1, INA226_I2CTIMEOUT);
if (HAL_I2C_Master_Receive(I2CHandler,DevAddress, ReceivedTable, 2, INA226_I2CTIMEOUT) != HAL_OK) return 0xFF;
else return ((uint16_t)ReceivedTable[0]<<8 | ReceivedTable[1]);
}

uint16_t INA226_getCurrentReg(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
uint8_t SentTable[1] = {INA226_CURRENT};
uint8_t ReceivedTable[2];
HAL_I2C_Master_Transmit(I2CHandler,DevAddress, SentTable, 1, INA226_I2CTIMEOUT);
if (HAL_I2C_Master_Receive(I2CHandler,DevAddress, ReceivedTable, 2, INA226_I2CTIMEOUT) != HAL_OK) return 0xFF;
else return ((uint16_t)ReceivedTable[0]<<8 | ReceivedTable[1]);
}

uint16_t INA226_getManufID(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
uint8_t SentTable[1] = {INA226_MANUF_ID};
uint8_t ReceivedTable[2];
HAL_I2C_Master_Transmit(I2CHandler,DevAddress, SentTable, 1, INA226_I2CTIMEOUT);
if (HAL_I2C_Master_Receive(I2CHandler,DevAddress, ReceivedTable, 2, INA226_I2CTIMEOUT) != HAL_OK) return 0xFF;
else return ((uint16_t)ReceivedTable[0]<<8 | ReceivedTable[1]);
}

uint16_t INA226_getDieID(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
uint8_t SentTable[1] = {INA226_DIE_ID};
uint8_t ReceivedTable[2];
HAL_I2C_Master_Transmit(I2CHandler,DevAddress, SentTable, 1, INA226_I2CTIMEOUT);
if (HAL_I2C_Master_Receive(I2CHandler,DevAddress, ReceivedTable, 2, INA226_I2CTIMEOUT) != HAL_OK) return 0xFF;
else return ((uint16_t)ReceivedTable[0]<<8 | ReceivedTable[1]);
}

uint8_t INA226_setMaskEnable(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress, uint16_t ConfigWord) {
uint8_t SentTable[3];
SentTable[0] = INA226_MASK;
SentTable[1] = (ConfigWord & 0xFF00) >> 8;
SentTable[2] = (ConfigWord & 0x00FF);
return HAL_I2C_Master_Transmit(I2CHandler, DevAddress, SentTable, 3, INA226_I2CTIMEOUT);
}

uint16_t INA226_getMaskEnable(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
uint8_t SentTable[1] = {INA226_MASK};
uint8_t ReceivedTable[2];
HAL_I2C_Master_Transmit(I2CHandler,DevAddress, SentTable, 1, INA226_I2CTIMEOUT);
if (HAL_I2C_Master_Receive(I2CHandler,DevAddress, ReceivedTable, 2, INA226_I2CTIMEOUT) != HAL_OK) return 0xFF;
else return ((uint16_t)ReceivedTable[0]<<8 | ReceivedTable[1]);
}

uint8_t INA226_setAlertLimit(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress, uint16_t ConfigWord) {
uint8_t SentTable[3];
SentTable[0] = INA226_ALERTL;
SentTable[1] = (ConfigWord & 0xFF00) >> 8;
SentTable[2] = (ConfigWord & 0x00FF);
return HAL_I2C_Master_Transmit(I2CHandler, DevAddress, SentTable, 3, INA226_I2CTIMEOUT);
}

uint16_t INA226_getAlertLimit(I2C_HandleTypeDef *I2CHandler, uint16_t DevAddress) {
uint8_t SentTable[1] = {INA226_ALERTL};
uint8_t ReceivedTable[2];
HAL_I2C_Master_Transmit(I2CHandler,DevAddress, SentTable, 1, INA226_I2CTIMEOUT);
if (HAL_I2C_Master_Receive(I2CHandler,DevAddress, ReceivedTable, 2, INA226_I2CTIMEOUT) != HAL_OK) return 0xFF;
else return ((uint16_t)ReceivedTable[0]<<8 | ReceivedTable[1]);
}

感谢阅读,希望对你有帮助

  • 8
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
好的,关于STM32 HAL库硬件I2C通信,我可以给你一些基本的信息和代码示例。 首先需要在CubeMX中配置I2C外设,并且生成HAL库初始化代码。以下是一个示例代码,可以用于初始化I2C1外设: ``` /* I2C1 init function */ static void MX_I2C1_Init(void) { hi2c1.Instance = I2C1; hi2c1.Init.Timing = 0x00707CBB; hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.OwnAddress2 = 0; hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if (HAL_I2C_Init(&hi2c1) != HAL_OK) { Error_Handler(); } } ``` 然后,就可以使用HAL库提供的函数进行I2C通信了。以下是一个示例代码,可以用于向I2C设备发送数据: ``` uint8_t i2c_tx_buf[2]; i2c_tx_buf[0] = 0x00; // 选择寄存器地址 i2c_tx_buf[1] = 0x12; // 写入的数据 HAL_I2C_Master_Transmit(&hi2c1, I2C_DEVICE_ADDRESS, i2c_tx_buf, 2, HAL_MAX_DELAY); ``` 其中,`I2C_DEVICE_ADDRESS`是I2C设备的地址,可以在设备的数据手册中查找。 接下来是一个示例代码,可以用于从I2C设备读取数据: ``` uint8_t i2c_rx_buf[2]; i2c_rx_buf[0] = 0x00; // 选择寄存器地址 HAL_I2C_Master_Transmit(&hi2c1, I2C_DEVICE_ADDRESS, i2c_rx_buf, 1, HAL_MAX_DELAY); HAL_I2C_Master_Receive(&hi2c1, I2C_DEVICE_ADDRESS, i2c_rx_buf, 2, HAL_MAX_DELAY); ``` 其中,先使用`HAL_I2C_Master_Transmit`函数向I2C设备发送寄存器地址,然后使用`HAL_I2C_Master_Receive`函数从I2C设备接收数据。 以上是一个简单的硬件I2C通信的示例,希望可以帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值