Proton Tutorial - Switch


User interaction comes in a variety of methods, none simpler then the humble switch. Checking the status of Pins is very simple, and this basic switch example demonstrates it.

The 10K (Could be any large value) resistor is a "Pull-Down" resistor, and keeps the Pin tied to earth (0v) should the switch be open (contacts not touching). If it was not in place, the input Pin (After having a logic High value) would have residual voltage remaining, as its impedance is very high, and would falsely interpret the Pin as still having a logic High state. With the resistor in place, its brought back to 0v immediately after any switching, also known as "tied to earth."


Code Example

Switch Program
Device = 16F877 ' Specify What PIC your using  XTAL = 4 ' The Crystal/Osc Frequency  
Symbol Switch = PORTA.0
TRISA.0 = 1 ' Config PORTA.0 as an input 
 If Switch = 1 Then
 'Do what ever you want if the switch is closed  Else
 'Do what ever you want if the switch is opened  EndIf
 GoTo Main


Forum Activity

Member Access