AnsweredAssumed Answered

STM32F4 ADC in scan/discontinuous mode using HAL

Question asked by du.hay on Mar 12, 2016
Latest reply on Mar 12, 2016 by kudryashov.dmitry

Hi all,

I am using STM32F407 device and IAR EWARM.

I want to use the ADC in STM32 in the following way:
At each 2ms, ADC1 converts 3 channels. let's say ch0 then ch2 and then ch3.
After that the ADC stops waiting for the next 2ms.
On the 2nd 2ms ADC converts the 3 channels: ch0 then ch2 and then ch3.
This means for each timer trigger I want to get three conversions.
I am lost which ADC mode I can use.
I am testing the scan mode but without success. Each conversion needs its own trigger.
i.e I can't trigger three conversions with a single timer trigger.
Now I am trying the discontinuous mode and without success too.
For debugging, I am counting the number of ADC interrupts fired vs the number of TIM interrupts fired. I have to find ADC interrupts counter = 3 x TIM interrupts counter.

Which ADC mode do you think I can use?

I know I can use the software trigger with 1ms/3 but I want to use the suitable hardware feature.
Here below my ADC configuration. Let me know your comments:

   /* ADC Initialization */
  mhandle.Instance          = ADC1;
  mhandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
  mhandle.Init.Resolution = ADC_RESOLUTION_12B;
  mhandle.Init.ScanConvMode = DISABLE;
  mhandle.Init.ContinuousConvMode = DISABLE;
  mhandle.Init.DiscontinuousConvMode = ENABLE;
  mhandle.Init.NbrOfDiscConversion = 3;
  mhandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
  mhandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T8_TRGO;
  mhandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  mhandle.Init.NbrOfConversion = 3;
  mhandle.Init.DMAContinuousRequests = ENABLE;
  mhandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
 
  HAL_ADC_Init(&mhandle);
 
  /* Configure ADC3 regular channel */ 
  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  sConfig.Offset = 0;
 
  HAL_ADC_ConfigChannel(&mhandle, &sConfig);

  sConfig.Channel = ADC_CHANNEL_2;
  sConfig.Rank = 2;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  sConfig.Offset = 0;
 
  HAL_ADC_ConfigChannel(&mhandle, &sConfig);
 
  sConfig.Channel = ADC_CHANNEL_3;
  sConfig.Rank = 3;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  sConfig.Offset = 0;
 
  HAL_ADC_ConfigChannel(&mhandle, &sConfig);

and I am using HAL_ADC_Start_IT(&mhandle) then HAL_TIM_Base_Start_IT(&htim) once ADC is set up.

My ISR look like this:

void TIM8_UP_TIM13_IRQHandler(void)
{
  TIMCounter++;
  call hal tim irq handler
}
void ADC_IRQHandler(void)
{
   ADCCounter++; // ADCCounter should be three times TIMCounter
    call hal adc irq handler
}

 


Outcomes