INA219电流、电压、功率测量芯片应用

INA219电流、电压、功率测量芯片应用

简述

‌INA219是一款由德州仪器(Texas Instruments)生产的高精度电流/功率监测芯片,广泛应用于电池监控、电源管理等需要精确电流和功率测量的应用中‌‌。该芯片通过I2C总线接口与微控制器进行通信,能够实时监测负载电流、电压和功率等参数。

技术参数:

  • 电压测量范围‌:0V到26V,适用于广泛的应用场景‌。
  • 电流测量范围‌:通过外部电流检测电阻,可测量的电流范围一般从0A到3.2A或更高‌。
  • 功率计算范围‌:根据测量的电压和电流决定,单位为瓦特‌。
  • 分辨率‌:电流分辨率最高可达0.1mA,电压分辨率最高可达0.1mV‌。

特点和应用场景
INA219具有以下特点:

  • 高精度‌:能够提供高精度的电流和电压测量。
  • 低功耗‌:适用于需要低功耗的应用场景。
  • 多种型号‌:包括INA219A、INA219B和INA219C,每个型号有不同的特点和适用场景‌ 。

常见型号及其特点:

  • INA219A‌:适用于一般低功耗应用,具有较高的电流测量精度和较低的偏置电流‌。
  • INA219B‌:具有更低的功耗,并且在电流测量方面提供更高的精度,适用于需要高精度电流监控的系统‌。
  • INA219C‌:提供较宽的工作电压范围和更高的电流量程,适用于更高电流范围的监测应用‌。
    在这里插入图片描述
    在这里插入图片描述

芯片引脚

在这里插入图片描述

  • IN+和IN_:分别是接检测分流电阻的两端。
  • GND:接电源负极
  • Vs:电源正极(电压范围:3-5.5V)
  • SCL:通讯时钟线
  • SDA:通讯数据线
  • A0和A1:地址选择引脚(地址对应表如下图)
    在这里插入图片描述

应用电路

配置用于检测分流和总线电压:

  • 分流电压:指的是分流电阻 R S H U N T R_{SHUNT} RSHUNT两端的电压。
  • 总线电压:指的是 V I N − V_{IN-} VIN和GND之间的电压。

在这里插入图片描述

在这里插入图片描述

  • 带有输入滤波的应用电路:
    在这里插入图片描述

寄存器

INA219通信协议使用的是I2C总线协议,16bit寄存器即2个字节。一共有6个寄存器,其中只需要设置00h和05h两个寄存器,其他为只读寄存器:

  • 00:配置寄存器,主要用来配置INA219的工作方式和配置参数(读/写)

  • 01:分流电阻电压寄存器(只读)

  • 02:总线电压寄存器 V I N − V_{IN-} VIN和GND之间的电压(只读)

  • 03:功率寄存器(只读)

  • 04:电流寄存器(只读)

  • 05:校准寄存器(读/写)
    在这里插入图片描述
    在这里插入图片描述

  • RST:Bit 15复位位,设置为1产生系统复位,就像上电复位一样,所有寄存器复位成默认值。该位自清零。

  • BRNG:Bit 13总线电压量程范围,0=16V,1=32V(默认值)

  • PG:Bits[11, 12] PGA(设置分流电阻最大的电压,用于计算最大检测电流):
    在这里插入图片描述
    BADC: Bits[7-10]总线电压ADC分辨率/平均值设置(总线电压寄存器02h)
    SADC: Bits[3-6]分压电阻电压ADC分辨率/平均值设置(分流电压寄存器01h)
    在这里插入图片描述
    MODE: Bit[0-2]运行模式
    在这里插入图片描述
    数据输出寄存器:

  • 分流电压寄存器(01h):
    在这里插入图片描述

  • 总线电压寄存器(02h):
    CNVR:Bit 1 转换完成 为1表示转换完成。
    OVF: Bit 0 数学溢出标志位,为1表示功率或电流计算结果溢出,指示电流或功率寄存器数据可能无意义。
    在这里插入图片描述

  • 功率寄存器(03h)
    在这里插入图片描述

  • 电流寄存器(04h)
    在这里插入图片描述

  • 校准寄存器(05h)
    在这里插入图片描述
    校准值计算:
    校准值可通过如下公式计算:
    在这里插入图片描述

首先确定分流电阻 R S H U N T R_{SHUNT} RSHUNT的阻值=0.1R,设置最大检测电压为:32V,设置分流电阻电压范围:320mV
所以最大检测电流 I M A X I_{MAX} IMAX=320mV/0.1R=0.32V/0.1R=3.2A
Current_LSB_MIN= I M A X / 2 15 I_{MAX}/2^{15} IMAX/215=3.2/23767=0.00009766 ADC@15bit
Current_LSB_MAX= I M A X / 2 12 I_{MAX}/2^{12} IMAX/212=3.2/4096=0.00078125 ADC@12bit
Current_LSB的选值范围为Current_LSB_MIN和Current_LSB_MAX之间,选择靠近Current_LSB_MIN
因此这里选择Current_LSB=10010^-6=100uA=0.0001A(每bit位对应的电流大小)
计算基准值:Cal=0.04096/(Current_LSB/R)=0.04096/(0.0001A
0.1R)=4096=0x1000
当配置05寄存器的值为:0x1000

但是这样配置完后发现测量到的电流值不对,官方手册还给出了Cal的校准公式:
在这里插入图片描述
用电流表测到的实际值为0.290A,INA219测量结果为0.342A,
校准后的cal=4096*0.290/0.3421 = 3472 = 0x0D90
设置后的测量值正确

计算功率LSB
P_LSB=20I_LSB=200.0001A=0.002W
在这里插入图片描述
在这里插入图片描述

驱动代码

Type_def.h

typedef signed char   s8;     //  8 bits 
typedef signed int    s16;    // 16 bits 
typedef signed long   s32;    // 32 bits 

typedef unsigned char   u8;     //  8 bits 
typedef unsigned int    u16;    // 16 bits 
typedef unsigned long   u32;    // 32 bits 

typedef signed char     int8;   //  8 bits 
typedef signed int      int16;  // 16 bits 
typedef signed long     int32;  // 32 bits 

typedef unsigned char   uint8;  //  8 bits 
typedef unsigned int    uint16; // 16 bits 
typedef unsigned long   uint32; // 32 bits 

typedef signed char     int8_t;   //  8 bits 
typedef signed int      int16_t;  // 16 bits 
typedef signed long     int32_t;  // 32 bits 

typedef unsigned char   uint8_t;  //  8 bits 
typedef unsigned int    uint16_t; // 16 bits 
typedef unsigned long   uint32_t; // 32 bits 

drv_ina219.h

#ifndef __DRV_INA219_H__
#define __DRV_INA219_H__

