News:

Let's find out together what makes a PIC Tick!

Main Menu

INA219 (Bidirectional Current / Power Monitor, I2C Interface)

Started by OG, Mar 02, 2021, 01:20 AM

Previous topic - Next topic

OG

INA219 Zerø-Drift, Bidirectional Current / Power Monitor with I2C Interface


Sample Code by Les


INA219 Datasheet

RGV250

Hi,
I had a request for my version of the code so here it is, it is quite old but should be easy to fix if it does not compile.
INA219.zip

Bob

Dolci



flosigud

Here is my INA219 code.

'    ----------------------------------------------------------------------------
'    Name    : INA219.BAS
'    Author    : Flosi Guðmundsson
'    Notice    : Copyright (c) 2020 Flosi Guðmundsson
'            : All Rights Reserved
'    Date    : 24.9.2020
'    Version    : 1.0
'    Notes    : Driver For the INA219 current sensor
'            : Based on code written by Bryan Siepert
'            : and Kevin "KTOWN" Townsend for Adafruit Industries.
'    ----------------------------------------------------------------------------


'   Include "33fj128GP802.inc"
    Include "33FJ12MC202.inc"
'   Include "33FJ32MC302.inc"
    Include "ina219.inc"
'    ----------------------------------------------------------------------------


'    Floating point variables to hold results
'    ----------------------------------------------------------------------------
    Dim fShuntVoltage_mV As Float
    Dim fBusVoltage_mV As Float
    Dim fCurrent_mA As Float
    Dim fLoadVoltage As Float
    Dim fPower_mW As Float

'   Set Calibration
'    ----------------------------------------------------------------------------
'    setCAL_32V_2A()
'    setCAL_32V_1A()
'    setCAL_16V_1A()


'    ----------------------------------------------------------------------------

    HRSOut "Measuring voltage and current with INA219 ...",13

    Do
        setCAL_16V_1A()                                ' Calibrate INA219 in case of a reset
        fShuntVoltage_mV = fGetShuntVoltage_mV()    '
        fBusVoltage_mV = fGetBusVoltage_V()
        fCurrent_mA = fGetCurrent_mA()
        fPower_mW = fGetPower_mW()
        fLoadVoltage = fBusVoltage_mV + (fShuntVoltage_mV / 1000)

        HRSOut "Bus Voltage:    ", Dec fBusVoltage_mV, " V", 13
        HRSOut "Shunt Voltage:  ", Dec fShuntVoltage_mV, " mV", 13
        HRSOut "Load Voltage:   ", Dec fLoadVoltage, " V", 13
        HRSOut "Current:        ", Dec fCurrent_mA, " mA", 13
        HRSOut "Power:            ", Dec fPower_mW, " mW", 13

        DelayMS 500
    Loop


'    ----------------------------------------------------------------------------
And the include:

' ----------------------------------------------------------------------------
' Name : INA19.inc
' Author : Flosi Guðmundsson
' Date : 5.5.2019
' Version : 1.0
' Notes : Driver for the INA219 current sensor
' : Based on code written by Bryan Siepert
' : and Kevin "KTOWN" Townsend for Adafruit Industries.
' ----------------------------------------------------------------------------

' Commands
' ----------------------------------------------------------------------------

' setCalibration()
' setMultipliers()
' setCAL_32V_2A()
' setCAL_32V_1A()
' setCAL_16V_1A()
' fGetBusVoltage_V()
' fGetShuntVoltage_mV
' fGetCurrent_mA()
' fGetPower_mW()


' Constants
' ----------------------------------------------------------------------------
'
Symbol INA219_ADDRESS $80 ' default I2C INA219_ADDRESS

