Send INA219 voltage and current to USB
Fork of mbed_Shield_Accelerometer by
main.cpp@3:1202856c8578, 2014-06-30 (annotated)
- Committer:
- x893
- Date:
- Mon Jun 30 08:29:36 2014 +0000
- Revision:
- 3:1202856c8578
- Parent:
- 2:9e18b475f4da
Send voltage and current to USB
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:494536c26f91 | 1 | #include "mbed.h" |
x893 | 2:9e18b475f4da | 2 | #include "INA219.h" |
x893 | 2:9e18b475f4da | 3 | |
x893 | 2:9e18b475f4da | 4 | Serial debug(USBTX, USBRX); |
x893 | 2:9e18b475f4da | 5 | DigitalOut led(LED1); |
x893 | 2:9e18b475f4da | 6 | INA219 ina219(PB_9, PB_8, 0x80, 100000); |
screamer | 0:494536c26f91 | 7 | |
screamer | 0:494536c26f91 | 8 | int main() |
screamer | 0:494536c26f91 | 9 | { |
x893 | 2:9e18b475f4da | 10 | double V,C, V0, C0; |
x893 | 2:9e18b475f4da | 11 | unsigned short ctrl; |
x893 | 2:9e18b475f4da | 12 | |
x893 | 2:9e18b475f4da | 13 | debug.format(8); |
x893 | 2:9e18b475f4da | 14 | debug.baud(115200); |
x893 | 2:9e18b475f4da | 15 | debug.printf("INA219 - (Build:[" __DATE__ "/" __TIME__ "])\r\n\n"); |
x893 | 2:9e18b475f4da | 16 | |
x893 | 2:9e18b475f4da | 17 | while (1) |
x893 | 2:9e18b475f4da | 18 | { |
x893 | 2:9e18b475f4da | 19 | ctrl = ina219.getRegister(0x00); |
x893 | 2:9e18b475f4da | 20 | if (ctrl != 0x399F) // Control register after power on |
x893 | 2:9e18b475f4da | 21 | { |
x893 | 2:9e18b475f4da | 22 | debug.printf("--- INA219 not found ---\r\n"); |
x893 | 2:9e18b475f4da | 23 | ina219.getI2C().frequency(100000); |
x893 | 2:9e18b475f4da | 24 | led = 0; |
x893 | 2:9e18b475f4da | 25 | } |
x893 | 2:9e18b475f4da | 26 | else |
x893 | 2:9e18b475f4da | 27 | { |
x893 | 2:9e18b475f4da | 28 | V = ina219.getVoltage(); // in V |
x893 | 2:9e18b475f4da | 29 | C = ina219.getCurrent() * 1000.0; // in mA |
x893 | 2:9e18b475f4da | 30 | if (V0 != V || C0 != C) |
x893 | 2:9e18b475f4da | 31 | { // Voltage or Current changed |
x893 | 2:9e18b475f4da | 32 | V0 = V; |
x893 | 2:9e18b475f4da | 33 | C0 = C; |
x893 | 2:9e18b475f4da | 34 | debug.printf(" V:%2.4f V I:%4.1f mA \r", V, C); |
x893 | 2:9e18b475f4da | 35 | } |
x893 | 2:9e18b475f4da | 36 | led = !led; |
x893 | 2:9e18b475f4da | 37 | } |
x893 | 2:9e18b475f4da | 38 | wait(0.25); |
screamer | 0:494536c26f91 | 39 | } |
x893 | 2:9e18b475f4da | 40 | } |