Author Topic: need explanation of this little code step by step  (Read 1662 times)

0 Members and 1 Guest are viewing this topic.

Offline ect_09Topic starter

  • Contributor
  • Posts: 17
need explanation of this little code step by step
« on: November 14, 2014, 10:22:50 am »
Code: [Select]
do
{ if (Button(&Switch_Port, Switch_Pin, Debounce_Time, 0)) {
 if (!Switch) { LED = ~LED; }
while (!Switch); // Wait for release of the button } }
 while(1); // Infinite Loop }

code is taken from this link
http://embedded-lab.com/blog/?p=164
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: need explanation of this little code step by step
« Reply #1 on: November 14, 2014, 10:33:29 am »
do{
 if (Button(&Switch_Port, Switch_Pin, Debounce_Time, 0)) {
  if (!Switch) {
   LED = ~LED;
  }
  while (!Switch); // Wait for release of the button
 }
}
while(1); // Infinite Loop
}


Does not compile.
But it should toggle the led is the result of Button() is true and Switch is false. It will continue to the infinite loop when Switch is/becomes true.
 

Offline briselec

  • Regular Contributor
  • *
  • Posts: 94
  • Country: au
Re: need explanation of this little code step by step
« Reply #2 on: November 14, 2014, 11:33:56 am »
Code: [Select]
do{
        if (Button(&Switch_Port, Switch_Pin, Debounce_Time, 0)) {     // calls a library function that debounces  the button press
                if (!Switch) {                   // if Switch is equal to 0.
                        LED = ~LED;        // bitwise negation. If the Switch is equal to 0 and the LED is off it becomes on, on becomes off.
                }
               while (!Switch);            //an empty while loop. execution is stuck here till Switch is not equal to 0.
        }
 } while(1);    // Infinite Loop }       // 1 is always 1 so we're stuck in a continuous do..while loop.

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf