INA219 技术笔记

INA219 技术笔记_第1张图片

INA219 技术笔记_第2张图片

INA219 技术笔记_第3张图片

INA219 技术笔记_第4张图片

四、代码主体(仅参考,非完全体)

/*.c-------------------------------------------------------------------------------------------------------------------------------------*/


//获取电压(单位:mv)
int sensor_vbus_voltage_mv_get( struct current_sensor* sensor )
{
    static unsigned int value_reg;

    value_reg = cur_sensor_register_read( sensor, INA219_REG_BUSVOLTAGE );

    //右移3为去掉:bit2,CNVR,OVF三位,再乘以 4MV (官方文档规定),得到当前总线的电压值

    return (int)(( value_reg >> 3)*4);         
}

//获取电流(单位:mA)
int sensor_current_ma_get( struct current_sensor* sensor )
{
    static int value_reg;//static unsigned int

    cur_sensor_register_write( sensor, INA219_REG_CONFIG,INA219_CONFIG_value);
    value_reg = cur_sensor_register_read( sensor, INA219_REG_CURRENT);

    if( value_reg < 0)
    {   //如果是负数,说明电流方向为反,此处取绝对值
        value_reg = -value_reg;
    }
    _nop();
    _nop();

    return (int)(value_reg * IAN_I_LSB);        //得到寄存器的值在乘以每位对应的值(IAN_I_LSB)得到实际的电流
}

//获取当前功率(单位:mw)
int sensor_power_mw_get( struct current_sensor* sensor )
{
    unsigned int value_reg;

    value_reg = cur_sensor_register_read( sensor, INA219_REG_POWER);

    _nop();
    _nop();

    return (int)(value_reg * INA_Power_LSB);   //得到寄存器的值在乘以每位对应的值(INA_Power_LSB)得到实际的功率
}


/* ------------------------------------------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------------------------------------------ */
void cur_sensor_initial(void)
{
    unsigned char i = 0;

    for( i = 0; i < SENSOR_NUM; i++ )
    {
        /* initial sensor i */
        cur_sensor[i].dev_addr = sensor_addr[i];
        cur_sensor_register_write( &cur_sensor[i], INA219_REG_CONFIG, INA219_CONFIG_value);
        cur_sensor_register_write( &cur_sensor[i], INA219_REG_CALIBRATION, INA_CAL);
    }
}

 

/*.h-------------------------------------------------------------------------------------------------------------------------------------*/
// 寄存器地址
#define INA219_REG_CONFIG                       (unsigned char)(0x00)      // 模式配置寄存器 (R/W)
#define INA219_REG_SHUNTVOLTAGE                 (unsigned char)(0x01)      // 分流电阻电压寄存器 (R)
#define INA219_REG_BUSVOLTAGE                   (unsigned char)(0x02)      // 总线电压寄存器 (R)
#define INA219_REG_POWER                        (unsigned char)(0x03)      // 功率寄存器 (R)
#define INA219_REG_CURRENT                      (unsigned char)(0x04)      // 电流寄存器 (R)
#define INA219_REG_CALIBRATION                  (unsigned char)(0x05)      // 基准值寄存器 (R/W)

//寄存器(0x01)参数定义
#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)

//INA配置的参数
#define INA_R                                   0.1     //阻值大小(单位ohm)
#define INA_I_MAX                               3       //硬件最大检测电流(单位:A)    INA219_CONFIG_GAIN_电压/INA_R
#define IAN_I_LSB                               0.1     //(0x04)检测出值对应每位电流值 选择接近 = (INA_I_MAX/32767(2^15))*1000(为了得到单位为A)       (A)
#define INA_Power_LSB                           2       //(0x03)检测出值对应每位功率值 = 20 * IAN_I_LSB    (MW)
#define INA_CAL                                 4096    //(0x05)寄存器基准值

//根据上面定义设置 配置寄存器(0x00)的值:0x3C1F
#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)

 

 

你可能感兴趣的:(编程)