2024-08-20 07:09 PM
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.
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.
2024-08-20 07:21 PM
> 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.
2024-08-20 07:25 PM
ADC_SAMPLETIME_15CYCLES
12 + 15 / 30 MHz = ?
2024-08-20 10:15 PM
HI,
I modified my code and settings.
/**
* @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.
2024-08-21 12:14 AM
ADCCLK is shown in the Datasheet like this.
If I set APB2 to 60Mhz and clock divide to 2, is it correct that ADC operates at 30MHz?
2024-08-21 05:40 AM
> 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.
2024-08-21 04:37 PM
I understand what you're saying. I'll have to approach it in a different way.
Thank you.