Struggle with the ADC configuration of an stm32L476RG

0

I work on a project and I wanted to use ADC conversion (I work on an stm32L476), but I'm struggling with my code. My goal was to put a potentiometer between the ground and my 3.3 pin and see the result on PA1 with Putty. Here is the code of my ADC initialisation :

void BSP_Threshold_Init(){
    //Enable GPIOA clock
    RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;

    //Configure PA1 as input
    GPIOA->MODER &= ~GPIO_MODER_MODE1_Msk;
    GPIOA->MODER |= (0x00 <<GPIO_MODER_MODE1_Pos);

     // Enable ADC clock
    RCC->APB2ENR |= RCC_AHB2ENR_ADCEN;

    // Reset ADC configuration
    ADC1->CR    = 0x00000000;
    ADC1->CFGR  = 0x00000000;
    ADC1->CFGR2  = 0x00000000;
    ADC1->SQR1 = 0x00000000;
    ADC123_COMMON->CCR = 0x00000000;

    // Enable continuous conversion mode
    ADC1->CFGR |= ADC_CFGR_CONT;

    // 12-bit resolution
    ADC1->CFGR |= (0x00 <<ADC_CFGR_RES_Pos);

    // Set sampling time to 24.5 ADC clock cycles
    ADC1->SMPR1 |= (0x03 << 0U);

    // Choose the number of conversion (here 1)
    ADC1->SQR1 |= (0x00 << 0U);

    //Choose the channel of the first conversion
    ADC1->SQR1 |= (0x01 << 6U);

    //// Select HCLK/1 as ADC clock
    ADC123_COMMON->CCR |= (0x01 <<ADC_CCR_CKMODE_Pos);

    // Enable ADC
    ADC1->CR |= ADC_CR_ADEN;

    // Start conversion
    ADC1->CR |= ADC_CR_ADSTART;

}

Then in my main, I want to print the value of my ADC on Putty (I worked with Putty several times, so I don't think there is a problem here).

Here is the portion of code in my main :

while(1)
    {

        // Wait here until ADC EOC
        while ((ADC1->ISR & ADC_ISR_EOC) != ADC_ISR_EOC);

        // Report result to console
        my_printf("ADC value = %x\r\n", ADC1->DR);

        // Wait about 200ms
        for (i=0; i<500000; i++);

    }

I printed other things in the init phase with my_printf, but when comes the time to do the loop, nothing appears. Do you have any clues of what I could do wrong ? I don't know if my problem comes from my ADC configuration, or if it comes from my clock configuration or the way I wait for the ADC conversion in my main.

Thanks in advance.

stm32
asked on Stack Overflow Nov 22, 2018 by Hix

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0