Programming the PIC Microcontroller with MBasic

Chapter 6: Reading Complex Input Switches

Chapter 4 looked at reading individual switches and a matrix keypad. We'll extend our switch reading to look at additional pin saving techniques. We'll also see how rotary encoders both internally decoded and incremental versions can be read with a PIC.

Pin Saving Techniques

It's a common requirement to read a configuration switch during start-up. Perhaps the switch defines the baud rate for a serial output, or it may set some other parameter. Regardless, to dedicate several pins to sense a start-up configuration is a waste of scarce pin resources and may force us to use a larger, more expensive PIC than otherwise desired.

Simple Approach

How might we read a 16-state switch? We'll immediately discard hooking 16 individual switches to 16 input pins. We recall that 4-bits give us 16 unique combinations, so we can use four switch contacts to generate 16 states. This might be either a 4-section DIP switch with individual switch selections, or it might be a rotary DIP switch with positions 0 F internally coded into one common connection and four switched poles, such as the simple connection is shown in Figure 6-1. A closed switch places a high on the corresponding input pin, and an open switch places a low on the corresponding pin. Reading RB7 RB4 as PortB.HighNib, the switch sections correspond to the 4 bits in the nibble.

To determine the switch closure pattern corresponding to any particular value, we look at the binary combinations. For example, if PortB.HighNib is...

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Keylock Switches
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.