在线时间1079 小时
UID299704
注册时间2011-6-18
NXP金币2006
TA的每日心情 | 开心 7 天前 |
---|
签到天数: 1503 天 连续签到: 1 天 [LV.Master]伴坛终老
版主
  
- 积分
- 10088
- 最后登录
- 2025-7-29
|
INA219 是一款具备I2C 或SMBUS 兼容接口的分流器和功率监测计。该器件监测分流器电压降和总线电源电压,转换次数和滤波选项可通过编程设定。可编程校准值与内部乘法器相结合,支持直接读取电流值(单位:安培)。通过附加乘法寄存器可计算功率(单位:瓦)。I2C 或SMBUS 兼容接口具有16 个可编程地址。
INA219 可在0V 至26V 范围内感测总线中的分压。该器件由3V 至5.5V 单电源供电,电源的最大流耗为1mA。
芯片同是支持差分分压电流检测跟总线电压检测,芯片外部引脚A0、A1引脚,支持支持16路地址设置,同时可挂载16路设备。
INA219内部只有6个寄存器,需要配置的寄存器仅有两个。
configuration寄存器,其配置芯片的工作状态与工作参数。
calibration寄存器,其校正芯片的采样数据。
其余的寄存器为只读寄存器,shunt电压,bus电压,功率,电流。
对其编程就显得非常简单了,仅需要通过I2C总线,首先配置芯片,然后再读取对应的寄存器值即可。
该芯片电路简洁,工作稳定,价格实惠。
源代码如下:
- #include <stdint.h>
- #include "bspI2C.h"
- #include "devINA219.h"
- struct tINA219Class charli219 = {
- .bus = 0,
- .address = INA219_ADDRESS,
- };
- int8 ina219_init(tINA219Class *dev)
- {
- int8_t ret = 0;
- tI2CMasterClass i2c_dev;
- i2c_dev.busNum = dev->bus;
- i2c_dev.salveAddr = dev->address;
- ret = openI2C(&i2c_dev, busWrite);
- if(ret != 0)
- {
- // printf("i2c bus busy...\n");
- return (1);
- }
- writeI2c(&i2c_dev, INA219_REG_CONFIG, INA219_CONFIG_value);
- }
- int8_t ina219_read_voltage(tINA219Class *dev, int16_t *value_ptr)
- {
- int8_t ret = 0;
- uint8_t mbuf[2];
- tI2CMasterClass i2c_dev;
- i2c_dev.busNum = dev->bus;
- i2c_dev.salveAddr = dev->address;
- ret = wrReadI2c(&i2c_dev, INA219_REG_BUSVOLTAGE, mbuf, 2);
- *value_ptr = (((mbuf[0] << 5) + mbuf[1]) >> 3) * 4;
- return (ret);
- }
复制代码 头文件的源代码:
- #ifndef MIDWARE_DEVINA219_H_
- #define MIDWARE_DEVINA219_H_
- #include <stdint.h>
- #define INA219_ADDRESS (0x40 << 1) // A0 = GND, A1 = GND
- #define INA219_REG_CONFIG (0x00)
- #define INA219_REG_SHUNTVOLTAGE (0x01)
- #define INA219_REG_BUSVOLTAGE (0x02)
- #define INA219_REG_POWER (0x03)
- #define INA219_REG_CURRENT (0x04)
- #define INA219_REG_CALIBRATION (0x05)
- #define INA219_CONFIG_RESET (0x8000) // Reset Bit
- #define INA219_CONFIG_BVOLTAGERANGE_MASK (0x2000) // Bus Voltage Range Mask
- #define INA219_CONFIG_BVOLTAGERANGE_16V (0x0000) // 0-16V Range
- #define INA219_CONFIG_BVOLTAGERANGE_32V (0x2000) // 0-32V Range
- #define INA219_CONFIG_GAIN_MASK (0x1800) // Gain Mask
- #define INA219_CONFIG_GAIN_1_40MV (0x0000) // Gain 1, 40mV Range
- #define INA219_CONFIG_GAIN_2_80MV (0x0800) // Gain 2, 80mV Range
- #define INA219_CONFIG_GAIN_4_160MV (0x1000) // Gain 4, 160mV Range
- #define INA219_CONFIG_GAIN_8_320MV (0x1800) // Gain 8, 320mV Range
- #define INA219_CONFIG_BADCRES_MASK (0x0780) // Bus ADC Resolution Mask
- #define INA219_CONFIG_BADCRES_9BIT (0x0080) // 9-bit bus res = 0..511
- #define INA219_CONFIG_BADCRES_10BIT (0x0100) // 10-bit bus res = 0..1023
- #define INA219_CONFIG_BADCRES_11BIT (0x0200) // 11-bit bus res = 0..2047
- #define INA219_CONFIG_BADCRES_12BIT (0x0400) // 12-bit bus res = 0..4097
- #define INA219_CONFIG_SADCRES_MASK (0x0078) // Shunt ADC Resolution and Averaging Mask
- #define INA219_CONFIG_SADCRES_9BIT_1S_84US (0x0000) // 1 x 9-bit shunt sample
- #define INA219_CONFIG_SADCRES_10BIT_1S_148US (0x0008) // 1 x 10-bit shunt sample
- #define INA219_CONFIG_SADCRES_11BIT_1S_276US (0x0010) // 1 x 11-bit shunt sample
- #define INA219_CONFIG_SADCRES_12BIT_1S_532US (0x0018) // 1 x 12-bit shunt sample
- #define INA219_CONFIG_SADCRES_12BIT_2S_1060US (0x0048) // 2 x 12-bit shunt samples averaged together
- #define INA219_CONFIG_SADCRES_12BIT_4S_2130US (0x0050) // 4 x 12-bit shunt samples averaged together
- #define INA219_CONFIG_SADCRES_12BIT_8S_4260US (0x0058) // 8 x 12-bit shunt samples averaged together
- #define INA219_CONFIG_SADCRES_12BIT_16S_8510US (0x0060) // 16 x 12-bit shunt samples averaged together
- #define INA219_CONFIG_SADCRES_12BIT_32S_17MS (0x0068) // 32 x 12-bit shunt samples averaged together
- #define INA219_CONFIG_SADCRES_12BIT_64S_34MS (0x0070) // 64 x 12-bit shunt samples averaged together
- #define INA219_CONFIG_SADCRES_12BIT_128S_69MS (0x0078) // 128 x 12-bit shunt samples averaged together
- #define INA219_CONFIG_MODE_MASK (0x0007) // Operating Mode Mask
- #define INA219_CONFIG_MODE_POWERDOWN (0x0000)
- #define INA219_CONFIG_MODE_SVOLT_TRIGGERED (0x0001)
- #define INA219_CONFIG_MODE_BVOLT_TRIGGERED (0x0002)
- #define INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED (0x0003)
- #define INA219_CONFIG_MODE_ADCOFF (0x0004)
- #define INA219_CONFIG_MODE_SVOLT_CONTINUOUS (0x0005)
- #define INA219_CONFIG_MODE_BVOLT_CONTINUOUS (0x0006)
- #define INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS (0x0007)
- #define INA_R (0.1)
- #define INA_I_MAX (3)
- #define IAN_I_LSB (0.1)
- #define INA_Power_LSB (2)
- #define INA_CAL (4096)
- #define INA219_CONFIG_value INA219_CONFIG_BVOLTAGERANGE_32V|INA219_CONFIG_GAIN_8_320MV|INA219_CONFIG_BADCRES_12BIT|INA219_CONFIG_SADCRES_12BIT_1S_532US|INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS
- struct tINA219Class
- {
- uint8_t bus;
- uint8_t address;
- };
- #endif /* MIDWARE_DEVINA219_H_ */
复制代码
其余采样量只是替换寄存器而已,在这里不再赘述。
提醒一下,对于其余值的数据,符号位的整理也是非常重要,尤其要测试,
|
|