need explanation of this little code step by step

ect_09

Joined May 6, 2012
180
Code:
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

djsfantasi

Joined Apr 11, 2010
6,367
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

Joined May 6, 2012
180
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?
if (Button(&Switch_Port, Switch_Pin, Debounce_Time, 0))