Lab IJ32-30 State-of-Charge Meter, Phase 1        S215  09 1116
15 Points
                        OEES 215
Back to Main Page
Example program
The following program accepts simulated battery voltages from the pot, and displays the battery's percent state of charge when you press the button. When the button is not pressed, the simulated voltages are shown. The equation for calculating percent state of charge was derived from data obtained by measuring actual voltages as a five amp-hour battery was discharging.

//******************************************************************************
// IJ32-10 State-of-Charge-Meter, Phase 1 --EXAMPLE            S215  09 1116
// *****************************************************************************
#include "s215-header11.h"
#pragma code
void main (void)
{
unsigned short    adc;        //Output of ADC
float             percent,    //Percent charge remaining.       
                  v;          //Battery voltage.
InitPorts();                  //Initialize I/O ports and ADC (set it
                              //  to ADC channel 0).
while (1==1)
{
//|||||||||||||||||||||||||| MAIN LOOP ||||||||||||||||||||||||||||||||
adc = Runavg0(ADC10bit());

v = adc * (13.-11.)/1023. + 11.;  /* Make v range from 11 to 13 as the pot is
turned from fully CCW to fully CW. The following general form for a linear
equation was used to get the above equation:
y = (y2-y1)*x/(x2-x1) + b  */

percent = 37.651*v*v - 807.59*v + 4308.5;
    //Based on equation from IK16-27 spreadsheet.

if (PB==1) DispFP(percent, 0);
else       DispFP(v,2);

//|||||||||||||||||||||||| END OF MAIN LOOP ||||||||||||||||||||||||||       
}
}

Downloads:  ij32-10-soc-meter-phase1-EXAMPLE.c

Modify the Example
Create a function called Soc, and put the equation to calculate percent state of charge (percent = 37.651*v*v - 807.59*v + 4308.5;) into this function. When the main program sends the voltage to this function, it should return the percent state of charge.

Also, the Soc function should check the voltage to be sure that it's within the range for which the equation is valid, which is 11.50 volts to 12.51 volts. If the voltage is larger than 12.51 volts, have the function return 100. If the voltage is smaller than 11.50 volts, have the function return 0. In other words, for voltages that will give a state of charge greater than 100%, make the state of charge 100%. And, for voltages that will give a state of charge less than 0, make the state of charge 0%.