cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 ADC error

brian-o
Associate
I have an ADC issue with the STM32F0. Looked all through the HAL docs and not sure what the error is. I can't get the ADC to complete a conversion or if it does only the first call. When I debug with the ADC calibration call, it goes to a fault. When I try sampling it with polling it doesn't update (I'm guessing because the conversion never completes). If I start it with the interrupt call, it never services the interrupt because I'm guessing the conversion never completes.
 
Everything was setup in CubeMX as below
briano_0-1703132087156.png

 

My attempt to get the ADC value (also not sure if I should be calling to handle &hadc3, but result is the same with no ADC value)

[code]

HAL_Delay(1);

HAL_ADC_Start(&hadc1);

// Poll ADC1 Perihperal & TimeOut = 1mSec

HAL_ADC_PollForConversion(&hadc1, 10);

// Read The ADC Conversion Result & Map It To PWM DutyCycle

AD_RES = HAL_ADC_GetValue(&hadc1);

HAL_ADC_Stop(&hadc1);

[/code]

3 REPLIES 3
Pierre_Paris
ST Employee

Hello @brian-o,

Thanks for your question !

Here some remark and guidance :

  • "I can't get the ADC to complete a conversion or if it does only the first call" -> Can you please share your handler ? How many channel do you use ? 
  • For the execution, you respected well the sequence below for the execution of ADC conversion by polling :
    • 1/ Activate the ADC peripheral and start conversions using function HAL_ADC_Start()
      2/ Wait for ADC conversion completion using function HAL_ADC_PollForConversion()
      3/ Retrieve conversion results using function HAL_ADC_GetValue()
      4/ Stop conversion and disable the ADC peripheral using function HAL_ADC_Stop()
  • But you need to configure the ADC first by respecting :
    • 1/ Configure the ADC parameters (resolution, data alignment, ...) and regular group parameters (conversion trigger, sequencer, ...) using function HAL_ADC_Init().

      2/ Configure the channels for regular group parameters (channel number, channel rank into sequencer, ..., into regular group) using function HAL_ADC_ConfigChannel().

      3/ Also, I advise you to use HAL_ADCEx_Calibration_Start() to perform an automatic ADC calibration.
  • Have you thought implemented callback functions ?

Best Regards,

Pierre

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Thank you Pierre,

the ADC calibration call (when used) enters some failure leading to a WHILE(1) loop in the HAL library, so there is likely a related issue.

ADC initialization code is below from the cube generated content, &hadc1 or 3 handle is declared in my globals:

ADC_HandleTypeDef hadc1;

 

Initialization code:

static void MX_ADC_Init(void)

{

 

/* USER CODE BEGIN ADC_Init 0 */

 

/* USER CODE END ADC_Init 0 */

 

ADC_ChannelConfTypeDef sConfig = {0};

 

/* USER CODE BEGIN ADC_Init 1 */

 

/* USER CODE END ADC_Init 1 */

 

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

*/

hadc.Instance = ADC1;

hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;

hadc.Init.Resolution = ADC_RESOLUTION_10B;

hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;

hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

hadc.Init.LowPowerAutoWait = DISABLE;

hadc.Init.LowPowerAutoPowerOff = DISABLE;

hadc.Init.ContinuousConvMode = DISABLE;

hadc.Init.DiscontinuousConvMode = DISABLE;

hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;

hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

hadc.Init.DMAContinuousRequests = DISABLE;

hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;

if (HAL_ADC_Init(&hadc) != HAL_OK)

{

Error_Handler();

}

 

/** Configure for the selected ADC regular channel to be converted.

*/

sConfig.Channel = ADC_CHANNEL_3;

sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;

sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;

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

{

Error_Handler();

}

/* USER CODE BEGIN ADC_Init 2 */

 

/* USER CODE END ADC_Init 2 */

 

}

Pierre_Paris
ST Employee

Hello @brian-o ,

Thank you for sharing your code ! For your information, there's a button for a better code display, don't hesitate to use it next time (you can check the picture below) :)

Pierre_P_0-1703236089193.png

Regarding the issue :

  • You should enable Continuous mode by changing "hadc.Init.ContinuousConvMode = ENABLE;" to automatically restart conversion after each one.
  • Can you specified which failure ? Have you configure the ADC before calibration ?

Best Regards,

Pierre

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.