2016-12-29 06:53 AM
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?):
2016-12-31 04:54 AM
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.
2016-12-31 05:58 AM
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.
2017-01-04 10:33 AM
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.
2017-01-16 12:26 AM
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.