2024-03-18 03:06 AM - edited 2024-03-18 03:28 AM
Hello,
I would like to determine the ADC sampling frequency.
I am initializing the ADC with following:
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
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 = 2;
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 = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_112CYCLES;//ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != 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_1;
sConfig.Rank = ADC_REGULAR_RANK_2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
Here is a content of my SystemClock_Config:
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 128;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
How do I determine (or eventually set following my desire) the ADC sampling frequency? I believe it's done with "sConfig.SamplingTime = ADC_SAMPLETIME_112CYCLES;". How long is one cycle in my setup?
Thank you very much!
Vaclav
Solved! Go to Solution.
2024-03-18 05:16 AM
So AHB is at 64M now; divider 4 -> 16M ; = 62,5ns
adc sampl. 112 + 12 (12 bit resolution) -> 124 cyc.
16M / 124 = 129 kHz , sampling.
+
I always check real rate, its so easy to calculate wrong. :)
Simple check: make a loop with adc conversions and set /reset a port pin, maybe a LED is on a pin you can use.
Then look with scope, is it about the right timing.(if using HAL calls, they might need extra 1..2us , so just do a loop with 1000 conversions , should need about 8 ms . )
Or make longer loop, 10000 conversions, and check with reading HAL_GetTick(); and see time in ms steps; about 80 ms then.
2024-03-18 03:29 AM
Hi,
the ADC is on AHB bus , you set to xx? MHz . + divide by 4 .
ADC needs 12 clks + sampling time, you set to ??? ; on rank ? , you set to have 2 conversions.
So conversion speed is 12+sampling clocks at the ahb-bus/4 clock.
2024-03-18 04:38 AM
Can you please check my clock setup and tell me the ADC Sampling Frequency?
Here is the clock setup:
As I wrote before, I am setting sConfig.SamplingTime = ADC_SAMPLETIME_112CYCLES.
Thank you very much!
Vaclav
2024-03-18 05:16 AM
So AHB is at 64M now; divider 4 -> 16M ; = 62,5ns
adc sampl. 112 + 12 (12 bit resolution) -> 124 cyc.
16M / 124 = 129 kHz , sampling.
+
I always check real rate, its so easy to calculate wrong. :)
Simple check: make a loop with adc conversions and set /reset a port pin, maybe a LED is on a pin you can use.
Then look with scope, is it about the right timing.(if using HAL calls, they might need extra 1..2us , so just do a loop with 1000 conversions , should need about 8 ms . )
Or make longer loop, 10000 conversions, and check with reading HAL_GetTick(); and see time in ms steps; about 80 ms then.
2024-03-18 05:23 AM
Thank you very much AScha.3!