STM32G051x ADC strange referance voltage

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
User avatar
HDKLog
Posts: 45
Joined: Thu Aug 18, 2016 12:36 am
Has thanked: 1 time
Been thanked: 3 times

STM32G051x ADC strange referance voltage

Post by HDKLog »

Hello, I was able to successfully setup a project for my board with STM32G051F8P6 but was unable to configure ADC properly. My voltage source is 3.3V (from LDO) but I get strange readings when I read ADC1. I have a Voltage Divider connected to ADC1 channel 1 with resistor 1 15k to the ground and 68k to Vs
And here is my ADC config:

Code: Select all

/*
 * ADC conversion group 2.
 * Mode:        Linear buffer, 1 channel, SW triggered.
 * Channels:    IN01.
 */
#define ADC_GRP2_NUM_CHANNELS 1
#define ADC_GRP2_BUF_DEPTH 1
adcsample_t samples2[ADC_GRP2_BUF_DEPTH * ADC_GRP2_NUM_CHANNELS];
const ADCConversionGroup adc_grp2_cfg = {
  .circular     = false,
  .num_channels = ADC_GRP2_NUM_CHANNELS,
  .end_cb       = NULL,
  .error_cb     = adcerrorcallback,
  .cfgr1        = ADC_CFGR1_CONT | ADC_CFGR1_RES_12BIT,
  .tr1          = ADC_TR_DISABLED,
  .tr2          = ADC_TR_DISABLED,
  .tr3          = ADC_TR_DISABLED,
  .awd2cr       = 0U,
  .awd3cr       = 0U,
  .smpr         = ADC_SMPR_SMP_3P5,
  .chselr       = ADC_CHSELR_CHSEL1
};


I start ADC and then start sampling by executing

Code: Select all

  /*
   * Starting PORTAB_ADC1 driver and the temperature sensor.
   */
  adcStart(&ADCD1, NULL);
 
  adcConvert(&ADCD1, &adc_grp2_cfg, samples2, ADC_GRP2_BUF_DEPTH);
 


But when I convert read value

Code: Select all

 float voltage = samples2 * 18.26f / 4095f;  
I have 2.96V instead of 3.8V. I know This MCU has an Internal Voltage reference buffer but enabling or disabling it does not affect anything

Code: Select all

VREFBUF->CSR = VREFBUF_CSR_ENVR | VREFBUF_CSR_VRS | VREFBUF_CSR_HIZ;

Do you have any idea what can be wrong here?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: STM32G051x ADC strange referance voltage

Post by Giovanni »

Hi,

ADC_SMPR_SMP_3P5

Try increasing the sample time, there is a table somewhere in the RM or DS, it should be selected depending on the input impedance.

Giovanni
User avatar
HDKLog
Posts: 45
Joined: Thu Aug 18, 2016 12:36 am
Has thanked: 1 time
Been thanked: 3 times

Re: STM32G051x ADC strange referance voltage

Post by HDKLog »

Thank you Giovanni, I had never thought about input impedance.
According to the datasheet:

Code: Select all

000: 1.5 ADC clock cycles
001: 3.5 ADC clock cycles
010: 7.5 ADC clock cycles
011: 12.5 ADC clock cycles
100: 19.5 ADC clock cycles
101: 39.5 ADC clock cycles
110: 79.5 ADC clock cycles
111: 160.5 ADC clock cycles

I chose ADC_SMPR_SMP_160P5 and it works
Post Reply