I'm working on some embedded software and I ran into a problem and I cannot figure out why this block isn't working.
I set up interrupts to for several buttons, including a mechanical encoder. I used breakpoints and debugging to determine the code flow. The block that isn't working as it should is super simple.
uint8_t calcEncoderValue(void)
{
int encVal = 0;
if (gpio_get_pin_level(ENC_A))
{
encVal = 2;
}
if (gpio_get_pin_level(ENC_B))
{
encVal = encVal + 1;
}
return (uint8_t) encVal;
}
The code goes into the first if
statement when A is HIGH, but value for encVal remains 0.
Coding doesn't get much simpler than this block, so I'm absolutely stumped. Does anyone have any idea what is going wrong here?
2 Answers 2
This line:
int encVal = 0;
Creates a variable local to the ISR.
If you expect that variable name to be globally accessible then you could have problems.
Even if you have declared a global variable by that name, the local version of encVal will be changed inside the ISR and then will be discarded when the ISR returns.
As Peter comments, in the global declaration it should be declared with the volatile keyword so the compiler knows it may change at any time (changed by the ISR, in this case).
Just another perspective. Take it with a grain of salt.
In C, when a logical operation is being evaluated, if the result is known before all subexpressions have been evaluated, then the evaluation stops, or short circuits.
A "Short Circuit Evaluation" might have happened in the "if (gpio_get_pin_level(ENC_x))".
Refer to this article https://microchipdeveloper.com/c:short-circuit-evaluation
-
1\$\begingroup\$ Short circuit evaluation refers to
&&
and||
. There's no way for the function calls to not occur in these expressions. And declaring a temporary variable for their return values as volatile does nothing useful. \$\endgroup\$Oskar Skog– Oskar Skog2022年10月23日 08:11:32 +00:00Commented Oct 23, 2022 at 8:11 -
1\$\begingroup\$ If there is a theoretical possibility for this to be a short circui evaluation problem, please at least mention where would the short circuit happen and why. \$\endgroup\$Justme– Justme2022年10月23日 08:14:47 +00:00Commented Oct 23, 2022 at 8:14
volatile
keyword is your friend in that case. \$\endgroup\$