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