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;

Skip Navigation Links.

Collapse Site Tutorial IndexSite Tutorial Index
Collapse 16F PIC Examples16F PIC Examples
LED's
Switches
Keypads
LCD's
7 Seg Displays
ADC
DAC
Expand MotorsMotors
Expand ThermometersThermometers
DS1307 RTC Chip
DS275
EEPROM's
RF Modules
RGB LED's
Code Snippets
Expand Code SnippetsCode Snippets
Expand 18F PIC Examples18F PIC Examples
Expand Handy TipsHandy Tips