Skip to main content
MReyn
Associate
March 8, 2020
Question

Value optimized out when reading from ADC.

  • March 8, 2020
  • 3 replies
  • 4025 views

Hello everyone,

i'm encountering a strange issue, I'm trying to read from the ADC on a STM32f429zi.

When my pin is grounded the value of the ADC is not 0 but around 1000. When i try to debug the value it doesn't give me any result but says: <optimized out>. This is the code i'm using:

	 uint32_t value = 0;
	 HAL_ADC_Start(&hadc1);
	 HAL_ADC_PollForConversion(&hadc1, 100);
	 value = HAL_ADC_GetValue(&hadc1);
	 HAL_ADC_Stop(&hadc1);

I'm using system workbench for stm32 with a ST-link using SWD. I hope somebody can help me solve this issue. Thank you all! 0693W0000000Ax6QAE.jpg

This topic has been closed for replies.

3 replies

TDK
March 8, 2020
  1. Change your compiler setting to "Debug" so it doesn't optimize out variables.
  2. Is that pin initialized correctly? Did you set up the correct channel?

"If you feel a post has answered your question, please click ""Accept as Solution""."
MReyn
MReynAuthor
Associate
March 8, 2020

Thank you for your reply,

i was able to view the exact value of the ADC, when i apply 3.3V the readout is correct. But when i apply 0V to the ADC input pin, the result shows 1023 and this should be zero. What can be the cause of the ADC reference drift? I again confirmed my input voltages and those were ok. When i apply around 0.4V to the inpu0693W0000000B3sQAE.jpgt pin the ADC already goes to 4095. Attached you can find my ADC settings

TDK
March 8, 2020
It is unlikely that you'll get a reading of 1023 if the pin is really at GND. The negative voltage reference is GND, and that doesn't drift relative to itself. I would recheck your assumptions.
What sample time are you using? Change it to the highest and try again.
Tips for general ADC accuracy can be found in AN2834.
"If you feel a post has answered your question, please click ""Accept as Solution""."
etheory
Senior
March 8, 2020

To avoid value being optimized out, it should be set as volatile.

i.e.

volatile uint32_t value = 0u;

Imen GH
ST Employee
March 9, 2020

Hello,

I have reproduced the case on my side and I don’t have any problem (when using ADC1_CH3 on PA3).

when I apply 0V to the ADC input pin, the obtained result is already 0, as shown in Figure (when using IAR).

In addition, I used a potentiometer in input and it can be confirmed that the obtained values vary between 0 and 4095.

Please find attached my file main.c .

Best regards,

0693W0000000KWZQA2.png

MReyn
MReynAuthor
Associate
March 9, 2020

Thank you for your feedback.

I'm pretty sure it's a hardware issue, my code is working fine.

What value pot meter did you use?

Please see my reply above since i did a test on VREFINT resulting in a funky value.