cancel
Showing results for 
Search instead for 
Did you mean: 

Why my ADC value alternates when signal is steady?

Henrik Sandaker Palm
Associate II
Posted on December 29, 2016 at 15:53

Hi,

I'm quite new to STM32 and Cube HAL. Hardware is custom, mcu is STM32F042G6Ux, created initial code with Cube MX. IDE is 

SW4STM32.

The issue is that the ADC value is alternating somewhat between two values (roughly 300-something mostly and sometimes 1700-something). I see it while debugging, I do one iteration of the main while-loop and watch the ADC value

adc_val

 returned from HAL_ADC_GetValue function. The voltage is around 450mV. The reading of the internal temperature via ADC is stable and fine.

Here is the code (how to embed code properly?): 

http://pastebin.com/rvLLcXsk

 
13 REPLIES 13
Posted on December 31, 2016 at 12:54

I've set up both ADCs now for 239 cycles sampling time, which is maximum.

With this sequence (someone, how can I embed code properly?):

volatile uint32_t temp_ret = HAL_ADC_PollForConversion(&hadc_temp, HAL_MAX_DELAY);

volatile uint32_t adc_ret = HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);

temp = HAL_ADC_GetValue(&hadc_temp);

adc_val = HAL_ADC_GetValue(&hadc);

...both values are always the same, and still alternating randomly.

But when I do as suggested, reading the adc a few times:

//Read temp.

volatile uint32_t temp_ret = HAL_ADC_PollForConversion(&hadc_temp, HAL_MAX_DELAY);

temp = HAL_ADC_GetValue(&hadc_temp);

temp_ret = HAL_ADC_PollForConversion(&hadc_temp, HAL_MAX_DELAY);

temp = HAL_ADC_GetValue(&hadc_temp);

temp_ret = HAL_ADC_PollForConversion(&hadc_temp, HAL_MAX_DELAY);

temp = HAL_ADC_GetValue(&hadc_temp);

//Read ADC

volatile uint32_t adc_ret = HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);

adc_val = HAL_ADC_GetValue(&hadc);

adc_ret = HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);

adc_val = HAL_ADC_GetValue(&hadc);

adc_ret = HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);

adc_val = HAL_ADC_GetValue(&hadc);

...I always get consistent and correct answers! 

So I while I'm thankful for the solution, I wonder why it is so? Now that I have a stable adc readout I can confirm that it was the two channels adc readings that got shared between themselves.

Posted on December 31, 2016 at 13:58

Made a new observation. As you might sawe the readings were stable and correct when reading the adc three times each as in the code I posted above. But if I read them only two times each, I now get the ~1670 value in both readings, only that they can differ slightly like +/- 1 or 2. So It's the same channel but different readings. I find this very strange.

Henrik Sandaker Palm
Associate II
Posted on January 04, 2017 at 19:33

After reading a bit more, many of my mistakes became very obvious. Reading my code (in the first post),  I'm initializing ADC1 twice, also trying to set different read cycles and giving ranking numbers on individual channels, which is not supported on my mcu. 

Henrik Sandaker Palm
Associate II
Posted on January 16, 2017 at 09:26

Also what probably put me off is that I didn't realize the ADC kept running and sampling while I had the debugger pausing the mcu core. This gives strange results, especially if you're pausing between ADC samples or within ADC interrupt callback routines.