cancel
Showing results for 
Search instead for 
Did you mean: 

ADC gets stuck after doing a HAL_Delay

xpp07
Senior

In the while loop I have this:

HAL_ADC_PollForConversion(&hadc1, 1000);
adcValue = HAL_ADC_GetValue(&hadc1);

The ADC works okay until I execute this line, which is inside a function:

HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET);
HAL_Delay(3000);
command_dutyC = 0.5;

This delay is used just once in the whole program, i .e, when the delay's completed, it won't execute more.

The rest of the program continues working well after the delay, but the ADC gets stuck, it displays the same value all the time, even when the input is changing.

This is my ADC init:

static void MX_ADC1_Init(void)
{
 
  ADC_ChannelConfTypeDef sConfig;
 
    /**Common config 
    */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV16;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.NbrOfDiscConversion = 1;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc1.Init.OversamplingMode = DISABLE;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_8;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
}

1 ACCEPTED SOLUTION

Accepted Solutions

Ok, and didn't I say that everything other than SysTick needs a priority >= 1

You're trying to ensure SysTick can interrupt whatever else is running/blocking, this isn't going to happen if everyone is set to zero. You'll spin in a loop and the tick count will never go up.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

9 REPLIES 9

Remember the callbacks occur under interrupt context. You need to make SysTick (or other clocking source) can preempt all other interrupts.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
xpp07
Senior

How do I do that?

I'd supposed you'd make sure TICK_INT_PRIORITY is zero, that others are set higher, and the NVIC mode is set to 4-bit preemption mode.

Grep/Find-in-Files for HAL_NVIC_SetPriority, HAL_NVIC_SetPriorityGrouping in HAL_Init, etc.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
xpp07
Senior

Look

0690X000006CN0bQAG.png

xpp07
Senior

That's my NVIC configuration. I'm still having issues with the ADC after the delay.

Bob S
Principal

What status is HAL_ADC_PollForConversion() returning when it fails (edit: change you code so that you DO check the return status, don't assume is works)? If that doesn't clear things up, use your debugger and single-step through the HAL_ADC_PollForConversion() function to see why it is failing.

Ok, and didn't I say that everything other than SysTick needs a priority >= 1

You're trying to ensure SysTick can interrupt whatever else is running/blocking, this isn't going to happen if everyone is set to zero. You'll spin in a loop and the tick count will never go up.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
nerdHardware
Associate

HAL_Delay() gets stuck because the ​system tick is not incremented. Simply include this function HAL_IncTick(); in the SysTick_Handler(void);

HHarj.1
Senior

don't forget to start the ADC.