Shop OBEX P1 Docs P2 Docs Learn Events
Determine if a pullup is on a PIN or if it is floating — Parallax Forums

Determine if a pullup is on a PIN or if it is floating

ke4pjwke4pjw Posts: 1,155
edited 2024-10-29 20:56 in PASM2/Spin2 (P2)

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

  • JonnyMacJonnyMac Posts: 9,101

    If you're looking for an external pull-up, you may want to use pinclear() so that any internal connections are removed.

  • ke4pjwke4pjw Posts: 1,155

    So something like this?

    PINL(55)
    PINCLEAR(55)
    if PINR(55) == 1
        ' Pin has a pullup 
    
    

    I am going to give this a swing tonight. Thanks!

  • JonnyMacJonnyMac Posts: 9,101
    edited 2024-10-29 23:02

    I tend to be cautious, so I might do this:

     pub has_xpullup(pin)
    
      pinclear(pin)
      pinlow(pin)
      pinfloat(pin)
    
      return pinread(pin) == 1
    
  • evanhevanh Posts: 15,905
    edited 2024-10-29 23:30

    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.

     pub has_xpullup(pin)
    
      pinclear(pin)
      pinlow(pin)
      pinfloat(pin)
      waitus(1)
    
      return pinr(pin)
    
  • ke4pjwke4pjw Posts: 1,155

    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 r
    
  • evanhevanh Posts: 15,905

    Why is that DRVH there? Did it help in some way?

  • AribaAriba Posts: 2,690

    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 pulldown
    

    Andy

  • evanhevanh Posts: 15,905
    edited 2024-10-30 13:24

    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 pulldown
    

    This will work well with even very high pull-up resistances.

  • ke4pjwke4pjw Posts: 1,155

    Thanks guys, all around! What I have works but I will keep these in my back pocket. I am using a 100K pullup.

  • evanhevanh Posts: 15,905

    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,

  • evanhevanh Posts: 15,905

    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.

Sign In or Register to comment.