// I2C Address Options
#define INA219_I2C_ADDRESS_CONF_0               (u8)(0x40 << 1)     // A0 = GND, A1 = GND
#define INA219_I2C_ADDRESS_CONF_1               (u8)(0x41 << 1)     // A0 = VS+, A1 = GND
#define INA219_I2C_ADDRESS_CONF_2               (u8)(0x42 << 1)     // A0 = SDA, A1 = GND
#define INA219_I2C_ADDRESS_CONF_3               (u8)(0x43 << 1)     // A0 = SCL, A1 = GND
#define INA219_I2C_ADDRESS_CONF_4               (u8)(0x44 << 1)     // A0 = GND, A1 = VS+
#define INA219_I2C_ADDRESS_CONF_5               (u8)(0x45 << 1)     // A0 = VS+, A1 = VS+
#define INA219_I2C_ADDRESS_CONF_6               (u8)(0x46 << 1)     // A0 = SDA, A1 = VS+
#define INA219_I2C_ADDRESS_CONF_7               (u8)(0x47 << 1)     // A0 = SCL, A1 = VS+
#define INA219_I2C_ADDRESS_CONF_8               (u8)(0x48 << 1)     // A0 = GND, A1 = SDA
#define INA219_I2C_ADDRESS_CONF_9               (u8)(0x49 << 1)     // A0 = VS+, A1 = SDA
#define INA219_I2C_ADDRESS_CONF_A               (u8)(0x4A << 1)     // A0 = SDA, A1 = SDA
#define INA219_I2C_ADDRESS_CONF_B               (u8)(0x4B << 1)     // A0 = SCL, A1 = SDA
#define INA219_I2C_ADDRESS_CONF_C               (u8)(0x4C << 1)     // A0 = GND, A1 = SCL
#define INA219_I2C_ADDRESS_CONF_D               (u8)(0x4D << 1)     // A0 = VS+, A1 = SCL
#define INA219_I2C_ADDRESS_CONF_E               (u8)(0x4E << 1)     // A0 = SDA, A1 = SCL
#define INA219_I2C_ADDRESS_CONF_F               (u8)(0x4F << 1)     // A0 = SCL, A1 = SCL
#define INA219_I2C_ADDRESS                      INA219_I2C_ADDRESS_CONF_0
 
 
/*----------------------------------------------------------------------------*/
// Register Addresses
#define INA219_REG_CONFIG                       (u8)(0x00)      // CONFIG REGISTER (R/W)
#define INA219_REG_SHUNTVOLTAGE                 (u8)(0x01)      // SHUNT VOLTAGE REGISTER (R)
#define INA219_REG_BUSVOLTAGE                   (u8)(0x02)      // BUS VOLTAGE REGISTER (R)
#define INA219_REG_POWER                        (u8)(0x03)      // POWER REGISTER (R)
#define INA219_REG_CURRENT                      (u8)(0x04)      // CURRENT REGISTER (R)
#define INA219_REG_CALIBRATION                  (u8)(0x05)      // CALIBRATION REGISTER (R/W)
 
 
/*----------------------------------------------------------------------------*/
// Macros for assigning config bits
#define INA219_CFGB_RESET(x)                    (u16)((x & 0x01) << 15)     // Reset Bit
#define INA219_CFGB_BUSV_RANGE(x)               (u16)((x & 0x01) << 13)     // Bus Voltage Range
#define INA219_CFGB_PGA_RANGE(x)                (u16)((x & 0x03) << 11)     // Shunt Voltage Range
#define INA219_CFGB_BADC_RES_AVG(x)             (u16)((x & 0x0F) << 7)      // Bus ADC Resolution/Averaging
#define INA219_CFGB_SADC_RES_AVG(x)             (u16)((x & 0x0F) << 3)      // Shunt ADC Resolution/Averaging
#define INA219_CFGB_MODE(x)                     (u16) (x & 0x07)            // Operating Mode
 
 
/*----------------------------------------------------------------------------*/
// Configuration Register
#define INA219_CFG_RESET                        INA219_CFGB_RESET(1)            // Reset Bit
 
#define INA219_CFG_BVOLT_RANGE_MASK             INA219_CFGB_BUSV_RANGE(1)       // Bus Voltage Range Mask
#define INA219_CFG_BVOLT_RANGE_16V              INA219_CFGB_BUSV_RANGE(0)       // 0-16V Range
#define INA219_CFG_BVOLT_RANGE_32V              INA219_CFGB_BUSV_RANGE(1)       // 0-32V Range (default)
 
#define INA219_CFG_SVOLT_RANGE_MASK             INA219_CFGB_PGA_RANGE(3)        // Shunt Voltage Range Mask
#define INA219_CFG_SVOLT_RANGE_40MV             INA219_CFGB_PGA_RANGE(0)        // Gain 1, 40mV Range
#define INA219_CFG_SVOLT_RANGE_80MV             INA219_CFGB_PGA_RANGE(1)        // Gain 2, 80mV Range
#define INA219_CFG_SVOLT_RANGE_160MV            INA219_CFGB_PGA_RANGE(2)        // Gain 4, 160mV Range
#define INA219_CFG_SVOLT_RANGE_320MV            INA219_CFGB_PGA_RANGE(3)        // Gain 8, 320mV Range (default)
 