Symbol CONFIG_RESET $8000 ' Reset Bit
Symbol CONFIG_BVOLTAGERANGE_MASK $2000 ' Bus Voltage Range Mask
Symbol CONFIG_BVOLTAGERANGE_16V $0000 ' 0-16V Range
Symbol CONFIG_BVOLTAGERANGE_32V $2000 ' 0-32V Range
Symbol CONFIG_GAIN_MASK $1800 ' Gain Mask
Symbol CONFIG_GAIN_1_40MV $0000 ' Gain 1, 40mV Range
Symbol CONFIG_GAIN_2_80MV $0800 ' Gain 2, 80mV Range
Symbol CONFIG_GAIN_4_160MV $1000 ' Gain 4, 160mV Range
Symbol CONFIG_GAIN_8_320MV $1800 ' Gain 8, 320mV Range
Symbol CONFIG_BADCRES_MASK $0780 ' Bus ADC Resolution Mask
Symbol CONFIG_BADCRES_9BIT $0000 ' 9-bit bus res  0..511
Symbol CONFIG_BADCRES_10BIT $0080 ' 10-bit bus res  0..1023
Symbol CONFIG_BADCRES_11BIT $0100 ' 11-bit bus res  0..2047
Symbol CONFIG_BADCRES_12BIT $0180 ' 12-bit bus res  0..4097
Symbol CONFIG_SADCRES_MASK $0078 ' Shunt ADC Resolution and Averaging Mask
Symbol CONFIG_SADCRES_9BIT_1S_84US $0000 ' 1 x 9-bit shunt sample
Symbol CONFIG_SADCRES_10BIT_1S_148US $0008 ' 1 x 10-bit shunt sample
Symbol CONFIG_SADCRES_11BIT_1S_276US $0010 ' 1 x 11-bit shunt sample
Symbol CONFIG_SADCRES_12BIT_1S_532US $0018 ' 1 x 12-bit shunt sample
Symbol CONFIG_SADCRES_12BIT_2S_1060US $0048 ' 2 x 12-bit shunt samples averaged together
Symbol CONFIG_SADCRES_12BIT_4S_2130US $0050 ' 4 x 12-bit shunt samples averaged together
Symbol CONFIG_SADCRES_12BIT_8S_4260US $0058 ' 8 x 12-bit shunt samples averaged together
Symbol CONFIG_SADCRES_12BIT_16S_8510US $0060 ' 16 x 12-bit shunt samples averaged together
Symbol CONFIG_SADCRES_12BIT_32S_17MS $0068 ' 32 x 12-bit shunt samples averaged together
Symbol CONFIG_SADCRES_12BIT_64S_34MS $0070 ' 64 x 12-bit shunt samples averaged together
Symbol CONFIG_SADCRES_12BIT_128S_69MS $0078 ' 128 x 12-bit shunt samples averaged together
Symbol CONFIG_MODE_MASK $0007 ' Operating Mode Mask
Symbol CONFIG_MODE_POWERDOWN $0000
Symbol CONFIG_MODE_SVOLT_TRIGGERED $0001
Symbol CONFIG_MODE_BVOLT_TRIGGERED $0002
Symbol CONFIG_MODE_SANDBVOLT_TRIGGERED $0003
Symbol CONFIG_MODE_ADCOFF $0004
Symbol CONFIG_MODE_SVOLT_CONTINUOUS $0005
Symbol CONFIG_MODE_BVOLT_CONTINUOUS $0006
Symbol CONFIG_MODE_SANDBVOLT_CONTINUOUS $0007


' INA219 registers
' ----------------------------------------------------------------------------
'
Symbol REG_CONFIG $00 ' config register
Symbol REG_SHUNTVOLTAGE $01 ' shunt voltage register
Symbol REG_BUSVOLTAGE $02 ' bus voltage register
Symbol REG_POWER $03 ' power register
Symbol REG_CURRENT $04 ' current register
Symbol REG_CALIBRATION $05 ' calibration register


' Config
' ----------------------------------------------------------------------------
'
Symbol  CONFIG_32V_2A CONFIG_BVOLTAGERANGE_32V | CONFIG_GAIN_8_320MV | CONFIG_BADCRES_12BIT | CONFIG_SADCRES_12BIT_1S_532US | CONFIG_MODE_SANDBVOLT_CONTINUOUS
Symbol  CONFIG_32V_1A CONFIG_BVOLTAGERANGE_32V | CONFIG_GAIN_8_320MV | CONFIG_BADCRES_12BIT | CONFIG_SADCRES_12BIT_1S_532US | CONFIG_MODE_SANDBVOLT_CONTINUOUS
Symbol  CONFIG_16V_1A CONFIG_BVOLTAGERANGE_16V | CONFIG_GAIN_1_40MV | CONFIG_BADCRES_12BIT | CONFIG_SADCRES_12BIT_1S_532US | CONFIG_MODE_SANDBVOLT_CONTINUOUS


