16F Switch Example

Checking the status of Pins is very simple, and this basic
switch
program 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."

Note the PIC's power supply/oscillator are not shown
Code example:
DEVICE = 16F877 ' Specify What PIC your using
XTAL = 4 ' The Crystal/Osc Frequency
Symbol Switch = PortA.0
ALL_DIGITAL = True
TRISA.0 = 1 ' Config PORTA.0 as an input
Main:
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
Where you can get
the components;

 | Site Tutorial Index |
|  | 16F PIC Examples |
| |  | Motors |
| |  | Thermometers |
| |  | Code Snippets |
|  | 18F PIC Examples |
|  | Handy Tips |