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.

BQ27542EVM: 电流校正ccGain以及浮点数转换不正确。

Part Number: BQ27542EVM

我有一个bq27542器件,我用Battery Management Studio校正后,电流是准的-1000mA,读取DF数据为:

CC Offset=0xFA89;

Board Offset=0xAF;

校正后的 CC Gain=0x805851A8(5.573mohm)

校正后的 CC Delta=0x94761F72(5.550mohm)

按照公式:4.768/x=5.573,则X=0.8555535618158981。

我怎么也找不住浮点数:0.8555535618158981与0x805851A8之间的关系。

那么,我现在按照官方给出的 floating2Byte函数进行转换:获得0x805B058E,这与读取到的DF数据(0x805851A8)看上去没有任何关联。

 

接下来,我利用自己写的代码进行校正。

外部加载-1000mA电流时:

获得的变量值如下:

avgRawData=1091

ccOffset=64137

boardOffset=175

带入公式:double ccGain = (double)(knownCurrent / (double)(avgRawData - (double)((ccOffset +(int) boardOffset) / 16)));

ccGain =0.34129692832764508

ccDelta=407182.93515358365

通过floating2Byte转换后得到4byte为:0x7F2EBE78

写入后:

然而,实际上的CC Gain(13.799)和CC Delta(5993346.187)完全是错误的,当然了,电流肯定也是错误的。

研究了好几天,始终研究不透:

1、要么官方给的CC Gain获取公式不对。

2、要么floating2Byte转换错误。

现在贴出floating2Byte的C#版本代码,希望得到官方的帮助与其他大神的指点。

void floating2Byte(double value, short[] rawData)
        {
            double CC_value = value; //Read CC_gain or CC_delta value from the gauge.
            int exp = 0; //Initialize the exponential for the floating to byte conversion
            double val = CC_value;
            double mod_val;
            if (val < 0)
            {
                mod_val = val * -1;
            }
            else
            {
                mod_val = val;
            }
            double tmpVal = mod_val;
            tmpVal *= (1 +Math.Pow(2, -25));
            if (tmpVal < 0.5)
            {
                while (tmpVal<0.5)
                {
                    tmpVal *= 2;
                    exp--;
                }
            }
            else if (tmpVal<=1.0)
            {
                while (tmpVal >= 1.0)
                {
                    tmpVal /= 2;
                    exp++;
                }
            }
            if (exp > 127)
            {
                exp = 127;
            }
            else if (exp < -128)
            {
                exp = -128;
            }
            tmpVal = Math.Pow(2, 8 - exp) * mod_val - 128;
            byte byte2 = (byte)Math.Floor(tmpVal);

            tmpVal = Math.Pow(2, 8) * (double)(tmpVal - (double)byte2);
            byte byte1 = (byte)Math.Floor(tmpVal);

            tmpVal = Math.Pow(2, 8) * (double)(tmpVal - (double)byte1);
            byte byte0 = (byte)Math.Floor(tmpVal);

            if (val < 0)
            {
                byte2 |=  0x80;
            }
            rawData[0] = (short)(exp + 128);
            rawData[1] = (short)byte2;
            rawData[2] = (short)byte1;
            rawData[3] = (short)byte0;
        }