How to scan and differentiate multiple switches through a single input/output pin

embeddedmicrocontrollerswitchestimer

I was reading through the document tips and tricks using 8 bit controllers by microchip and found this diagram.I have seen this in application in some electronic circuits over internet but never found a satisfactory explanation on how the four switches were differentiated from one another.

The three switches were connected to a single I/O pin of micro controller.

Multiple switch sense through a single input
The document says:

A timer is used with a comparator or changing digital input to measure
the capacitor charge time. The charge time is used to determine which
button is pressed.

Software sequence:
1. Configure GP2 to output a low voltage to
discharge capacitor through I/O resistor.
2. Configure GP2 as one comparator input and
CVref as the other.
3. Use a timer to measure when the comparator
trips. If the time measured is greater than the
maximum allowed time, then repeat;
otherwise determine which button is pressed.
When a key is pressed, the voltage divider
network changes the RC ramp rate. 

can anybody help me to understand this method in a lucid manner.I'm not sure about how to do away with this and differentiate the switches.I'm sure this will be of help to some one working to limit the use of GPIO pins.

If any other alternative ways are available all options and suggestions are whole heatedly welcomed.

Thank you
With Regards,

Rookie

Best Answer

The principle in this technique is not so exact and reliable.

In this technique the mcu estimate how large is the discharging time. A timer is clocked, capacitor began to discharge, comparator is in the match voltage interrupt. That means when the voltage of input pin is equal or bigger than a specific voltage(that is defined in program, e.g. internal 2.56V) an interrupt will arise. Now in interrupt routine you can measure timer/counter value.

It's time is proportional to RC value that for equal C it means proportional to R. we can estimate the value of resistance. then you can guess which key is down.

A better and more reliable way is to use an ADC pin to attach multiple Buttons. A good explanation is formed here : http://www.edn.com/design/analog/4439796/Read-multiple-switches-using-ADC

Basic Rule of this technique is described in below picture :

enter image description here

Different switches, will produce different voltage on pins.

Main Advantage of this technique is that your buttons can pushed together simultaneously so the overall voltage is the equivalent voltage of overall resistance. You should select good resistor values to have different voltages for different switches pushed.

And at last another easy way to connect multiple switches is to use a decoder IC like 74148. for example connecting 16 button to 4 pins.