ARM Mbed library for TI INA226. High-Side or Low-Side Measurement, Bi-Directional Current and Power Monitor with I2C Compatible Interface.

Committer:
Branilson Luiz
Date:
Mon Sep 09 02:34:29 2019 -0300
Revision:
0:ed5e54b4383d
First commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Branilson Luiz 0:ed5e54b4383d 1 /*
Branilson Luiz 0:ed5e54b4383d 2 * Copyright (c) 2019 Branilson Luiz
Branilson Luiz 0:ed5e54b4383d 3 * main.hpp - INA226: polling multiple devices example using the ina226
Branilson Luiz 0:ed5e54b4383d 4 * Mbed Library.
Branilson Luiz 0:ed5e54b4383d 5 *
Branilson Luiz 0:ed5e54b4383d 6 * branilson (at) gmail dot com
Branilson Luiz 0:ed5e54b4383d 7 * Github: https://github.com/branilson/ina226_mbed_library
Branilson Luiz 0:ed5e54b4383d 8 *
Branilson Luiz 0:ed5e54b4383d 9 * This program is free software: you can redistribute it and/or modify it un-
Branilson Luiz 0:ed5e54b4383d 10 * der the terms of the version 3 GNU General Public License as published by
Branilson Luiz 0:ed5e54b4383d 11 * the Free Software Foundation.
Branilson Luiz 0:ed5e54b4383d 12 * This program is distributed in the hope that it will be useful, but WITHOUT
Branilson Luiz 0:ed5e54b4383d 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT-
Branilson Luiz 0:ed5e54b4383d 14 * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
Branilson Luiz 0:ed5e54b4383d 15 * details.
Branilson Luiz 0:ed5e54b4383d 16 * You should have received a copy of the GNU General Public License along with
Branilson Luiz 0:ed5e54b4383d 17 * this program. If not, see <http://www.gnu.org/licenses/>.
Branilson Luiz 0:ed5e54b4383d 18 */
Branilson Luiz 0:ed5e54b4383d 19
Branilson Luiz 0:ed5e54b4383d 20 #include "mbed.h"
Branilson Luiz 0:ed5e54b4383d 21 #include "ina226.hpp"
Branilson Luiz 0:ed5e54b4383d 22
Branilson Luiz 0:ed5e54b4383d 23 Serial pc(USBTX, USBRX);
Branilson Luiz 0:ed5e54b4383d 24 DigitalOut myled(LED1);
Branilson Luiz 0:ed5e54b4383d 25 I2C i2c(PB_7, PB_6);
Branilson Luiz 0:ed5e54b4383d 26 unsigned const int I2C_FREQ = 400000;
Branilson Luiz 0:ed5e54b4383d 27 unsigned const int NUM_DEVICES = 4;
Branilson Luiz 0:ed5e54b4383d 28 const int ina_addr[NUM_DEVICES] = {0x80, 0x82, 0x8a, 0x88};
Branilson Luiz 0:ed5e54b4383d 29 const float current_limits[NUM_DEVICES] = {0.01, 0.02, 0.02, 1.0};
Branilson Luiz 0:ed5e54b4383d 30 ina226 ina[NUM_DEVICES] = {ina226(i2c, ina_addr[0], I2C_FREQ),
Branilson Luiz 0:ed5e54b4383d 31 ina226(i2c, ina_addr[1], I2C_FREQ),
Branilson Luiz 0:ed5e54b4383d 32 ina226(i2c, ina_addr[2], I2C_FREQ),
Branilson Luiz 0:ed5e54b4383d 33 ina226(i2c, ina_addr[3], I2C_FREQ)};
Branilson Luiz 0:ed5e54b4383d 34
Branilson Luiz 0:ed5e54b4383d 35 int main() {
Branilson Luiz 0:ed5e54b4383d 36 pc.printf("INA226 TEST Program. (BUILD:[" __DATE__ "/" __TIME__ "])\n");
Branilson Luiz 0:ed5e54b4383d 37 int count = 1;
Branilson Luiz 0:ed5e54b4383d 38 unsigned int i;
Branilson Luiz 0:ed5e54b4383d 39 for (i=0; i < NUM_DEVICES; i++) { // setConfig/setCalibration batch
Branilson Luiz 0:ed5e54b4383d 40 pc.printf("INA226 Address %xh Config return: %d\n",
Branilson Luiz 0:ed5e54b4383d 41 ina_addr[i],
Branilson Luiz 0:ed5e54b4383d 42 ina[i].setConfig(AVERAGES_64,
Branilson Luiz 0:ed5e54b4383d 43 BUS_CONV_TIME_1100US,
Branilson Luiz 0:ed5e54b4383d 44 SHUNT_CONV_TIME_1100US,
Branilson Luiz 0:ed5e54b4383d 45 MODE_SHUNT_BUS_CONT));
Branilson Luiz 0:ed5e54b4383d 46 pc.printf("INA226 Address %xh Calibration return: %d\n",
Branilson Luiz 0:ed5e54b4383d 47 ina_addr[i],
Branilson Luiz 0:ed5e54b4383d 48 ina[i].setCalibration(0.01, 1.0));
Branilson Luiz 0:ed5e54b4383d 49 ina[i].enableShuntOverVoltageAlert();
Branilson Luiz 0:ed5e54b4383d 50 ina[i].setOverCurrentLimit(current_limits[i]);
Branilson Luiz 0:ed5e54b4383d 51 }
Branilson Luiz 0:ed5e54b4383d 52
Branilson Luiz 0:ed5e54b4383d 53 while (1) {
Branilson Luiz 0:ed5e54b4383d 54 pc.printf("\n%d:\n", count);
Branilson Luiz 0:ed5e54b4383d 55
Branilson Luiz 0:ed5e54b4383d 56 for (i=0; i < NUM_DEVICES; i++) {
Branilson Luiz 0:ed5e54b4383d 57 pc.printf("Device %xh: ManID %d, DieID %d, Cal %d, ShuntV %+2.6fV, %+2.6fV, %+2.6fA, %+2.6fW\n",
Branilson Luiz 0:ed5e54b4383d 58 ina_addr[i],
Branilson Luiz 0:ed5e54b4383d 59 ina[i].readManufacturerID(),
Branilson Luiz 0:ed5e54b4383d 60 ina[i].readDieID(),
Branilson Luiz 0:ed5e54b4383d 61 ina[i].readCalibration(),
Branilson Luiz 0:ed5e54b4383d 62 ina[i].readShuntVoltage(),
Branilson Luiz 0:ed5e54b4383d 63 ina[i].readBusVoltage(),
Branilson Luiz 0:ed5e54b4383d 64 ina[i].readCurrent(),
Branilson Luiz 0:ed5e54b4383d 65 ina[i].readPower());
Branilson Luiz 0:ed5e54b4383d 66 if (ina[i].isAlert()) {
Branilson Luiz 0:ed5e54b4383d 67 pc.printf("Overcurrent detected on device %xh\n", ina_addr[i]);
Branilson Luiz 0:ed5e54b4383d 68 }
Branilson Luiz 0:ed5e54b4383d 69 }
Branilson Luiz 0:ed5e54b4383d 70
Branilson Luiz 0:ed5e54b4383d 71 myled = 1;
Branilson Luiz 0:ed5e54b4383d 72 wait(1);
Branilson Luiz 0:ed5e54b4383d 73 myled = 0;
Branilson Luiz 0:ed5e54b4383d 74 wait(1);
Branilson Luiz 0:ed5e54b4383d 75 count++;
Branilson Luiz 0:ed5e54b4383d 76 }
Branilson Luiz 0:ed5e54b4383d 77 }