cancel
Showing results for 
Search instead for 
Did you mean: 

ADC initailization problem on STM32L series

luca2
Associate II
Posted on March 20, 2014 at 13:52

Hi,

I am trying to use ADC for reading the voltage on pin A4.. I wrote an initialization method, however it get stuck in the last WHILE.

Here there is the code:

void ADC_Initialize(void)

{

  ADC_InitTypeDef  ADC_InitStructure={};

  GPIO_InitTypeDef GPIO_InitStructure={};

        /* Enable GPIOA clock */

      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

          /* Configure PA.4 (ADC Channel 4) in analog mode */

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;

      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

      GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* Enable ADC1 clock */

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

        /* ADC1 configuration */

    ADC_InitStructure.ADC_ScanConvMode = DISABLE;

    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;

    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;

    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

    ADC_InitStructure.ADC_NbrOfConversion = 1;

    ADC_Init(ADC1, &ADC_InitStructure);

    /* Enable ADC1 */

        ADC_Cmd(ADC1, ENABLE);

        /* Wait until the ADC1 is ready */

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

}

Here some register values read in debug

Status register SR=256,

Control registers CR1=0 and CR2=1.

The problem is the bit ADONS (bit 6 of SR) that is never set.. and i get stuck inside the while...

Any idea why this happens?

#adc #stm32l #discovery
1 REPLY 1
luca2
Associate II
Posted on March 20, 2014 at 14:16

I actually found the error...my code uses the MSI clock..... but for making the ADC work,  the HSI clock must be enabled anyway...