need explanation of this little code step by step

Discussion in 'Programmer's Corner' started by ect_09, Nov 14, 2014.

  1. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    Code (Text):
    1. do
    2. { if (Button(&Switch_Port, Switch_Pin, Debounce_Time, 0)) {
    3.  if (!Switch) { LED = ~LED; }
    4. while (!Switch); // Wait for release of the button } }
    5.  while(1); // Infinite Loop }
    code is taken from this link http://embedded-lab.com/blog/?p=164
     
  2. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,796
    831
    It looks like the code bit you posted got truncated. No worries, as you posted the link.

    LED, Switch_port,switch_pin,Debounce_time and Switch are defined earlier in the program. Switch_port is the port to which the switch is connected; Switch_pin is the specific pin debounce time is used in the library function "Bounce". Switch is the position/pin of the switch. LED is the position of the LED. The code bit operates as follows.

    • Debounce the switch
    • If the switch is pressed, ~Switch, toggle the state of the LED. Note that when the switch is pressed, it sends a 0 to the pin. Hence, we test for the inverse of switch.
    • Wait for the switch to be released.
    • Repeat (forever)

    Any questions?
     
    ect_09 likes this.
  3. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    thanks for answering,
    can you explain this line???
    Code (Text):
    1. if (Button(&Switch_Port, Switch_Pin, Debounce_Time, 0))
     
  4. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,796
    831
    In the link you provided, there is a detailed description. "Bounce" is a library function; supplied as part if the language for the specific purpose of debouncing the switch. It is represented by the first line of my explanation.
     
    Last edited: Nov 14, 2014
    ect_09 likes this.
Loading...