cancel
Showing results for 
Search instead for 
Did you mean: 

ADC of STM32F401RC sometimes do not work

Pzhu.1
Associate II

Hi,

I configure the ADC as the following code; but sometimes(not everytime) the code will run to Line 70 : "Beer_Out_Dis(1);" ,and ADC will not work.

Did I do something wrong? Thanks verymuch.

/* ADC1 init function */
static void MX_ADC1_Init(void)
{
 
  ADC_ChannelConfTypeDef sConfig;
 
    /**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 = DISABLE;
  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 = 7;
  hadc1.Init.DMAContinuousRequests = ENABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  HAL_ADC_Init(&hadc1);
 
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. 
    */
  sConfig.Channel = ADC_CHANNEL_0;  //USB -> PA0 Voltage
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig); 
 
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. 
    */
  sConfig.Channel = ADC_CHANNEL_1;  //USB  ->  PA1 Current
  sConfig.Rank = 2;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig); 
  
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. 
    */
  sConfig.Channel = ADC_CHANNEL_4;  // J2 -> PA4
  sConfig.Rank = 3;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig); 
 
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. 
    */
  sConfig.Channel = ADC_CHANNEL_5;  // J10 -> PA5
  sConfig.Rank = 4;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
 
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. 
    */
  sConfig.Channel = ADC_CHANNEL_6;  // J4 -> PA6
  sConfig.Rank = 5;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig); 
 
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. 
    */
  sConfig.Channel = ADC_CHANNEL_7;  // J13 -> PA7
  sConfig.Rank = 6;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
 
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. 
    */
  sConfig.Channel = ADC_CHANNEL_9;  //POWER ->PB1
  sConfig.Rank = 7;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig);
 
  HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&Ad_Data[0], 7);
  
  if ( HAL_ADC_Start(&hadc1)!=HAL_OK)
  {
     Beer_Out_Dis(1);
  }
  
}

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Don't call both HAL_ADC_Start_DMA and HAL_ADC_Start. If it's busy with the first call, the second one will fail. If you want to use DMA, use HAL_ADC_Start_DMA. If you want to use polling, use HAL_ADC_Start.

https://github.com/STMicroelectronics/STM32CubeF4/blob/2f3b26f16559f7af495727a98253067a31182cfc/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c#L87

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

View solution in original post

2 REPLIES 2
TDK
Guru

Don't call both HAL_ADC_Start_DMA and HAL_ADC_Start. If it's busy with the first call, the second one will fail. If you want to use DMA, use HAL_ADC_Start_DMA. If you want to use polling, use HAL_ADC_Start.

https://github.com/STMicroelectronics/STM32CubeF4/blob/2f3b26f16559f7af495727a98253067a31182cfc/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c#L87

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

Thanks very much! I understand and I will modify the code.