#define INA219_CFG_BADCRES_MASK                 INA219_CFGB_BADC_RES_AVG(15)    // Bus ADC Resolution and Averaging Mask
#define INA219_CFG_BADCRES_9BIT_1S_84US         INA219_CFGB_BADC_RES_AVG(0)     // 1 x 9-bit Bus sample
#define INA219_CFG_BADCRES_10BIT_1S_148US       INA219_CFGB_BADC_RES_AVG(1)     // 1 x 10-bit Bus sample
#define INA219_CFG_BADCRES_11BIT_1S_276US       INA219_CFGB_BADC_RES_AVG(2)     // 1 x 11-bit Bus sample
#define INA219_CFG_BADCRES_12BIT_1S_532US       INA219_CFGB_BADC_RES_AVG(3)     // 1 x 12-bit Bus sample (default)
#define INA219_CFG_BADCRES_12BIT_2S_1MS         INA219_CFGB_BADC_RES_AVG(9)     // 2 x 12-bit Bus samples averaged together
#define INA219_CFG_BADCRES_12BIT_4S_2MS         INA219_CFGB_BADC_RES_AVG(10)    // 4 x 12-bit Bus samples averaged together
#define INA219_CFG_BADCRES_12BIT_8S_4MS         INA219_CFGB_BADC_RES_AVG(11)    // 8 x 12-bit Bus samples averaged together
#define INA219_CFG_BADCRES_12BIT_16S_8MS        INA219_CFGB_BADC_RES_AVG(12)    // 16 x 12-bit Bus samples averaged together
#define INA219_CFG_BADCRES_12BIT_32S_17MS       INA219_CFGB_BADC_RES_AVG(13)    // 32 x 12-bit Bus samples averaged together
#define INA219_CFG_BADCRES_12BIT_64S_34MS       INA219_CFGB_BADC_RES_AVG(14)    // 64 x 12-bit Bus samples averaged together
#define INA219_CFG_BADCRES_12BIT_128S_68MS      INA219_CFGB_BADC_RES_AVG(15)    // 128 x 12-bit Bus samples averaged together
 
#define INA219_CFG_SADCRES_MASK                 INA219_CFGB_SADC_RES_AVG(15)    // Shunt ADC Resolution and Averaging Mask
#define INA219_CFG_SADCRES_9BIT_1S_84US         INA219_CFGB_SADC_RES_AVG(0)     // 1 x 9-bit Shunt sample
#define INA219_CFG_SADCRES_10BIT_1S_148US       INA219_CFGB_SADC_RES_AVG(1)     // 1 x 10-bit Shunt sample
#define INA219_CFG_SADCRES_11BIT_1S_276US       INA219_CFGB_SADC_RES_AVG(2)     // 1 x 11-bit Shunt sample
#define INA219_CFG_SADCRES_12BIT_1S_532US       INA219_CFGB_SADC_RES_AVG(3)     // 1 x 12-bit Shunt sample (default)
#define INA219_CFG_SADCRES_12BIT_2S_1MS         INA219_CFGB_SADC_RES_AVG(9)     // 2 x 12-bit Shunt samples averaged together
#define INA219_CFG_SADCRES_12BIT_4S_2MS         INA219_CFGB_SADC_RES_AVG(10)    // 4 x 12-bit Shunt samples averaged together
#define INA219_CFG_SADCRES_12BIT_8S_4MS         INA219_CFGB_SADC_RES_AVG(11)    // 8 x 12-bit Shunt samples averaged together
#define INA219_CFG_SADCRES_12BIT_16S_8MS        INA219_CFGB_SADC_RES_AVG(12)    // 16 x 12-bit Shunt samples averaged together
#define INA219_CFG_SADCRES_12BIT_32S_17MS       INA219_CFGB_SADC_RES_AVG(13)    // 32 x 12-bit Shunt samples averaged together
#define INA219_CFG_SADCRES_12BIT_64S_34MS       INA219_CFGB_SADC_RES_AVG(14)    // 64 x 12-bit Shunt samples averaged together
#define INA219_CFG_SADCRES_12BIT_128S_68MS      INA219_CFGB_SADC_RES_AVG(15)    // 128 x 12-bit Shunt samples averaged together
 
