Menu

OK I give up how do you detect a ground

Help
2008-03-26
2013-05-30
  • Nobody/Anonymous

    A powered circuit detection is easy but I have been pouring though various sources I found witch I know use this feature and cannot seem to figure it out. I am a beginner and this seems to be semi advanced.

    How do you detect if a I/O pin is connected to a ground.?

     
    • Nobody/Anonymous

      Your selected pin should have a pull-up resistor (I *think* GCBASIC switches the internal soft pull-ups on by default but it is a good idea to use about 10k anyway).

      dir PORTB.0 in                     'make sure that your pin is an input

      if PORTB.0=0 then set PORTA.0 on   'switch on an output (we have to do something!)

      'works for me. You can use PORTB=1 to check that a pin is "high".

      if PORTB.0=1 then set PORTA.0 off

      Obviously, change the port & bit to suit your application!

      Mick

       
    • Nobody/Anonymous

      Thanks, now I understand.

       
    • Nobody/Anonymous

      I have a follow up question.
      I can make an I/O pin funtion as a ground right?

      like this?

        dir GPIO.1 in
        GPIO.1 = 0

      or would it need to be an output?

       
    • Nobody/Anonymous

      Yes, it has to be an output. While it's set as an input it has a high resistance to ground (and can be "floating" if the pull-ups are switched off). You would use:

      dir GPIO.1 out
      set GPIO.1 off   ;this would pull the output low

      set GPIO.1 on    ;this would pull the output up to 5v

      Note that there is a limit of how much current you can source or sink with an output pin (and a limit for the chip as a whole - you can't put a 20mA LED on every pin of a 16F84 and light them all at once, for example as it exceeds the maximum current limit for the chip, even though each pin can stand 25mA).

      Another thing; if you have driven an output low as above then you shouldn't try to read the pin back to see if it has gone low. The PIC chip doesn't work like that. Set a variable on when you set the pin on and set it off again when you set the pin off. Then you can always read the variable to discover the state of the pin.

      Mick

       
    • Nobody/Anonymous

      so it's not good to check an output to see if it's on or off?

       
    • Nobody/Anonymous

      Nope, not really. It depends on what the load is. A heavily loaded pin may read the wrong way. The pin is driven from a non-inverting buffer. It is read via a different buffer. You may well be driving the output buffer high, but if the load is pulling the pin down then reading the input buffer may give indeterminate results. It's safer to just use a variable. OTOH, if the pin is lightly loaded you can read from it with no problem.

      Note that TRIS (dir in GCBASIC) disables the output buffer, but not the input buffer. That means that you *can* read from an output pin (even if it could be unreliable) but you can't output to an input pin.

      Mick

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.