cancel
Showing results for 
Search instead for 
Did you mean: 

B-G431-ESC1 Battery Measurement source code error in conversion factor

SRedd.5
Senior III

I am referring to the B-G431-ESC1 source code and i feel there is an error in the battery voltage measurement source code. A conversion factor is used to convert from the ADC voltage to the battery voltage. It is initialized as follows

RDivider_Handle_t BusVoltageSensor_M1 =

{

 ._Super        =

 {

  .SensorType     = REAL_SENSOR,

  .ConversionFactor  = (uint16_t)(ADC_REFERENCE_VOLTAGE / VBUS_PARTITIONING_FACTOR),

 },

The voltage is read in u16Volt format when reading the ADC register value , I assume the adc driver will only give max value of 12Bit (4096) for 3.3V, but where is this conversion added to convert to Battery voltage. Please advise.

1 ACCEPTED SOLUTION

Accepted Solutions
cedric H
ST Employee

Hello @Community member​,

The ADC is configured to be left aligned, so the maximum value is actually 0xFFF0 = 65 520

The conversion you are looking for is done in function function VBS_GetAvBusVoltage_V from bus_voltage_sensor.c file.

/**
  * @brief  It return latest averaged Vbus measurement expressed in Volt format
  * @param  pHandle related Handle of BusVoltageSensor_Handle_t
  * @retval uint16_t Latest averaged Vbus measurement in Volt format
  */
__weak uint16_t VBS_GetAvBusVoltage_V(const BusVoltageSensor_Handle_t *pHandle)
{
  uint32_t temp;
#ifdef NULL_PTR_CHECK_BUS_VOLT
  if (MC_NULL == pHandle)
  {
    temp = 0U;
  }
  else
  {
#endif
    temp = (uint32_t)(pHandle->AvBusVoltage_d);
    temp *= pHandle->ConversionFactor;
    temp /= 65536U;
#ifdef NULL_PTR_CHECK_BUS_VOLT
  }
#endif
  return ((uint16_t)temp);
}

View solution in original post

3 REPLIES 3
cedric H
ST Employee

Hello @Community member​,

The ADC is configured to be left aligned, so the maximum value is actually 0xFFF0 = 65 520

The conversion you are looking for is done in function function VBS_GetAvBusVoltage_V from bus_voltage_sensor.c file.

/**
  * @brief  It return latest averaged Vbus measurement expressed in Volt format
  * @param  pHandle related Handle of BusVoltageSensor_Handle_t
  * @retval uint16_t Latest averaged Vbus measurement in Volt format
  */
__weak uint16_t VBS_GetAvBusVoltage_V(const BusVoltageSensor_Handle_t *pHandle)
{
  uint32_t temp;
#ifdef NULL_PTR_CHECK_BUS_VOLT
  if (MC_NULL == pHandle)
  {
    temp = 0U;
  }
  else
  {
#endif
    temp = (uint32_t)(pHandle->AvBusVoltage_d);
    temp *= pHandle->ConversionFactor;
    temp /= 65536U;
#ifdef NULL_PTR_CHECK_BUS_VOLT
  }
#endif
  return ((uint16_t)temp);
}

Thank you very much got response from ST employee, which will help to clarify our doubts and move forward more confidently.

0693W00000aIge3QAC.png 

As per the circuit, the maximum Battery voltage = 3.3/0.096 = 34.375V appx.

So if i receive the ADC count of 65520=0xFFF0, the max voltage is 34.375V

and if i receive a adc count of 32768 = 0x8000, the voltage is 17.18V. Am i correct? Please advise.

cedric H
ST Employee

Hello,

I do not see any issue with your computation.

Please note that the Pilot reads volt in unit16_t so you will have only integer values. in your case you should read 17 V.

Hope it helps.

Cedric