First working, tested and calibrated unit

Dependencies:   mbed USBDevice

Committer:
ruairilong
Date:
Mon Mar 27 21:00:03 2017 +0000
Revision:
3:a170b248ead8
Parent:
2:ddc96642fcdb
Child:
4:f7cc009aed74
Format code and remove unused functions.

Who changed what in which revision?

UserRevisionLine numberNew 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
ruairilong 2:ddc96642fcdb 36 // Variables for Heartbeat and Status monitoring
ruairilong 2:ddc96642fcdb 37 PwmOut myled1(LED1);
ruairilong 2:ddc96642fcdb 38 PwmOut myled2(LED2);
ruairilong 2:ddc96642fcdb 39 PwmOut myled3(LED3);
ruairilong 2:ddc96642fcdb 40 DigitalOut heartbeatLED(LED4);
JoeMiller 0:197cfa57c3c7 41 Serial pc(SERIAL_TX, SERIAL_RX);
JoeMiller 1:0d88cfafe20e 42 DigitalOut MOSFET(PA_4); // Connected to Gate of MOSFET
JoeMiller 0:197cfa57c3c7 43 AnalogIn VBin(PA_1); // To measure battery Voltage
JoeMiller 0:197cfa57c3c7 44 DigitalOut LED(D4); // Activity indicator
ruairilong 2:ddc96642fcdb 45 DigitalIn enable(PA_5);
ruairilong 2:ddc96642fcdb 46
JoeMiller 1:0d88cfafe20e 47 #define WIRE_LEAD_RES (0.23F)
JoeMiller 0:197cfa57c3c7 48 // BOARD SPECIFIC CALIBRATION CONSTANTS
JoeMiller 0:197cfa57c3c7 49 #define BOARDNUMBER 1
JoeMiller 0:197cfa57c3c7 50 #if BOARDNUMBER == 1
ruairilong 2:ddc96642fcdb 51 #define V_SCALE 3.735935F
JoeMiller 0:197cfa57c3c7 52 #elif BOARDNUMBER == 2
ruairilong 2:ddc96642fcdb 53 #define V_SCALE 3.3F
JoeMiller 0:197cfa57c3c7 54 #elif BOARDNUMBER == 3
ruairilong 2:ddc96642fcdb 55 #define V_SCALE 3.3F
ruairilong 2:ddc96642fcdb 56 #else
ruairilong 2:ddc96642fcdb 57 #define V_SCALE 3.3F
JoeMiller 0:197cfa57c3c7 58 #endif
JoeMiller 0:197cfa57c3c7 59
ruairilong 2:ddc96642fcdb 60 Ticker heartbeat;
ruairilong 2:ddc96642fcdb 61
JoeMiller 0:197cfa57c3c7 62 // Parameters
JoeMiller 1:0d88cfafe20e 63 #define PULSEWIDTH 1.0f // seconds
JoeMiller 0:197cfa57c3c7 64 char serial_inchar,waiting;
JoeMiller 0:197cfa57c3c7 65
JoeMiller 0:197cfa57c3c7 66 void OnSerial(void) // serial port interrupt used in calibration mode
JoeMiller 0:197cfa57c3c7 67 {
JoeMiller 0:197cfa57c3c7 68 serial_inchar = pc.getc();
JoeMiller 0:197cfa57c3c7 69 waiting = 0;
JoeMiller 0:197cfa57c3c7 70 }
JoeMiller 0:197cfa57c3c7 71
ruairilong 2:ddc96642fcdb 72
ruairilong 2:ddc96642fcdb 73 int main()
ruairilong 2:ddc96642fcdb 74 {
ruairilong 2:ddc96642fcdb 75
ruairilong 2:ddc96642fcdb 76 float OpenVoltage, LoadVoltage, Esr;
ruairilong 2:ddc96642fcdb 77
ruairilong 2:ddc96642fcdb 78 pc.baud(115200);
JoeMiller 1:0d88cfafe20e 79 printf("Battery ESR Tester\n\r");
JoeMiller 0:197cfa57c3c7 80 pc.attach(&OnSerial);
ruairilong 2:ddc96642fcdb 81
JoeMiller 1:0d88cfafe20e 82 MOSFET = 0;
JoeMiller 0:197cfa57c3c7 83 LED = 0;
JoeMiller 0:197cfa57c3c7 84
JoeMiller 0:197cfa57c3c7 85
JoeMiller 0:197cfa57c3c7 86 #if 1 // 0 = Calibration Mode, 1= Discharge test mode
JoeMiller 0:197cfa57c3c7 87
JoeMiller 1:0d88cfafe20e 88 printf("Test Mode\n\r");
ruairilong 2:ddc96642fcdb 89
JoeMiller 1:0d88cfafe20e 90 while(1) {
ruairilong 2:ddc96642fcdb 91
JoeMiller 1:0d88cfafe20e 92 waiting = 1;
ruairilong 2:ddc96642fcdb 93
ruairilong 2:ddc96642fcdb 94 while(enable) {
JoeMiller 1:0d88cfafe20e 95 wait(0.1);
ruairilong 2:ddc96642fcdb 96 }
ruairilong 2:ddc96642fcdb 97 mbedKeyBoard.printf("Test running: ");
ruairilong 2:ddc96642fcdb 98 printf("Test running: ");
ruairilong 2:ddc96642fcdb 99 wait(3); //debounce and force a minimum time between tests to ensure the battery has time to recovery.
JoeMiller 1:0d88cfafe20e 100 OpenVoltage = VBin.read()* V_SCALE;
JoeMiller 1:0d88cfafe20e 101 LED = 1;
JoeMiller 1:0d88cfafe20e 102 MOSFET = 1;
JoeMiller 1:0d88cfafe20e 103 wait(PULSEWIDTH);
JoeMiller 1:0d88cfafe20e 104 LoadVoltage = VBin.read()* V_SCALE;
JoeMiller 0:197cfa57c3c7 105 LED = 0;
JoeMiller 1:0d88cfafe20e 106 MOSFET = 0;
ruairilong 2:ddc96642fcdb 107 Esr = (OpenVoltage-LoadVoltage)/(LoadVoltage / 3.0f)- WIRE_LEAD_RES;
ruairilong 2:ddc96642fcdb 108 mbedKeyBoard.printf("Open Voltage = %1.3f, Loaded Voltage = %1.3f, ESR = %1.3f\n\r",
ruairilong 2:ddc96642fcdb 109 OpenVoltage, LoadVoltage, Esr);
JoeMiller 1:0d88cfafe20e 110 printf("Open Voltage = %1.3f, Loaded Voltage = %1.3f, ESR = %1.3f\n\r",
ruairilong 3:a170b248ead8 111 OpenVoltage, LoadVoltage, Esr);
JoeMiller 1:0d88cfafe20e 112 }
JoeMiller 0:197cfa57c3c7 113
JoeMiller 0:197cfa57c3c7 114 #else
JoeMiller 1:0d88cfafe20e 115
JoeMiller 0:197cfa57c3c7 116 //Perform Board Calibration
JoeMiller 0:197cfa57c3c7 117 printf("\n\rCalibration Mode\n\r");
JoeMiller 0:197cfa57c3c7 118 waiting = 1;
ruairilong 3:a170b248ead8 119
JoeMiller 0:197cfa57c3c7 120 while (pc.readable()) { // flush buffer
ruairilong 2:ddc96642fcdb 121 serial_inchar = pc.getc();
ruairilong 2:ddc96642fcdb 122 }
ruairilong 3:a170b248ead8 123
JoeMiller 1:0d88cfafe20e 124 printf("Set Vin to 3.600V then [press any key]\n\r");
ruairilong 3:a170b248ead8 125
ruairilong 2:ddc96642fcdb 126 while(waiting == 1) {
JoeMiller 0:197cfa57c3c7 127 wait(0.05);
ruairilong 2:ddc96642fcdb 128 }
ruairilong 3:a170b248ead8 129
JoeMiller 1:0d88cfafe20e 130 printf("Reading...\n\r");
JoeMiller 0:197cfa57c3c7 131 wait(0.5);
JoeMiller 1:0d88cfafe20e 132 OpenVoltage = VBin.read();
JoeMiller 1:0d88cfafe20e 133 printf("Cut/paste this calibration into the calibration section...\n\r\n\r");
JoeMiller 1:0d88cfafe20e 134 printf("#define V_SCALE %fF\n\r",3.6/OpenVoltage);
ruairilong 2:ddc96642fcdb 135
ruairilong 3:a170b248ead8 136 #endif
ruairilong 2:ddc96642fcdb 137
JoeMiller 0:197cfa57c3c7 138 }