cancel
Showing results for 
Search instead for 
Did you mean: 

ADC Clock and Conversion Time question

giwonKIM
Associate III

Hi, I'm giwon KIM in South Korea.

 

I'm using NUCLEO-401RE board.

 

In STM32F401RE datasheet and reference manual, It is said to be capable of operating at up to 30 MHz.

So its conversion time was set by calculating (12+3) / 30 MHz = 500 nsec.

I generated a sine wave of 250Hz and advertised it, but as calculated, I had to get 8000 samples per cycle, but I only got about 1300.

Is there a reason for this?

Below is actually a waveform drawn with Excel by acquiring a cycle sample.

giwonKIM_0-1724206009978.png

 

And this is my ADC1 init function.

 

/**

* @brief ADC1 Initialization Function

* @param None

* @retval None

*/

static void MX_ADC1_Init(void)

{

 

/* USER CODE BEGIN ADC1_Init 0 */

 

/* USER CODE END ADC1_Init 0 */

 

ADC_ChannelConfTypeDef sConfig = {0};

 

/* USER CODE BEGIN ADC1_Init 1 */

 

/* USER CODE END ADC1_Init 1 */

 

/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)

*/

hadc1.Instance = ADC1;

hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;

hadc1.Init.Resolution = ADC_RESOLUTION_12B;

hadc1.Init.ScanConvMode = DISABLE;

hadc1.Init.ContinuousConvMode = ENABLE;

hadc1.Init.DiscontinuousConvMode = DISABLE;

hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;

hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc1.Init.NbrOfConversion = 1;

hadc1.Init.DMAContinuousRequests = DISABLE;

hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;

if (HAL_ADC_Init(&hadc1) != HAL_OK)

{

Error_Handler();

}

 

/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.

*/

sConfig.Channel = ADC_CHANNEL_0;

sConfig.Rank = 1;

sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;

if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN ADC1_Init 2 */

 

/* USER CODE END ADC1_Init 2 */

 

}

 

If you have any question, please ask me.

 

Thank you.

6 REPLIES 6
TDK
Guru

> sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;

Conversion time will be 15 + 12 ticks in this case. 15 for sampling and 12 for conversion. With a 30 MHz clock, this is 900 ns

A 250 Hz sine wave period is 4 ms.

4 ms / 900 ns = 4444.

 

So why is yours different? Perhaps show your code.

  • What is your ADC clock? Show your clock setup.
  • Are you using continuous ADC conversion to a buffer using DMA? It's not going to work well with polling.
If you feel a post has answered your question, please click "Accept as Solution".
MasterT
Lead

ADC_SAMPLETIME_15CYCLES

12 + 15 / 30 MHz = ?

giwonKIM
Associate III

HI,

I modified my code and settings.

 

giwonKIM_0-1724217085172.png

/**

* @brief ADC1 Initialization Function

* @param None

* @retval None

 

*/

static void MX_ADC1_Init(void)

{

 

/* USER CODE BEGIN ADC1_Init 0 */

 

/* USER CODE END ADC1_Init 0 */

 

ADC_ChannelConfTypeDef sConfig = {0};

 

/* USER CODE BEGIN ADC1_Init 1 */

 

/* USER CODE END ADC1_Init 1 */

 

/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)

*/

hadc1.Instance = ADC1;

hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;

hadc1.Init.Resolution = ADC_RESOLUTION_12B;

hadc1.Init.ScanConvMode = DISABLE;

hadc1.Init.ContinuousConvMode = ENABLE;

hadc1.Init.DiscontinuousConvMode = DISABLE;

hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;

hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc1.Init.NbrOfConversion = 1;

hadc1.Init.DMAContinuousRequests = DISABLE;

hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;

if (HAL_ADC_Init(&hadc1) != HAL_OK)

{

Error_Handler();

}

 

/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.

*/

sConfig.Channel = ADC_CHANNEL_0;

sConfig.Rank = 1;

sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;

if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN ADC1_Init 2 */

 

/* USER CODE END ADC1_Init 2 */

 

}

 

The output sine waveform is 250Hz as it is, and how many samples should be produced at this time?

 

My calculation is (12+3) / 84MHz = 22400, But if you sample one cycle, there are about 13,000.

 

DMA is not in use and operates in polling mode.

 

Thank you.

giwonKIM
Associate III

ADCCLK is shown in the Datasheet like this.

giwonKIM_0-1724224363045.png

If I set APB2 to 60Mhz and clock divide to 2, is it correct that ADC operates at 30MHz?

giwonKIM_1-1724224452955.png

 

 

 

> DMA is not in use and operates in polling mode.

So that's your problem. The chip takes time to run code, don't expect to get 5.6 Msps in polling mode.

If you feel a post has answered your question, please click "Accept as Solution".

I understand what you're saying. I'll have to approach it in a different way.

 

Thank you.