PNI / Mbed 2 deprecated BatteryESRTester

Dependencies:   mbed USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed USBJoystick Library Demo
00002  * Copyright (c) 2012, v01:  Initial version, WH,
00003  *                           Modified USBMouse code ARM Limited.
00004  *                           (c) 2010-2011 mbed.org, MIT License
00005  *               2016, v02:  Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"), to deal
00009  * in the Software without restriction, inclumosig without limitation the rights
00010  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in
00015  * all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018  * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023  * THE SOFTWARE.
00024  */
00025 
00026 // Note: you must connect the usb cable to your computer before the program will proceed
00027 
00028 // USB parts from: https://developer.mbed.org/forum/helloworld/topic/3496/
00029 
00030 #include "mbed.h"
00031 #include "USBKeyboard.h"
00032 
00033 //USBMouse mouse;
00034 USBKeyboard mbedKeyBoard;
00035 // Variables for Heartbeat and Status monitoring
00036 PwmOut myled1(LED1);
00037 PwmOut myled2(LED2);
00038 PwmOut myled3(LED3);
00039 DigitalOut heartbeatLED(LED4);
00040 Serial pc(SERIAL_TX, SERIAL_RX);
00041 DigitalOut MOSFET(PA_4);      // Connected to Gate of MOSFET
00042 AnalogIn   VBin(PA_1);        // To measure battery Voltage
00043 DigitalOut LED(D4);           // Activity indicator
00044 DigitalIn enable(PA_5);
00045 
00046 #define WIRE_LEAD_RES (0.23F)
00047 // BOARD SPECIFIC CALIBRATION CONSTANTS
00048 #define BOARDNUMBER 1
00049 #if BOARDNUMBER == 1
00050 #define V_SCALE  3.735935F
00051 #elif BOARDNUMBER == 2
00052 #define V_SCALE 3.3F
00053 #elif BOARDNUMBER == 3
00054 #define V_SCALE 3.3F
00055 #else
00056 #define V_SCALE 3.3F
00057 #endif
00058 // Parameters
00059 #define PULSEWIDTH  1.0f  // seconds
00060 char    serial_inchar,waiting;
00061 
00062 void OnSerial(void) // serial port interrupt used in calibration mode
00063 {
00064     serial_inchar = pc.getc();
00065     waiting = 0;
00066 }
00067 
00068 int main()
00069 {
00070 
00071     float OpenVoltage, LoadVoltage, Esr;
00072     pc.baud(115200);
00073     printf("Battery ESR Tester\n\r");
00074     pc.attach(&OnSerial);
00075     MOSFET = 0;
00076     LED = 0;
00077     
00078 #if 1  // 0 = Calibration Mode, 1= Discharge test mode  
00079 
00080     printf("Test Mode\n\r");
00081 
00082     while(1) {
00083 
00084         waiting = 1;
00085 
00086         while(enable) {
00087             wait(0.1);
00088         }
00089         mbedKeyBoard.printf("Test running: ");
00090         printf("Test running: ");
00091         wait(3); //debounce and force a minimum time between tests to ensure the battery has time to recovery.
00092         OpenVoltage = VBin.read()* V_SCALE;
00093         LED = 1;
00094         MOSFET = 1;
00095         wait(PULSEWIDTH);
00096         LoadVoltage = VBin.read()* V_SCALE;
00097         LED = 0;
00098         MOSFET = 0;
00099         Esr = (OpenVoltage-LoadVoltage)/(LoadVoltage / 3.0f)- WIRE_LEAD_RES;
00100         mbedKeyBoard.printf("Open Voltage = %1.3f, Loaded Voltage = %1.3f, ESR = %1.3f\n\r",
00101                             OpenVoltage, LoadVoltage, Esr);
00102         printf("Open Voltage = %1.3f, Loaded Voltage = %1.3f, ESR = %1.3f\n\r",
00103                OpenVoltage, LoadVoltage, Esr);
00104     }
00105 
00106 #else
00107 
00108     //Perform Board Calibration
00109     printf("\n\rCalibration Mode\n\r");
00110     waiting = 1;
00111 
00112     while (pc.readable()) { // flush buffer
00113         serial_inchar = pc.getc();
00114     }
00115 
00116     printf("Set Vin to 3.600V then [press any key]\n\r");
00117 
00118     while(waiting == 1) {
00119         wait(0.05);
00120     }
00121 
00122     printf("Reading...\n\r");
00123     wait(0.5);
00124     OpenVoltage = VBin.read();
00125     printf("Cut/paste this calibration into the calibration section...\n\r\n\r");
00126     printf("#define V_SCALE %fF\n\r",3.6/OpenVoltage);
00127 
00128 #endif
00129 
00130 }