cancel
Showing results for 
Search instead for 
Did you mean: 

ADC1 on STM32F334R8 - 2 channels - Software triggered conversion

MRach
Associate

I'd like to convert voltages on 2 channels of ADC1 on STM32F334R8 with no external triggering (no timer) and no DMA. I'd like to use software triggered conversion and I'd like to execute the following code in an infinite loop:

---- Rank 1

  • Launch conversion
  • Wait for the end of conversion
  • Read the numerical value

---- Rank 2

  • Launch conversion
  • Wait for the end of conversion
  • Read the numerical value

Two cases:

  • When continuous-time conversion is enabled (Code 1 below): When reading the numerical value, how would it be possible to say whether we are reading the value of Rank 1 or the value of Rank 2???
  • When continuous-time conversion is disabled (Code 2 below) and the sampling period is as low as 1.5 cycles: the same numerical value is returned for Rank 1 and Rank 2. Why is it so? Is the ADC performing its own sampling at the indicated rate? If the sampling period is too low and the ADC does not have time to perform conversion of both ranks, does it give priority to Rank1 which seems to be the only voltage converted? When the sampling is 19.5 or higher, the conversion of both ranks works fine.

-------- Code 1 -- Continuous-time conversion enabled ---------

HAL_ADC_Start(&hadc1);

while (1){

// Rank 1 (?)

if (HAL_ADC_PollForConversion(&hadc1, 1000) == HAL_OK){

int x = HAL_ADC_GetValue(&hadc1);

}

// Rank 2 (?)

if (HAL_ADC_PollForConversion(&hadc1, 1000) == HAL_OK){

int y = HAL_ADC_GetValue(&hadc1);

}

// some other code is here

}

// the pb here is that HAL_ADC_GetValue() returns conversion of either Rank1 or Rank2. How to figure out which rank has been converted? Or how to force conversion of a given rank?

-------- Code 2 -- Continuous-time conversion disabled ---------

while (1){

HAL_ADC_Start(&hadc1);

// Rank 1 (?)

if (HAL_ADC_PollForConversion(&hadc1, 1000) == HAL_OK){

int x = HAL_ADC_GetValue(&hadc1);

}

// Rank 2 (?)

if (HAL_ADC_PollForConversion(&hadc1, 1000) == HAL_OK){

int y = HAL_ADC_GetValue(&hadc1);

}

HAL_ADC_Stop(&hadc1);

// some other code is here

}

// the pb here appears when the sampling is as low as 1.5 cycles for both ranks. The same value is returned at every execution of HAL_ADC_GetValue(). When the sampling is 19.5 or higher, the conversion of both ranks works fine.

Please find below the configuration of STM32CubeMX.

------- STM32 CubeMC - Configuration -------

ADC Settings

  • Scan conversion mode enabled
  • continuous conversion mode enabled for Code1 / disabled for Code 2
  • DMA Disabled
  • End of conversion Selection: End of Single conversion
  • Overrun behavior: Data overritten

ADC_Regular_Conversion_Mode

  • Enable regular conversion: Enable
  • Number of conversions 2
  • External Trigger conversion source: Regular conversion launched by software
  • External Trigger conversion Edge: None

Rank 1 :

  • Channel: Channel temperature source
  • Sampling time: 4.5 cycles
  • ...

Rank 2

  • Channel: Vrefint
  • Sampling time: 4.5 cycles.

0 REPLIES 0