'' ================================================================================================= '' '' File....... jm_ina219.spin '' Purpose.... '' Author..... Jon "JonnyMac" McPhalen '' Copyright (C) 2014 Jon McPhalen '' -- see below for terms of use '' E-mail..... jon@jonmcphalen.com '' Started.... '' Updated.... 18 NOV 2014 '' '' ================================================================================================= con { fixed io pins } RX1 = 31 ' programming / terminal TX1 = 30 SDA1 = 29 ' eeprom / i2c SCL1 = 28 con { ina219 registers } REG_CONFIG = $00 REG_SVOLTS = $01 REG_BVOLTS = $02 REG_POWER = $03 REG_CURRENT = $04 REG_CAL = $05 con { ina219 calibration bits } RST = 1 << 15 BRNG_32 = 1 << 13 ' 32V bus range BRNG_16 = 0 ' 16V PG_040 = %00 << 11 ' 40mV shunt range PG_080 = %01 << 11 ' 80mV PG_160 = %10 << 11 ' 160mV PG_320 = %11 << 11 ' 320mV BADC_009B = %0000 << 7 ' 9-bit BADC_010B = %0001 << 7 ' 10-bit BADC_011B = %0010 << 7 ' 11-bit BADC_012B = %0011 << 7 ' 12-bit SADC_001X = %1000 << 3 ' 1x sampling, 12-bit SADC_002X = %1001 << 3 ' 2x sampling SADC_004X = %1010 << 3 SADC_008X = %1011 << 3 SADC_016X = %1100 << 3 SADC_032X = %1101 << 3 SADC_064X = %1110 << 3 SADC_128X = %1111 << 3 ' 128x sampling MD_PD = %000 << 0 ' power-down MD_SVT = %001 << 0 ' shunt voltage, triggered MD_BVT = %010 << 0 ' bus voltage, triggered MD_SBT = %011 << 0 ' shunt and bus, triggered MD_XADC = %100 << 0 ' adc off (disabled) MD_SVC = %101 << 0 ' shunt voltage, continuous MD_BVC = %110 << 0 ' bus voltage, continuous MD_SBC = %111 << 0 ' shunt and bus, continuous obj i2c : "jm_i2c" var byte devid ' i2c device id pub null ' This is not a top-level object pub start(sclpin, sdapin, addr) '' Start INA219 object '' -- sclpin and sdapin define i2c bus '' -- addr is device address, 0 to 3 i2c.setupx(sclpin, sdapin) ' connect to i2c bus case addr %00 : devid := %1000_000_0 %01 : devid := %1000_001_0 %10 : devid := %1000_100_0 %11 : devid := %1000_101_0 pub present '' Returns true if device present return i2c.present(devid) pub configure(cfg) '' Write configuration word to INA219 wr_reg(REG_CONFIG, cfg) pub shunt_volts | smv '' Returns voltage across shunt in millivolts smv := rd_reg(REG_SVOLTS) return ~~smv ' extend sign bit pub bus_volts | bmv '' Returns bus voltage in millivolts '' -- returns 0 if bad reading bmv := rd_reg(REG_BVOLTS) ifnot (bmv & 1) ' if good reading return (bmv >> 3) << 2 ' extract millivolts pub power '' Returns power register return rd_reg(REG_POWER) pub current | ib '' Returns bus current ib := rd_reg(REG_CURRENT) return ~~ib pub calibrate(cal) '' Writes 16-bit calibration register wr_reg(REG_CAL, cal) pri wr_reg(reg, value) '' Write 16-bit value to register i2c.start i2c.write(devid) i2c.write(reg) i2c.write(value.byte[1]) i2c.write(value.byte[0]) i2c.stop pri rd_reg(reg) : value '' Read 16-bit value from register i2c.start i2c.write(devid) i2c.write(reg) i2c.start i2c.write(devid | 1) value.byte[1] := i2c.read(i2c#ACK) value.byte[0] := i2c.read(i2c#NAK) i2c.stop dat { license } {{ Terms of Use: MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }}