This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

BQ27742-g1 floating point converstion

Other Parts Discussed in Thread: BQ27742-G1, BQSTUDIO

I am trying to calibrate a battery pack that uses a BQ27742-g1 gas gauge IC.  In doing the current calibration in order to calculate the ccGain and ccDelta the numbers must be converted from floating point to a special type of floating point understood by the gas gauge.  I followed their flow chart in the sluuax0b.pdf document (Section A.11) and wrote some c code to convert the number.  My problem is that I am questioning my results and have no way to test the answer I receive to make sure it is correct.  The byte0, byte1, and byte2 numbers I am getting end up being negative numbers, which I don't think is correct.  Does anyone have any code that implements their algorithm that they would be willing to share?  Or some example conversions of floating point numbers that I can check to make sure my algorithm is working?

  • Also wondering which byte of the resulting byte array goes where in the flash. Does rawData[0] go to address 0 (ccGain) and 4 (ccDelta), or does rawData[3] go to address 0 and 4.
  • Hello Bruce,

    Would you be able to provide the code for us to look at? Also giving the input/output data would be beneficial. I believe sample code is in the works, but do not know when it will be ready to post to the web.

    The flow chart is starting with the values you obtained for CC Gain and CC Delta. Section A.8 describes how CC Gain and CC Delta valuse are obtained. Then, Section A.11 shows how to take the floating point CC Gain and CC Delta values and convert them into raw hex data.

    Once you have the raw hex data you would store the CC Gain (4 bytes) and CC Delta (4 bytes) in the corresponding memory location (please see Data Flash Summary section).

    I hope this helps.
  • Below is the code I wrote that I tried to follow the diagram with. I actually found from the sluuax0b document 2 sections of flash where they store a fp number (the ccGain and ccDelta sections) and read out the default values and compared them to my function when using those same default values. So at least for those 2 data points I match. The data points are the following:

    CC Gain = 0.9536 = 0x80741F21
    CC Delta = 11.19e5 = 0x950898C0

    Also, do you have an algorithm to go from the gas gauge's floating point representation back to a floating point number? Would be another way for me to check my algorithm.

    gasGaugeConvFP(float input, unsigned char* output)
    {
    int exp = 0;

    float tmpVal = 0;
    if (input < 0)
    {
    tmpVal = -input * (1 + powf(2.0f, -25.0f));
    }
    else
    {
    tmpVal = input * (1 + powf(2.0f, -25.0f));
    }

    if (tmpVal < 0.5f)
    {
    while (tmpVal < 0.5)
    {
    tmpVal *= 2.0f;
    exp -= 1;
    }
    }
    else if (tmpVal > 1.0f)
    {
    while (tmpVal > 1.0)
    {
    tmpVal /= 2.0f;
    exp += 1;
    }
    }

    if (exp > 127)
    {
    exp = 127;
    }
    else if (exp < -128)
    {
    exp = -128;
    }

    tmpVal = powf(2.0, 8.0f - (float)exp) * input - 128.0;

    int byte2 = (int)tmpVal;

    tmpVal = powf(2.0, 8.0) * (tmpVal - (float)byte2);

    int byte1 = (int)tmpVal;

    tmpVal = powf(2.0, 8.0) * (tmpVal - (float)byte1);

    int byte0 = (int)tmpVal;

    if (input < 0)
    {
    byte2 |= 0x80;
    }

    output[0] = (unsigned char)(exp + 128);
    output[1] = (unsigned char)byte2;
    output[2] = (unsigned char)byte1;
    output[3] = (unsigned char)byte0;
    }
  • Hello Bruce,


    Your values look correct to me.  The only difference I see in the code is that the if-statement checking if tmpVal is > 1.0f should have greater than or equal as the test condition and the same goes for the while condition in that if statement.

    if(tmpVal >= 1.0f)

    {

    while(tmpVal >= 1.0f)

    {

    ...

    I am not aware of an algorithm to go from the raw data to the floating point information.  But, you can use the "Advanced Comm" plug-in in bqStudio in order to read back the raw data.  That lets you check what you come up with against what our tools produce.

    I hope this helps.