' Calibration values
' ----------------------------------------------------------------------------
'
Symbol  CAL_32V_2A 4096
Symbol  CAL_32V_1A 10240
Symbol  CAL_16V_1A 8192



' Floating point variables to hold multipliers
' ----------------------------------------------------------------------------
'
Dim fBusVoltageMultiplier_mV As Float
Dim fShuntVoltageMultiplier_mV As Float
Dim fCurrentMultiplier_mA As Float
Dim fPowerMultiplier_mW As Float
    Dim wTmp As SWord

' setCalibration()
' ----------------------------------------------------------------------------
'

$define setCalibration(CalValue) '
HBusOut INA219_ADDRESS,REG_CALIBRATION, [CalValue]

' setConfig()
' ----------------------------------------------------------------------------
'

$define setConfig(config) '
HBusOut INA219_ADDRESS,REG_CONFIG, [config]


' setMultipliers()
' ----------------------------------------------------------------------------
'
$define setMultipliers(BusVoltageMultiplier_mV, ShuntVoltageMultiplier_mV, CurrentMultiplier_mA, PowerMultiplier_mW) '
fBusVoltageMultiplier_mV = BusVoltageMultiplier_mV '
fShuntVoltageMultiplier_mV = ShuntVoltageMultiplier_mV '
fCurrentMultiplier_mA = CurrentMultiplier_mA '
fPowerMultiplier_mW = PowerMultiplier_mW


' setCAL_32V_2A()
' ----------------------------------------------------------------------------
'
$define setCAL_32V_2A() '
setCalibration(CAL_32V_2A) '
setConfig(CONFIG_32V_2A) '
setMultipliers(0.004, 0.04, 0.05, 0.8)


' setCAL_32V_1A()
' ----------------------------------------------------------------------------

$define setCAL_32V_1A() '
setCalibration(CAL_32V_1A) '
setConfig(CONFIG_32V_1A) '
setMultipliers(0.004, 0.04, 0.05, 1)

   
' setCAL_16V_1A()
' ----------------------------------------------------------------------------

$define setCAL_16V_1A() '
setCalibration(CAL_16V_1A) '
setConfig(CONFIG_16V_1A) '
setMultipliers(0.004, 0.05, 0.05, 1)






' fGetBusVoltage_V()
' ----------------------------------------------------------------------------
' Returns bus voltage in V

    $define fGetBusVoltage_V _fGetBusVoltage_V
Proc _fGetBusVoltage_V(), Float
wTmp = HBusIn INA219_ADDRESS, REG_BUSVOLTAGE
wTmp = wTmp >> 3
Result = wTmp * fBusVoltageMultiplier_mV
EndProc


' fGetShuntVoltage_mV
' ----------------------------------------------------------------------------
' Returns shunt voltage in mV

    $define fGetShuntVoltage_mV _fGetShuntVoltage_mV
Proc _fGetShuntVoltage_mV(), Float
wTmp = HBusIn INA219_ADDRESS, REG_SHUNTVOLTAGE
Result = wTmp * fShuntVoltageMultiplier_mV
EndProc


' fGetCurrent_mA()
' ----------------------------------------------------------------------------
' Returns current in mA

    $define fGetCurrent_mA _fGetCurrent_mA
Proc _fGetCurrent_mA(), Float
wTmp = HBusIn INA219_ADDRESS, REG_CURRENT
Result = wTmp * fCurrentMultiplier_mA                   
EndProc


' fGetPower_mW()
' ----------------------------------------------------------------------------
' Returns power in mW

    $define fGetPower_mW _fGetPower_mW
Proc _fGetPower_mW(), Float
wTmp = HBusIn INA219_ADDRESS, REG_POWER
Result = wTmp * fPowerMultiplier_mW
EndProc

' ----------------------------------------------------------------------------