cancel
Showing results for 
Search instead for 
Did you mean: 

My ADC in STM32F401 is not working at some sample rate sycles

CCobo.1
Associate

Hi, i am trying to get three analog signals using adc and dma in normal mode, when the buffer is complete I then process the information doing rms value, mean, std deviation etc.

I have the PCLK2 clock at 84 MHz and clock prescaler of the ADC at 4, using DMA in normal mode with continuous conversion mode and scan conversion mode.

The signals I'm trying to read are in the 20-30 Khz so 3 cycles sample gives me not enough precision and consistency in the values, 56 cycles could work but it would be nice to have 15 or 28 but the adc seems not working with these sample rates (i don't get any readings and the callbacks are never called)

I can't post photo of cubeIDE so i post the code generated, and my main

#define BUFFER_SIZE 1200

uint32_t adc_values[BUFFER_SIZE];

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_DMA_Init();

 MX_USART2_UART_Init();

 MX_ADC1_Init();

 /* USER CODE BEGIN 2 */

 HAL_ADC_Start(&hadc1);

 HAL_ADC_Start_DMA(&hadc1, adc_values, BUFFER_SIZE);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

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 = 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 = 3;

 hadc1.Init.DMAContinuousRequests = DISABLE;

 hadc1.Init.EOCSelection = ADC_EOC_SINGLE_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_56CYCLES;

 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 = 2;

 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_4;

 sConfig.Rank = 3;

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

 {

  Error_Handler();

 }

 /* USER CODE BEGIN ADC1_Init 2 */

 /* USER CODE END ADC1_Init 2 */

}

1 REPLY 1
TDK
Guru

> HAL_ADC_Start(&hadc1);

> HAL_ADC_Start_DMA(&hadc1, adc_values, BUFFER_SIZE);

You should only be calling one of these. See CubeMX examples.

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