完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
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 /**
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]); } |
|
|
|
|
只有小组成员才能发言,加入小组>>
1986 浏览 0 评论
imx6ull 和 lan8742 工作起来不正常, ping 老是丢包
4787 浏览 0 评论
4266 浏览 9 评论
3855 浏览 16 评论
4429 浏览 1 评论
4255浏览 3评论
2413浏览 0评论
3431浏览 0评论
1194浏览 0评论
2883浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-12 05:42 , Processed in 0.472664 second(s), Total 44, Slave 34 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
1095