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
  • 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 :)

Reply
  • 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 :)

Children
No data