Determine if a pullup is on a PIN or if it is floating
ke4pjw
Posts: 1,231
I did something like this on the P1, per @"Phil Pilgrim (PhiPi)" , to detect if a pin had a pullup on it, or if it was floating. It works incredibly reliably.
Can this also be done on the P2? I don't know if PINF is a FLTL or FLTH.
PINL(55)
PINF(55)
if PINR(55) == 1
' Pin has a pullup

Comments
If you're looking for an external pull-up, you may want to use pinclear() so that any internal connections are removed.
So something like this?
PINL(55) PINCLEAR(55) if PINR(55) == 1 ' Pin has a pullupI am going to give this a swing tonight. Thanks!
I tend to be cautious, so I might do this:
An extra cautiousness would be to have a known delay between the float and the pin read. Then a faster execution won't inadvertently sample before the pull-up crosses the threshold.
I had to use float low. I suspect pinfloat is the same at FLTH.
Thanks guys!
PUB HardwareVer() : r PINCLEAR(ENC_P) org DRVH #ENC_P FLTL #ENC_P DRVL #ENC_P FLTL #ENC_P end waitus(1) debug(udec_(PINREAD(ENC_P))) if PINREAD(ENC_P) == 1 ' Pin has a pullup debug("Version 2 hardware") r := 2 else debug("Version 1 hardware") r := 1 return rWhy is that DRVH there? Did it help in some way?
Another possibility on the P2 is to enable a weak internal pulldown resistor and then read the input. Any external pullup lower then the internal pulldown will pull the input high. This doesn't depend on timing.
wrpin(PIN, P_LOW_150K) 'enable internal pulldown of 150k pinl(PIN) if pinr(PIN) 'an external pullup will pull it high 'there is a pullup pinclear(PIN) 'disable internal pulldownAndy
Hehe, one step further is use the compDAC pin mode to measure a floating ADC - https://forums.parallax.com/discussion/174441/measuring-floating-pin-voltage-adc-from-just-the-built-in-comparator
wrpin(PIN, P_LOW_150K) 'enable internal pulldown of 150k pinl(PIN) if sar_reading(PIN) > 5 'an external pullup will pull it high 'there is a pullup pinclear(PIN) 'disable internal pulldownThis will work well with even very high pull-up resistances.
Thanks guys, all around! What I have works but I will keep these in my back pocket. I am using a 100K pullup.
Oops, the pin mode above doesn't combine with the SAR routine. CompDAC pin mode can do a 1k5 pull-down though. So still a possible approach there,
You know, I'm gonna add this to the SD driver code. The confusion that occurs if the pull-ups aren't in place are worth the extra code to check for them. Good reminder Terry. Thanks.