A BCD Switch is very simple... in it's popular form it has a common followed by four pins... A, B, C, D, or 1, 2, 4, 8.

Let's use PORTB...

Wire A (or 1) pin to PORTB.0
Wire B (or 2) pin to PORTB.1
Wire C (or 4) pin to PORTB.2
Wire D (or 8) pin to PORTB.3
Wire the Common pin to Vss (0v Ground).

The switch will pull-DOWN the relevant pins for the setting you require, so we need pull-up Resistors. The easiest way is to enable the internal weak pull-up's for PORTB... so at the beginning of your code put (see the Datasheet for your chosen PIC)...

OPTION_REG.7=0

Also you need to enable the port for input, so again at the beginning of your code you need to put...

TRISB=$FF

actually only the last four bits (bits 3 to 0) need to be configured for input, but my example set the whole port (all eight bits) that way.

Finally, to read the whole port into a previously defined byte variable, within your program execute...

Myvar=PORTB

This will read the WHOLE port into variable Myvar, so to isolate just the lower four bits (PBP manual section 4.17) should the upper four bits be getting in the way...

Myvar=Myvar & $0F

Finally, remember that your answer will be INVERTED because in our example we are PULLING-DOWN, so $0F will be all pins OFF, whilst $00 will be all pins ON. To invert the other way (again PBP manual 4.17)...

Myvar=Myvar ^ $0F

Novice, newbie or not, you can build your own truth table once you have wired and played to discover what pins pull up or down depending on your switch position... it will be a better learning excercise for homework than having me tell you here.

Melanie