First working, tested and calibrated unit

Dependencies:   mbed USBDevice

main.cpp

Committer:
JoeMiller
Date:
2017-03-22
Revision:
1:0d88cfafe20e
Parent:
0:197cfa57c3c7
Child:
2:ddc96642fcdb

File content as of revision 1:0d88cfafe20e:

/* BATTERY ESR MEASUREMENT

Note: The design intent was to calibrate each board rather than designing 
    and assembling precision analog circuits for each shield-board.
      Use compiler directive switch in this firmware to perform routine to 
    obtain calibration coeficients for each board. Place these obtained cal 
    coefficients in the CALIBRATION CONSTANTS section and then recompile for test mode
    
    See ENG-1493 for more details and results

**** Be sure to set the approriate BOARDNUMBER before compiling ***********

*/


#include "mbed.h"

Serial pc(SERIAL_TX, SERIAL_RX);
DigitalOut MOSFET(PA_4);      // Connected to Gate of MOSFET
AnalogIn   VBin(PA_1);        // To measure battery Voltage
DigitalOut LED(D4);           // Activity indicator

#define WIRE_LEAD_RES (0.23F)

// BOARD SPECIFIC CALIBRATION CONSTANTS
#define BOARDNUMBER 1

#if BOARDNUMBER == 1
#define V_SCALE  3.735935F 
 
    
#elif BOARDNUMBER == 2
#define V_SCALE 3.3F                                                       

#elif BOARDNUMBER == 3
#define V_SCALE 3.3F                                                       

#else 
#define V_SCALE 3.3F                                                       

#endif



// Parameters
#define PULSEWIDTH  1.0f  // seconds
char    serial_inchar,waiting;

void OnSerial(void) // serial port interrupt used in calibration mode
{
    serial_inchar = pc.getc();
    waiting = 0;
}

 
int main() {
    float OpenVoltage,LoadVoltage;

    pc.baud(115200);    
    printf("Battery ESR Tester\n\r");
    pc.attach(&OnSerial);
    
    MOSFET = 0;
    LED = 0;


#if 1  // 0 = Calibration Mode, 1= Discharge test mode  

    printf("Test Mode\n\r");
    while(1) {
        waiting = 1;
        while(waiting){
            wait(0.1);
        }    
        OpenVoltage = VBin.read()* V_SCALE;
        LED = 1;
        MOSFET = 1;
        wait(PULSEWIDTH);
        LoadVoltage = VBin.read()* V_SCALE;
        LED = 0;
        MOSFET = 0;
        
        printf("Open Voltage = %1.3f, Loaded Voltage = %1.3f, ESR = %1.3f\n\r",
            OpenVoltage,LoadVoltage,(OpenVoltage-LoadVoltage)/(LoadVoltage/3.0f)-WIRE_LEAD_RES);
    }


    
#else

    //Perform Board Calibration
    printf("\n\rCalibration Mode\n\r");
    waiting = 1;
    while (pc.readable()) { // flush buffer
            serial_inchar = pc.getc();
        }    
    printf("Set Vin to 3.600V then [press any key]\n\r");
    while(waiting == 1){
        wait(0.05);
    }    
    printf("Reading...\n\r");
    wait(0.5);
    OpenVoltage = VBin.read();

    printf("Cut/paste this calibration into the calibration section...\n\r\n\r");
    printf("#define V_SCALE %fF\n\r",3.6/OpenVoltage);
    
    
#endif  
 
}