#define INA219_CFG_MODE_MASK                    INA219_CFGB_MODE(7)             // Operating Mode Mask
#define INA219_CFG_MODE_POWERDOWN               INA219_CFGB_MODE(0)             // Power-Down
#define INA219_CFG_MODE_SVOLT_TRIGGERED         INA219_CFGB_MODE(1)             // Shunt Voltage, Triggered
#define INA219_CFG_MODE_BVOLT_TRIGGERED         INA219_CFGB_MODE(2)             // Bus Voltage, Triggered
#define INA219_CFG_MODE_SANDBVOLT_TRIGGERED     INA219_CFGB_MODE(3)             // Shunt and Bus, Triggered
#define INA219_CFG_MODE_ADCOFF                  INA219_CFGB_MODE(4)             // ADC Off (disabled)
#define INA219_CFG_MODE_SVOLT_CONTINUOUS        INA219_CFGB_MODE(5)             // Shunt Voltage, Continuous
#define INA219_CFG_MODE_BVOLT_CONTINUOUS        INA219_CFGB_MODE(6)             // Bus Voltage, Continuous
#define INA219_CFG_MODE_SANDBVOLT_CONTINUOUS    INA219_CFGB_MODE(7)             // Shunt and Bus, Continuous (default)
 
 
/*----------------------------------------------------------------------------*/
// Bus Voltage Register
#define INA219_BVOLT_CNVR                       (u16)(0x0002)       // Conversion Ready
#define INA219_BVOLT_OVF                        (u16)(0x0001)       // Math Overflow Flag
 
struct ina219_value_t
{
  s16 voltage;
  s32 shunt;
  s32 current;
  s32 power;
};

void drv_ina219_init(void);

s16 ina219_GetBusVoltage_mV(void);

s32 ina219_GetShuntVoltage_uV(void);

s32 ina219_GetCurrent_uA(void);

s32 ina219_GetPower_mW(void);

void ina219_process(void);

#endif //__DRV_INA219_H__

drv_ina219.c



#include "config.h"
#include "debug.h"
#include "app_config.h"

#include "STC32G_Timer.h"
#include "STC32G_GPIO.h"
#include "STC32G_NVIC.h"
#include "STC32G_Exti.h"
#include "STC32G_I2C.h"
#include "STC32G_Delay.h"
#include "STC32G_Switch.h"

#include "drv_ina219.h"

#if TCFG_DRV_INA219_SUPPORT

struct ina219_value_t ina219_value;

u16 ina219_calValue = 0;

u8  ina219_busVolt_LSB_mV = 4;      // Bus Voltage LSB value = 4mV
u8  ina219_shuntVolt_LSB_uV = 10;   // Shunt Voltage LSB value = 10uV

u32 ina219_current_LSB_uA;
u32 ina219_power_LSB_mW;
 
/**
 * @brief   INA219写寄存器
*/
void ina219_Write_Register(u8 reg, u16 dat)
{
    u8 val[2];
    
    val[0] = (u8)(dat >> 8);
    val[1] = (u8)(dat & 0xFF);
    I2C_WriteNbyte(INA219_I2C_ADDRESS, reg, val, 2);
}
 
/**
 * @brief   INA219读寄存器
*/
void ina219_Read_Register(u8 reg, u16 *dat)
{
    u8 val[2];
  
    I2C_ReadNbyte(INA219_I2C_ADDRESS, reg, val, 2);
    *dat = ((u16)(val[0]) << 8) + val[1]; 
}

