cancel
Showing results for 
Search instead for 
Did you mean: 

CUBEMX ADC

Rudas.Robert
Associate II
Posted on May 23, 2016 at 23:14

Hi,

I have started to work with STM32L1. Maybe it's a silly questestion, but how to read multiple ADC channels without DMA?

It is simple to read 1 channel with HAL_ADC_GetValue(&hadc);

or multiple channels with DMA HAL_ADC_Start_DMA(&hadc, advalues, 3);

 

 

 

/* ADC init function */

 

void MX_ADC_Init(void)

 

{

 

 

  ADC_ChannelConfTypeDef sConfig;

 

 

    /**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_RESOLUTION12b;

 

  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;

 

  hadc.Init.ScanConvMode = ADC_SCAN_DISABLE;

 

  hadc.Init.EOCSelection = EOC_SEQ_CONV;

 

  hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE;

 

  hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;

 

  hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A;

 

  hadc.Init.ContinuousConvMode = DISABLE;

 

  hadc.Init.NbrOfConversion = 3;

 

  hadc.Init.DiscontinuousConvMode = DISABLE;

 

  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;

 

  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

 

  hadc.Init.DMAContinuousRequests = DISABLE;

 

  HAL_ADC_Init(&hadc);

 

    

 

 

 

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.

 

    */

 

  sConfig.Channel = ADC_CHANNEL_11;

 

  sConfig.Rank = 1;

 

  sConfig.SamplingTime = ADC_SAMPLETIME_4CYCLES;

 

  HAL_ADC_ConfigChannel(&hadc, &sConfig);

 

 

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.

 

    */

 

  sConfig.Channel = ADC_CHANNEL_12;

 

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

 

  sConfig.Rank = 3;

 

  HAL_ADC_ConfigChannel(&hadc, &sConfig);

 

 

}

 

2 REPLIES 2
Posted on May 24, 2016 at 02:16

how to read multiple ADC channels without DMA?

You could use different ADC? The STM32 model is to use DMA so there isn't a lot of wasted time reconfiguring, looping, and the measurements can be serviced in a HT/TC interrupt.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
carmine
Associate II
Posted on May 24, 2016 at 16:08

This is the procedure:

  1. Configure the ADC so that a scan conversion is performed (hadc.Init.ScanConvMode = ADC_SCAN_ENABLE). 
  2. Set EOC to end of each conversion rank (hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV).
  3. Configure channels.
  4. Start conversions using HAL_ADC_Start();
  5. Poll for each conversion using HAL_ADC_PollForConversion().
  6. Get the conversion result using HAL_ADC_GetValue().

IMHO, performing scan conversion using DMA is the best option.