cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F334R8 ADC problem

Santos
Associate

Hello everyone!

I am trying to use the ADC of my NUCLEO-F334R8 board. I want to read the PC1 pin (ADC12, channel 7). I try to initialize the ADC with the following code:

void BSP_ADC_Init()
{
    // Enable GPIOC clock
    RCC->AHBENR |= RCC_AHBENR_GPIOCEN;

    // Configure pin PC1 as analog
    GPIOC->MODER &= ~GPIO_MODER_MODER1_Msk;
    GPIOC->MODER |= (0x03 << GPIO_MODER_MODER1_Pos);

    // Enable ADC clock
    RCC->AHBENR |= RCC_AHBENR_ADC12EN;

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

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

    // Select channel 7
    ADC1->SQR1 |= (0x07 << ADC_SQR1_SQ1_Pos);

    // Set sampling time to 19.5 ADC clock cycles
    ADC1->SMPR1 = (0x04 << ADC_SMPR1_SMP7_Pos);

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

    // Select PCLK/2 as ADC clock
    RCC->CFGR2 |= (0x11 << RCC_CFGR2_ADCPRE12_Pos);

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

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

But it seems not to work. In the main, I get stuck in the loop:

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

EOC never sets and DR is always 0.

Could anyone help me?

 
2 REPLIES 2
Saket_Om
ST Employee

Hello @Santos 

Please follow the configuration provided in the LL example "Projects/STM32F334R8-Nucleo/Examples_LL/ADC/ADC_ContinuousConversion_TriggerSW".

 

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

By default, ADC clock is selected as asynchronous in ADCx_CCR.CKMODE. However, for asynchronous clock, you would need to set up PLL in RCC, and also set RCC_CFGR2.ADC12PRES to non-zero. Alternatively, set synchronous clock in ADCx_CCR.CKMODE.

JW