// INA219 Set Calibration 16V/16A(Max) 0.02|?
void ina219_SetCalibration_16V_16A(void)
{
  u16 configValue;
  
  // By default we use a pretty huge range for the input voltage,
  // which probably isn't the most appropriate choice for system
  // that don't use a lot of power.  But all of the calculations
  // are shown below if you want to change the settings.  You will
  // also need to change any relevant register settings, such as
  // setting the VBUS_MAX to 16V instead of 32V, etc.
  
  // VBUS_MAX     = 16V   (Assumes 16V, can also be set to 32V)
  // VSHUNT_MAX   = 0.32  (Assumes Gain 8, 320mV, can also be 0.16, 0.08, 0.04)
  // RSHUNT       = 0.02   (Resistor value in ohms)
  
  // 1. Determine max possible current
  // MaxPossible_I = VSHUNT_MAX / RSHUNT
  // MaxPossible_I = 16A
  
  // 2. Determine max expected current
  // MaxExpected_I = 16A
  
  // 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
  // MinimumLSB = MaxExpected_I/32767
  // MinimumLSB = 0.00048            (0.48mA per bit)
  // MaximumLSB = MaxExpected_I/4096
  // MaximumLSB = 0,00390            (3.9mA per bit)
  
  // 4. Choose an LSB between the min and max values
  //    (Preferrably a roundish number close to MinLSB)
  // CurrentLSB = 0.00050            (500uA per bit)
  
  // 5. Compute the calibration register
  // Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
  // Cal = 4096 (0x1000)
  
  // ina219_calValue = 0x1000;
  ina219_calValue = 0x0D90;  //0x1000;
  
  // 6. Calculate the power LSB
  // PowerLSB = 20 * CurrentLSB
  // PowerLSB = 0.01 (10mW per bit)
  
  // 7. Compute the maximum current and shunt voltage values before overflow
  //
  // Max_Current = Current_LSB * 32767
  // Max_Current = 16.3835A before overflow
  //
  // If Max_Current > Max_Possible_I then
  //    Max_Current_Before_Overflow = MaxPossible_I
  // Else
  //    Max_Current_Before_Overflow = Max_Current
  // End If
  //
  // Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
  // Max_ShuntVoltage = 0.32V
  //
  // If Max_ShuntVoltage >= VSHUNT_MAX
  //    Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
  // Else
  //    Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
  // End If
  
  // 8. Compute the Maximum Power
  // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
  // MaximumPower = 1.6 * 16V
  // MaximumPower = 256W
  
  // Set multipliers to convert raw current/power values
  ina219_current_LSB_uA = 100;     // Current LSB = 500uA per bit
  ina219_power_LSB_mW = 2;         // Power LSB = 10mW per bit = 20 * Current LSB
  
  // Set Calibration register to 'Cal' calculated above
  ina219_Write_Register(INA219_REG_CALIBRATION, ina219_calValue);
  
  // Set Config register to take into account the settings above
  configValue = ( INA219_CFG_BVOLT_RANGE_16V | INA219_CFG_SVOLT_RANGE_320MV | INA219_CFG_BADCRES_12BIT_16S_8MS | INA219_CFG_SADCRES_12BIT_16S_8MS | INA219_CFG_MODE_SANDBVOLT_CONTINUOUS );
  // configValue = ( INA219_CFG_BVOLT_RANGE_16V | INA219_CFG_SVOLT_RANGE_320MV | INA219_CFG_BADCRES_12BIT_32S_17MS | INA219_CFG_SADCRES_12BIT_32S_17MS | INA219_CFG_MODE_SANDBVOLT_CONTINUOUS );
  
  ina219_Write_Register(INA219_REG_CONFIG, configValue);
}
 
void ina219_configureRegisters(void)
{
  delay_ms(15);
  
  ina219_SetCalibration_16V_16A();
}
 
s16 ina219_GetBusVoltage_raw(void)
{
  s16 val;
  
  ina219_Read_Register(INA219_REG_BUSVOLTAGE, (u16*)&val);
  val >>= 3;                      // Shift to the right 3 to drop CNVR and OVF

  return (val);
}
 
