This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Strange execution sequence

Could someone please help me in clarifying that weird execution sequence of the following function:

int S2Pressed (void) {
  static int S2KeyCount = 0, S2KeyPressed = 0;

01  if (S2KeyPressed) {
02      if (((GPIOA->IDR & S2) == 0 ))  {             // Check if S2 is pressed
03      if (S2KeyCount < UNBOUNCE_CNT) S2KeyCount++;
04      else {
05        S2KeyPressed = 1;
06        S2KeyCount = 0;
07              return (1);
08      }
09    }
10  }
11  else {
12    if (!((GPIOA->IDR & S2) == 0 )) {             // Check if S2 is not pressed
13      if (S2KeyCount < UNBOUNCE_CNT) S2KeyCount++;
14      else {
15        S2KeyPressed = 0;
16        S2KeyCount = 0;
17      }
18    }
19  }
20  return (0);
   }
<pre/>

When I simulate the code, it enters the function on line 01 then jump to line 03, then jump back to line 02, then again to line 03, then back to line 02, and then directly jumps to line 12 then 20 and exit that function.
It looks like it doesn't matter what is inside the if/else statement because it always does the same with the exact sequence (01, 03, 02, 03, 02, 12), No matter whether I replace the code of the outer if<i/> from line 02 to 09 with the one of the else<i/> part (from line 12 to 18).

Now the weird thing is that I have copied that function from the ~Keil\Boards\MCBSTM32\GPIO\GPIO.uvprog
When I simulate the latter program it runs just fine without any problems!

Can anyone give me any hint one how to move to solve this issue!
Any help would be appreciated


Parents
  • Do not (!) post your own text as part of any code section. See how silly it looks with mega long text lines...

    Didn't preview indicate the problem with line length?

    By the way - you sure you copied the code ok.

    There is an if() statement that is true if a variable is set - inside that if statement that same variable may be set. If variable is not set, ou reach the else part. And the else part may clear the variable.

    Shouldn't it be reversed? If variable _isn't_ set, then enter first part of if statement and count up until setting variable as set. And if variable _is_ set, enter else side of statement and count time until clearing variable?

Reply
  • Do not (!) post your own text as part of any code section. See how silly it looks with mega long text lines...

    Didn't preview indicate the problem with line length?

    By the way - you sure you copied the code ok.

    There is an if() statement that is true if a variable is set - inside that if statement that same variable may be set. If variable is not set, ou reach the else part. And the else part may clear the variable.

    Shouldn't it be reversed? If variable _isn't_ set, then enter first part of if statement and count up until setting variable as set. And if variable _is_ set, enter else side of statement and count time until clearing variable?

Children
  • I guess it was an accident: note that (s)he tried to close the code section with <pre/> - but the slash should have been before the "pre"

    So, at least some marks for trying!

    "Didn't preview indicate..."

    Yes, it should have been noticeable in the preview

  • Thank you for your kind help!

    I have set the optimization level to 0 and everything is working as normal again.

    Sorry for the silly format, but it was my mistake, I clicked post instead of preview, So I couldn't fix it when I saw it :)

    Thank you again for your help!

  • Wait!

    What you really must understand is that what you were seeing before is perfectly normal!

    This is fundamental to the concept of high-level languages!

  • But it still seems there is a copy/paste problem somewhere. Unless my eyes fools me with your indent, the code says:

    int S2Pressed (void) {
        static int S2KeyPressed = 0;
    
        if (S2KeyPressed) {
           if (debounced_press()) {
               S2KeyPressed = 1;
               return TRUE;
            }
        }  else {
            if (debounced_release()) {
                S2KeyPressed = 0;
                return FALSE;
            }
        }
        return FALSE;
    }
    

    How will the code ever set S2KeyPressed, if the requirement for setting it is that it is already non-zero?

  • Thank you Andy for your reply!

    I got your point, but I think I should read more about how HLL simulation is done, so I can understand how to debug a code like this one.

  • I have posted the manipulated code, where I have switched between the outer if and else statement just to check if that have any impact on the execution sequence.

    Following is the original code:

    int S2Pressed (void) {
      static int S2KeyCount = 0, S2KeyPressed = 0;
    
      if (S2KeyPressed) {
        if (!((GPIOA->IDR & S2) == 0 )) {             // Check if S2 is not pressed
          if (S2KeyCount < UNBOUNCE_CNT) S2KeyCount++;
          else {
            S2KeyPressed = 0;
            S2KeyCount = 0;
          }
        }
      }
      else {
        if (((GPIOA->IDR & S2) == 0 ))  {             // Check if S2 is pressed
          if (S2KeyCount < UNBOUNCE_CNT) S2KeyCount++;
          else {
            S2KeyPressed = 1;
            S2KeyCount = 0;
                    return (1);
          }
        }
      }
      return (0);
    }
    

    Anyway the logic of the code wasn't my problem. But thank you for your feedback :)

  • The Keil simulator does not simulate the HLL - it just simulates the execution of the machine (Lowest-level) instructions.

    That's why stepping in the debugger (whether in simulation or connected to a real target) often doesn't tie-up with the HLL source lines.