1
\$\begingroup\$

I'm trying to read my ADC pin but i'm getting strange results. When my ADC input pin is connected to ground, the result from my ADC is definitely not zero. I know it's somewhere between 1000-2000. When i try to read the exact value within the debug window it says: optimized out. What could be the reason for this issue since i already had achieved this with the same chip/code? I've measured the input pin and it's definitely 0V I'm using system workbench for stm32 and CubeMX, programming trough an ST-link. Chip is an STM32f429zi Here is my code, i use the value later in my code.

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

enter image description here I hope someone can help me solve this issue. Thank you all!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ did you forget a "volatile" pragma on some declaration? \$\endgroup\$ Mar 8, 2020 at 22:14
  • \$\begingroup\$ Declare 'value' outside of the interrupt and mark it volatile, or use it to do something (write a register or output). \$\endgroup\$
    – Ron Beyer
    Mar 8, 2020 at 22:49
  • \$\begingroup\$ This code is inside my main loop, not in an interrupt. \$\endgroup\$ Mar 8, 2020 at 22:51
  • 1
    \$\begingroup\$ It's still the same thing, if the compiler detects that you write to a variable but never read from it, it's eligible to be optimized out because the compiler figures it's never used. \$\endgroup\$
    – Ron Beyer
    Mar 8, 2020 at 22:58

2 Answers 2

3
\$\begingroup\$

You need to make 'value' volatile or perform some dummy operation on it after loading to prevent your compiler from optimizing out. Bonus tip, if you're in System Workbench, you might want to change your compiler settings to 'optimize for debug' if you're using a debugger tool. You also may want to consider using the DMA peripheral, which pairs nicely with STM32's ADC.

\$\endgroup\$
1
\$\begingroup\$

I think that the "value" variable you define is a local variable. When debugging a local variable, you must define a breakpoint in the function that defines the local variable, and then you can display the value of the variable correctly during debugging.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.