s16 ina219_GetCurrent_raw(void)
{
  s16 val;
  
  // Sometimes a sharp load will reset the INA219, which will
  // reset the cal register, meaning CURRENT and POWER will
  // not be available ... avoid this by always setting a cal
  // value even if it's an unfortunate extra step
  ina219_Write_Register(INA219_REG_CALIBRATION, ina219_calValue);
  
  // Now we can safely read the CURRENT register!
  ina219_Read_Register(INA219_REG_CURRENT, (u16*)&val);
  
  return (val);
}
 
 
s16 ina219_GetBusVoltage_mV(void)
{
  s16 val;
  
  ina219_Read_Register(INA219_REG_BUSVOLTAGE, (u16*)&val);
  val >>= 3;                      // Shift to the right 3 to drop CNVR and OVF
  val *= ina219_busVolt_LSB_mV;   // multiply by LSB(4mV)
  
  return (s16)(val);
}
 
s32 ina219_GetShuntVoltage_uV(void)
{
  s32 val;
  s16 reg;
  
  ina219_Read_Register(INA219_REG_SHUNTVOLTAGE, (u16*)&reg);
  val = (s32)reg * ina219_shuntVolt_LSB_uV;   // multiply by LSB(10uV)
  
  return (s32)val;
}
 
s32 ina219_GetCurrent_uA(void)
{
  s32 val;
  s16 reg;
  
  // Sometimes a sharp load will reset the INA219, which will
  // reset the cal register, meaning CURRENT and POWER will
  // not be available ... avoid this by always setting a cal
  // value even if it's an unfortunate extra step
  ina219_Write_Register(INA219_REG_CALIBRATION, ina219_calValue);
  
  // Now we can safely read the CURRENT register!
  ina219_Read_Register(INA219_REG_CURRENT, (u16*)&reg);
  
  val = (s32)reg * ina219_current_LSB_uA;
  
  return (s32)(val);
}
 
s32 ina219_GetPower_mW(void)
{
  s32 val;
  s16 reg;
  
  // Sometimes a sharp load will reset the INA219, which will
  // reset the cal register, meaning CURRENT and POWER will
  // not be available ... avoid this by always setting a cal
  // value even if it's an unfortunate extra step
  ina219_Write_Register(INA219_REG_CALIBRATION, ina219_calValue);
  
  // Now we can safely read the POWER register!
  ina219_Read_Register(INA219_REG_POWER, (u16*)&reg);
  
  val = (s32)reg * ina219_power_LSB_mW;
  
  return (s32)(val);
}
 
void ina219_process(void)
{
  
        // TODO: 总线电压 Vin-和GND之间的电压
        ina219_value.voltage = ina219_GetBusVoltage_mV();
        log_d("ina219 voltage is:%d(mV)\r\n", ina219_value.voltage);
        
        // TODO: 分流电阻两端电压
        ina219_value.shunt = ina219_GetShuntVoltage_uV();
        log_d("ina219 shunt   is:%ld(uV)\r\n",ina219_value.shunt);
            
        // TODO: 电流
        ina219_value.current = ina219_GetCurrent_uA();
        log_d("ina219 current is:%ld(uA)\r\n",ina219_value.current);
        
        ina219_value.power = ina219_GetPower_mW();
        log_d("ina219 power   is:%ld(mW)\r\n",ina219_value.power);			
}


void drv_ina219_init(void)
{
    I2C_InitTypeDef i2c;
    i2c.I2C_Enable = ENABLE;
    i2c.I2C_Mode = I2C_Mode_Master;
    i2c.I2C_Speed = MAIN_Fosc/2/(100000*2+4);
    i2c.I2C_MS_WDTA = DISABLE;
    i2c.I2C_SL_MA = ENABLE;
    I2C_Init(&i2c);

    // NVIC_I2C_Init(I2C_Mode_Master, ENABLE, Priority_1);  // I2C Master模式可不用开中断

    P2_MODE_IO_PU(GPIO_Pin_4);
    P2_MODE_IO_PU(GPIO_Pin_5);

    I2C_SW(I2C_P24_P25);

    ina219_configureRegisters();

    log_d("INA219 Driver Init\n");
}

#endif


main.c

main()
{
	// 
	drv_ina219_init();
	ina219_process();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SongYuLong的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值