AnsweredAssumed Answered

STM32L053R8 Problem trying to sample VREFINT

Question asked by quinz.daniel on Oct 8, 2014
Latest reply on Oct 8, 2014 by quinz.daniel
Hi All,

I have configured the ADC according to the initialization code below and then try to to a conversion to read the VREFINT voltage so as to calculated AVDD.  The initialization appears to work ok and the conversion succeeds, but the results returned are incorrect.  In fact, after a hardware reset, the first conversion is way off, the second is close, but the third and subsequent readings are close to the actual AVDD (3.31 as per volt meter and 3.40 as per return value).  I have searched through the datasheet extensively and I cannot explain what is happening.  I suspect something in either my initialization or conversion code is either missing or in the wrong order.

Here is the initialization code:

 
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN | RCC_APB2ENR_ADC1EN ;           // Enable SYSCFG & ADC1 clocksasm ("DSB") ;                                                         // Errata 2.1.8 Work-aroundSYSCFG->CFGR3 |= SYSCFG_CFGR3_EN_BGAP | SYSCFG_CFGR3_ENBUF_BGAP_ADC ; // Enable VREFINT for ADCwhile ((PWR->CSR & PWR_CSR_VREFINTRDYF) == 0) ;                       // Wait VREFINT readyADC1->CR |= ADC_CR_ADCAL ;                                            // Start calibration and enable regulator while ((ADC1->ISR & ADC_ISR_EOCAL) == 0) ;                            // Wait for calibration to be doneADC1->CR |= ADC_CR_ADEN ;                                             // Enable ADCwhile ((ADC1->ISR & ADC_ISR_ADRDY) == 0) ;                            // wait for ADC to be readyADC->CCR |= ADC_CCR_VREFEN | (9U << 18) ;                             // Enable vref internal and divide ADCCLK : 16 MHZ/64 = 250 KHzADC1->SMPR = 2 ;                                                      // (13.5 + 12.5) ADC cycles/conversion = 9615.4 Kspswhile ((SYSCFG->CFGR3 & SYSCFG_VREFINT_ADC_RDYF) == 0) ;              // Wait for ADC to be ready to read VREFINT








Here is the conversion code:


 
ADC1->CHSELR = ADC_CHSELR_CHSEL17 ;        // V-Ref voltageADC1->CR    |= ADC_CR_ADSTART ;            // Start conversionwhile ((ADC1->ISR & ADC_ISR_EOSMP) == 0) ; // Wait for conversion to finishreturn UInt16 (ADC1->DR) ;                 // Clears ADC_ISR_EOSMP flag and returns sample









Outcomes