PNI
/
BatteryESRTester
First working, tested and calibrated unit
main.cpp@4:f7cc009aed74, 2017-03-27 (annotated)
- Committer:
- ruairilong
- Date:
- Mon Mar 27 21:01:21 2017 +0000
- Revision:
- 4:f7cc009aed74
- Parent:
- 3:a170b248ead8
Format code and remove unused functions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ruairilong | 2:ddc96642fcdb | 1 | /* mbed USBJoystick Library Demo |
ruairilong | 2:ddc96642fcdb | 2 | * Copyright (c) 2012, v01: Initial version, WH, |
ruairilong | 2:ddc96642fcdb | 3 | * Modified USBMouse code ARM Limited. |
ruairilong | 2:ddc96642fcdb | 4 | * (c) 2010-2011 mbed.org, MIT License |
ruairilong | 2:ddc96642fcdb | 5 | * 2016, v02: Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button |
ruairilong | 2:ddc96642fcdb | 6 | * |
ruairilong | 2:ddc96642fcdb | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
ruairilong | 2:ddc96642fcdb | 8 | * of this software and associated documentation files (the "Software"), to deal |
ruairilong | 2:ddc96642fcdb | 9 | * in the Software without restriction, inclumosig without limitation the rights |
ruairilong | 2:ddc96642fcdb | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
ruairilong | 2:ddc96642fcdb | 11 | * copies of the Software, and to permit persons to whom the Software is |
ruairilong | 2:ddc96642fcdb | 12 | * furnished to do so, subject to the following conditions: |
ruairilong | 2:ddc96642fcdb | 13 | * |
ruairilong | 2:ddc96642fcdb | 14 | * The above copyright notice and this permission notice shall be included in |
ruairilong | 2:ddc96642fcdb | 15 | * all copies or substantial portions of the Software. |
ruairilong | 2:ddc96642fcdb | 16 | * |
ruairilong | 2:ddc96642fcdb | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
ruairilong | 2:ddc96642fcdb | 18 | * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
ruairilong | 2:ddc96642fcdb | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
ruairilong | 2:ddc96642fcdb | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
ruairilong | 2:ddc96642fcdb | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ruairilong | 2:ddc96642fcdb | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
ruairilong | 2:ddc96642fcdb | 23 | * THE SOFTWARE. |
ruairilong | 2:ddc96642fcdb | 24 | */ |
JoeMiller | 0:197cfa57c3c7 | 25 | |
ruairilong | 2:ddc96642fcdb | 26 | // Note: you must connect the usb cable to your computer before the program will proceed |
JoeMiller | 0:197cfa57c3c7 | 27 | |
ruairilong | 2:ddc96642fcdb | 28 | // USB parts from: https://developer.mbed.org/forum/helloworld/topic/3496/ |
JoeMiller | 0:197cfa57c3c7 | 29 | |
JoeMiller | 0:197cfa57c3c7 | 30 | #include "mbed.h" |
ruairilong | 2:ddc96642fcdb | 31 | #include "USBKeyboard.h" |
ruairilong | 2:ddc96642fcdb | 32 | |
ruairilong | 2:ddc96642fcdb | 33 | //USBMouse mouse; |
ruairilong | 2:ddc96642fcdb | 34 | USBKeyboard mbedKeyBoard; |
ruairilong | 2:ddc96642fcdb | 35 | // Variables for Heartbeat and Status monitoring |
ruairilong | 2:ddc96642fcdb | 36 | PwmOut myled1(LED1); |
ruairilong | 2:ddc96642fcdb | 37 | PwmOut myled2(LED2); |
ruairilong | 2:ddc96642fcdb | 38 | PwmOut myled3(LED3); |
ruairilong | 2:ddc96642fcdb | 39 | DigitalOut heartbeatLED(LED4); |
JoeMiller | 0:197cfa57c3c7 | 40 | Serial pc(SERIAL_TX, SERIAL_RX); |
JoeMiller | 1:0d88cfafe20e | 41 | DigitalOut MOSFET(PA_4); // Connected to Gate of MOSFET |
JoeMiller | 0:197cfa57c3c7 | 42 | AnalogIn VBin(PA_1); // To measure battery Voltage |
JoeMiller | 0:197cfa57c3c7 | 43 | DigitalOut LED(D4); // Activity indicator |
ruairilong | 2:ddc96642fcdb | 44 | DigitalIn enable(PA_5); |
ruairilong | 2:ddc96642fcdb | 45 | |
JoeMiller | 1:0d88cfafe20e | 46 | #define WIRE_LEAD_RES (0.23F) |
JoeMiller | 0:197cfa57c3c7 | 47 | // BOARD SPECIFIC CALIBRATION CONSTANTS |
JoeMiller | 0:197cfa57c3c7 | 48 | #define BOARDNUMBER 1 |
JoeMiller | 0:197cfa57c3c7 | 49 | #if BOARDNUMBER == 1 |
ruairilong | 2:ddc96642fcdb | 50 | #define V_SCALE 3.735935F |
JoeMiller | 0:197cfa57c3c7 | 51 | #elif BOARDNUMBER == 2 |
ruairilong | 2:ddc96642fcdb | 52 | #define V_SCALE 3.3F |
JoeMiller | 0:197cfa57c3c7 | 53 | #elif BOARDNUMBER == 3 |
ruairilong | 2:ddc96642fcdb | 54 | #define V_SCALE 3.3F |
ruairilong | 2:ddc96642fcdb | 55 | #else |
ruairilong | 2:ddc96642fcdb | 56 | #define V_SCALE 3.3F |
JoeMiller | 0:197cfa57c3c7 | 57 | #endif |
JoeMiller | 0:197cfa57c3c7 | 58 | // Parameters |
JoeMiller | 1:0d88cfafe20e | 59 | #define PULSEWIDTH 1.0f // seconds |
JoeMiller | 0:197cfa57c3c7 | 60 | char serial_inchar,waiting; |
JoeMiller | 0:197cfa57c3c7 | 61 | |
JoeMiller | 0:197cfa57c3c7 | 62 | void OnSerial(void) // serial port interrupt used in calibration mode |
JoeMiller | 0:197cfa57c3c7 | 63 | { |
JoeMiller | 0:197cfa57c3c7 | 64 | serial_inchar = pc.getc(); |
JoeMiller | 0:197cfa57c3c7 | 65 | waiting = 0; |
JoeMiller | 0:197cfa57c3c7 | 66 | } |
JoeMiller | 0:197cfa57c3c7 | 67 | |
ruairilong | 2:ddc96642fcdb | 68 | int main() |
ruairilong | 2:ddc96642fcdb | 69 | { |
ruairilong | 2:ddc96642fcdb | 70 | |
ruairilong | 2:ddc96642fcdb | 71 | float OpenVoltage, LoadVoltage, Esr; |
ruairilong | 2:ddc96642fcdb | 72 | pc.baud(115200); |
JoeMiller | 1:0d88cfafe20e | 73 | printf("Battery ESR Tester\n\r"); |
JoeMiller | 0:197cfa57c3c7 | 74 | pc.attach(&OnSerial); |
JoeMiller | 1:0d88cfafe20e | 75 | MOSFET = 0; |
JoeMiller | 0:197cfa57c3c7 | 76 | LED = 0; |
ruairilong | 4:f7cc009aed74 | 77 | |
JoeMiller | 0:197cfa57c3c7 | 78 | #if 1 // 0 = Calibration Mode, 1= Discharge test mode |
JoeMiller | 0:197cfa57c3c7 | 79 | |
JoeMiller | 1:0d88cfafe20e | 80 | printf("Test Mode\n\r"); |
ruairilong | 2:ddc96642fcdb | 81 | |
JoeMiller | 1:0d88cfafe20e | 82 | while(1) { |
ruairilong | 2:ddc96642fcdb | 83 | |
JoeMiller | 1:0d88cfafe20e | 84 | waiting = 1; |
ruairilong | 2:ddc96642fcdb | 85 | |
ruairilong | 2:ddc96642fcdb | 86 | while(enable) { |
JoeMiller | 1:0d88cfafe20e | 87 | wait(0.1); |
ruairilong | 2:ddc96642fcdb | 88 | } |
ruairilong | 2:ddc96642fcdb | 89 | mbedKeyBoard.printf("Test running: "); |
ruairilong | 2:ddc96642fcdb | 90 | printf("Test running: "); |
ruairilong | 2:ddc96642fcdb | 91 | wait(3); //debounce and force a minimum time between tests to ensure the battery has time to recovery. |
JoeMiller | 1:0d88cfafe20e | 92 | OpenVoltage = VBin.read()* V_SCALE; |
JoeMiller | 1:0d88cfafe20e | 93 | LED = 1; |
JoeMiller | 1:0d88cfafe20e | 94 | MOSFET = 1; |
JoeMiller | 1:0d88cfafe20e | 95 | wait(PULSEWIDTH); |
JoeMiller | 1:0d88cfafe20e | 96 | LoadVoltage = VBin.read()* V_SCALE; |
JoeMiller | 0:197cfa57c3c7 | 97 | LED = 0; |
JoeMiller | 1:0d88cfafe20e | 98 | MOSFET = 0; |
ruairilong | 2:ddc96642fcdb | 99 | Esr = (OpenVoltage-LoadVoltage)/(LoadVoltage / 3.0f)- WIRE_LEAD_RES; |
ruairilong | 2:ddc96642fcdb | 100 | mbedKeyBoard.printf("Open Voltage = %1.3f, Loaded Voltage = %1.3f, ESR = %1.3f\n\r", |
ruairilong | 2:ddc96642fcdb | 101 | OpenVoltage, LoadVoltage, Esr); |
JoeMiller | 1:0d88cfafe20e | 102 | printf("Open Voltage = %1.3f, Loaded Voltage = %1.3f, ESR = %1.3f\n\r", |
ruairilong | 3:a170b248ead8 | 103 | OpenVoltage, LoadVoltage, Esr); |
JoeMiller | 1:0d88cfafe20e | 104 | } |
JoeMiller | 0:197cfa57c3c7 | 105 | |
JoeMiller | 0:197cfa57c3c7 | 106 | #else |
JoeMiller | 1:0d88cfafe20e | 107 | |
JoeMiller | 0:197cfa57c3c7 | 108 | //Perform Board Calibration |
JoeMiller | 0:197cfa57c3c7 | 109 | printf("\n\rCalibration Mode\n\r"); |
JoeMiller | 0:197cfa57c3c7 | 110 | waiting = 1; |
ruairilong | 3:a170b248ead8 | 111 | |
JoeMiller | 0:197cfa57c3c7 | 112 | while (pc.readable()) { // flush buffer |
ruairilong | 2:ddc96642fcdb | 113 | serial_inchar = pc.getc(); |
ruairilong | 2:ddc96642fcdb | 114 | } |
ruairilong | 3:a170b248ead8 | 115 | |
JoeMiller | 1:0d88cfafe20e | 116 | printf("Set Vin to 3.600V then [press any key]\n\r"); |
ruairilong | 3:a170b248ead8 | 117 | |
ruairilong | 2:ddc96642fcdb | 118 | while(waiting == 1) { |
JoeMiller | 0:197cfa57c3c7 | 119 | wait(0.05); |
ruairilong | 2:ddc96642fcdb | 120 | } |
ruairilong | 3:a170b248ead8 | 121 | |
JoeMiller | 1:0d88cfafe20e | 122 | printf("Reading...\n\r"); |
JoeMiller | 0:197cfa57c3c7 | 123 | wait(0.5); |
JoeMiller | 1:0d88cfafe20e | 124 | OpenVoltage = VBin.read(); |
JoeMiller | 1:0d88cfafe20e | 125 | printf("Cut/paste this calibration into the calibration section...\n\r\n\r"); |
JoeMiller | 1:0d88cfafe20e | 126 | printf("#define V_SCALE %fF\n\r",3.6/OpenVoltage); |
ruairilong | 2:ddc96642fcdb | 127 | |
ruairilong | 3:a170b248ead8 | 128 | #endif |
ruairilong | 2:ddc96642fcdb | 129 | |
JoeMiller | 0:197cfa57c3c7 | 130 | } |