What The Heck Is He Talking About??

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
I stumbled across this webpage where I found this snippet from a PIC forum the author suggests for further reading.

• -Remove-madygTakeThisOuT at spamexcite.com asks: " the Pic can see a "1" or a "0" however how can a Pic "see" the middle position of a 3 state dip sw?" James Newton replies: Assuming there is a 1k resistor between the switch and the pin, set the pin as an output, pull it low then immediatly switch back to an input and read the value. If it stayed low, repeat the process but pull it high instead. If it stays where you put it, the switch is in the middle.
This is making use of the pins own internal capacitance and would be more reliable with a small cap between the pin and ground. At that point you basically have an RC circuit such as is used in a sigma-delta A to D converter or the POT command used in the BASIC STAMP.+

No matter how many times I read this I couldn't make any sense of it. Primarily because the OP and the responder are discussing an input state that is verboten in the digital world. As I see it they're discussing floating an input pin, which can be detrimental to CMOS,.. or at the very least cause extremely erratic behavior.

What say you? Am I misconstruing this thread?

Chris
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
14,271
Joined
Nov 17, 2011
Messages
14,271
I'd agree that the OP wants to know whether he can read the "open" state of an input pin.
The responder goes further in advising a pull-up (which would remove the open condition by placing a 1 on the input) and using a sequence of commands to discharge the pin/trace capacitance and looking for the state of the input immediately after turning off the low-output.
I think this technique may work, but the pin wouldn't be open as in the OP's question. It will be pulled up to 1.
I've seen this technique being used with external capacitors and (admittedly) comparator inputs to a timer within the µC to create a very simple A/D converter (e.g. here).
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
That's where I'm a bit fuzzy. Nowhere in the responder's reply did he make the position of the SPDT(CO) switch clear. When he says "make the input pin an output and pull it up or pull it down" I'm assuming he's referring to doing so via instructions. I can't see where he mentions moving the switch position off of its 'CO' position.

Harald, thanks for the link. I'll read through it.

By the way.. The reason I stumbled upon this link is because I want to get more involved with our uC's & Programming section of the forum. I no longer want to be locked in to Picaxe only. ;)

Thanks,
Chris
 

Six_Shooter

Nov 16, 2012
98
Joined
Nov 16, 2012
Messages
98
I agree with your original confusion, I thought the very same thing when I read it.

It sounds like they are trying to turn a digital pin into fuzzy logic. Without reading the rest of the thread to know for sure it sounds like the person asking the question just might not understand how to use pull up/down resistors to set a proper digital pin input.
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Yes, this method will work. It's designed to detect the position of a three-position switch (e.g. an ON-OFF-ON switch) where the changeover contact is connected to one of the end pins in the end positions, or nothing in the centre position.

If the end pins are connected to VCC and 0V, then the changeover contact has one of three states:

Position 1: Changeover contact shorted to VCC
Position 2 (centre): Changeover contact not connected to anything
Position 3: Changeover contact shorted to 0V.

One way to read this state is to connect the changeover contact through a lowish-value resistor (e.g. 1k) to the microcontroller pin, with an R-C circuit (e.g. 100 pF capacitor to 0V, 100k resistor to VCC) connected to one side of that resistor - it doesn't make much difference which side. Probably the microcontroller pin side would be better.

So the I/O pin is normally set to input mode. To read the switch position, you read the state of that pin and save it as state1. Then you drive the pin to the state opposite to the rail that's connected to the resistor in the RC network - in this case, drive the pin low - and set it to an output. This forces the pin to that state, overriding the pullup or pulldown action of the 1k resistor from the switch (if the switch is in position 1 or 3). Then you set the pin back to input mode, wait a short time (e.g. a few NOPs) and read the pin state into state2.

You then have four possibilities for state1 and state2:

state1 low, state2 low: the switch is in position 3 and the 1k resistor is pulling the pin low fairly strongly.

state1 high, state2 high: the switch is in position 1 and the 1k resistor is pulling the I/O pin high fairly strongly. The current from the 1k resistor is enough to charge up the capacitor quickly, during the short delay before the MCU reads the pin the second time, so state2 reads as high.

state1 high, state2 low: the switch is in position 2. On the first read, the 100k resistor had charged up the small capacitor; the switch had no effect on this because its changeover contact doesn't connect to anything. So state1 was high. On the second read, the capacitor had been discharged (when the pin was set to an output and driven low), and the capacitor was still charging up through the 100k resistor. The RC circuit values are chosen so that the capacitor is only slightly charged, and still below the LOW input threshold, when the pin is sampled into state2 after the short delay after the capacitor was discharged.

state1 low, state2 high: shouldn't happen.

Sorry for the painfully long description. Maybe it will help someone who hasn't figured it out for themselves yet.
 
Last edited:

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
From what I gathered the OP is desirous of reading the position of a SPDT switch connected to an input. Normally this would be an elementary task. After all it's easy to read a high (1) or Low (0) on an input. The hitch here is the fact that the switch is a 'center off' model and the OP wants the ability to read this state too.

What the responder seems to be describing relies on the capacitance of an input pin and its ability to temporarily hold a charge. I think it's starting to make sense to me but I'm going to sleep on it before I commit to claiming full understanding.

Chris
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Right. I don't agree with the idea of relying on the input capacitance of the pin itself. I would always use an explicit external capacitor.
 
Top