cancel
Showing results for 
Search instead for 
Did you mean: 

Vbat and temperature monitoring problem STM32F303CB

Serhii Shevchuk
Associate
Posted on June 21, 2017 at 12:55

Hello.

There was a problem with the operation of the ADC with built-in thermistor and VBAT voltage.

The value 4095 is always read.

Here is an example of code for reading voltage VBAT:

  /* Configure the ADC clock */

  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);

 

  /* Enable ADC1 clock */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);

        

  /* Setup SysTick Timer for 1 �sec interrupts  */

  if (SysTick_Config(SystemCoreClock / 1000000))

  {

    /* Capture error */

    while (1)

    {}

  }

      

  ADC_StructInit(&ADC_InitStructure);

  /* Calibration procedure */

  ADC_VoltageRegulatorCmd(ADC1, ENABLE);

 

  /* Insert delay equal to 10 �s */

  Delay(10);

 

  ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);

  ADC_StartCalibration(ADC1);

 

  while(ADC_GetCalibrationStatus(ADC1) != RESET );

  calibration_value = ADC_GetCalibrationValue(ADC1);

 

  Delay(100);

     

  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;                                                                    

  ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;                    

  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;             

  ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;                  

  ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;          

 

  ADC_CommonInit(ADC1, &ADC_CommonInitStructure);

 

  ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable;

  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;

  ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;         

  ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;

  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

  ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;   

  ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;  

  ADC_InitStructure.ADC_NbrOfRegChannel = 1;

  ADC_Init(ADC1, &ADC_InitStructure);

     

  ADC_RegularChannelConfig(ADC1, ADC_Channel_Vbat, 1, ADC_SampleTime_7Cycles5);  

  ADC_VbatCmd(ADC1, ENABLE);

 

  /* Enable ADC1 */

  ADC_Cmd(ADC1, ENABLE);

 

  /* wait for ADRDY */

  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));

    

  /* Infinite loop */

  while (1)

  {

    ADC_StartConversion(ADC1);

    

    /* Test EOC flag */

    while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);

    

    /* Get ADC1 converted data */

    ADC1ConvertedValue =ADC_GetConversionValue(ADC1);

    

    /* Compute the voltage */

    ADC1ConvertedVoltage = (ADC1ConvertedValue *3300)/0xFFF;    

    

    printf('ADC1ConvertedValue = %d\r\n',ADC1ConvertedValue);

  }

Reading the temperature sensor is similar, only the ADC channel selection differs:

ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_7Cycles5);  

  ADC_TempSensorCmd(ADC1, ENABLE);

In both cases, the ADC returns a value of 4095.

What am I doing wrong?

In the attachment - the whole project:

adc_std_01.rar -

VBAT measurement design

adc_std_02.rar -

Temperature measurement project

adc_01.rar -

A temperature measurement project assembled in CubeMX using HAL

adc_02.rar -

VBAT measurement design, assembled in CubeMX using HAL

Thanks in advance.

#temperature-sensor #stm32f303 #stm32f303-adc #vbat-monitoring
3 REPLIES 3
Daniel Kluge
Associate
Posted on July 14, 2017 at 10:46

I currently have the same problem with the internal temperature sensor of STM32F303VBT6 (using Cube 4.21.0 and FW-lib 1.8.0). Always get value 4095. Same approach on STM32F302 works fine.

Posted on July 14, 2017 at 11:24

Hello Serhii and Daniel

What is your harware configuration about  VDDA and VSSA pins ?

Is VDDA-VSSA voltage higher(according ofcourse to the RM) than the voltage try to read?

In case this voltagedifference between theese two power pins is less than the voltage you try to read  (and VSSA have some connection with ground), the value 4095 (12 bits full reading)  is normal.

Look at RM0316 page 310  about theese pins.

Posted on July 18, 2017 at 16:21

Hi Vangelis,

yes, that was the problem. On the layout for STM32F303 VREF+ was not connected. On STM32F302 VDDA and VREF+ share one µC pin. Thx!