Skip to main content
SRedd.5
Senior III
March 8, 2023
Solved

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

  • March 8, 2023
  • 2 replies
  • 1601 views

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.

Best answer by cedric H

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);
}

2 replies

cedric H
cedric HBest answer
Technical Moderator
March 9, 2023

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);
}

SRedd.5
SRedd.5Author
Senior III
March 10, 2023

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
Technical Moderator
March 10, 2023

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