Skip to main content
Associate
Mar 26, 2025 2:05 PM UTC
Solved

STM32F334R8 ADC problem

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?

 
This topic has been closed for replies.
waclawek.jan
waclawek.janBest answer
Super User
Apr 28, 2025 4:57 AM UTC

Did you follow the procedures to set those bits, outlined in ADVREG enable sequence and Software procedure to enable the ADC subchapters of the ADC chapter in RM? In other words, you need to wait both after enabling ADCREG and ADEN (the former is just a software delay, the latter indicated by ADRDY).

JW

9 replies

ST Technical Moderator
Mar 28, 2025 10:35 AM UTC

Hello @Santos 

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

 

 

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om
waclawek.jan
Super User
Mar 28, 2025 2:11 PM UTC

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

SantosAuthor
Associate
Apr 21, 2025 11:08 AM UTC

Thank you for your suggestion. I have already configured the PLL, and as you can see in my code, I have set RCC_CFGR2.ADC12PRES to a non-zero value (RCC->CFGR2 |= (0x11 << RCC_CFGR2_ADCPRE12_Pos)). Unfortunately, setting the synchronous clock in ADCx_CCR.CKMODE hasn't worked either.

waclawek.jan
Super User
Apr 22, 2025 9:50 AM UTC

Read out and check/post the ADC and RCC registers content.

JW

SantosAuthor
Associate
Apr 22, 2025 2:27 PM UTC

RCC_AHBENR: 0x100a0014

RCC_CFGR2: 0x110

ADC1_CR: 0x5

ADC1_CFGR: 0x2000

ADC1_SQR1: 0x1c0

ADC1_SMPR1: 0x800000

waclawek.jan
Super User
Apr 22, 2025 3:30 PM UTC

You don't have enabled ADVREGEN in ADC1_CR.

JW

SantosAuthor
Associate
Apr 23, 2025 12:26 PM UTC

I have just enabled it (ADC1_CR=0x10000005), but ADC still doesn't work.

waclawek.jan
waclawek.janBest answer
Super User
Apr 28, 2025 4:57 AM UTC

Did you follow the procedures to set those bits, outlined in ADVREG enable sequence and Software procedure to enable the ADC subchapters of the ADC chapter in RM? In other words, you need to wait both after enabling ADCREG and ADEN (the former is just a software delay, the latter indicated by ADRDY).

JW

SantosAuthor
Associate
May 5, 2025 10:30 AM UTC

Yes, you are absolutely right. Specifically, I was missing the step of waiting for the ADRDY after enabling the ADC, before setting the ADSTART bit. Now everything is working fine. Thank you very much for your help.