Watching for pin High


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    24

    Default Watching for pin High

    I am having trouble getting a program to run If statements when a pin goes high.

    I have a 16f628a for testing using three pins for outputs and four for inputs. Pins PortA.0 - PortA.3 are set low at the first of the program using:

    Low PortA.0 ect.

    The PCB has terminals when shorted send +5v to the inputs pulling them high.

    The main loop is such:

    :main
    If Porta.0 = HIGH then goto example1
    goto main

    When I pull the pin high, it doesn't do anything different. The rest of the pic and program is running fine sending out data when appropriate.

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Re: Watching for pin High

    Are you actually comparing porta.0 to "HIGH"? If so, try changing high to 1. High is a reserved word. You could also just say:
    If porta.0 then example1

    This will be true when the pin is high, and false when it is low.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Watching for pin High

    not familiar with that pic, but RA0 is shared with an analogue comparator, and the pin defaults to analogue so probably worth slapping in an...

    CMCON = 7

    to turn the comparator module off. (http://www.svetelektro.com/upload/16F628A.pdf page 64 upper right box)
    Last edited by HankMcSpank; - 15th October 2011 at 23:41.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Watching for pin High

    What's been said and if you first do LOW PortA.0 the TRIS bit will be cleared so that the pin (RA0 in this case) is an output. When you then do If PortA.0 = 1 you aren't reading the state of the pin since it's not set to be an input.

  5. #5
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: Watching for pin High

    Just to expand on what others have posted...
    Below is a schematic (sorry about the ugly resistors) that shows two different ways of wiring to detect a change on a port pin.

    First... with PortA.0 pulled high and a press on the button will cause a LOW or 0.

    Second... with PortA.1 pulled LOW and a press on the button will cause a HIGH or 1.

    The resistors can be 1K to 10K...


    Name:  2011-10-16_103005.jpg
Views: 386
Size:  8.5 KB

    It makes no difference and is not necessary to pre-set the state of the pin you are trying to test by "make port.pin LOW or HIGH" as that is an OUTPUT function not an INPUT function.

    The correct way is to choose one of the wiring options as illistrated, set the corresponding TRIS BIT for that port.pin to an INPUT or "1" and then test for the opposite state, which will indicate a change or button press.

    Code:
    TrisA= 000011   'set PortA.0 and PortA.1 as input
    
    IF PortA.0=0 THEN 'do something here when PortA.0 changes
    
    IF PortA.1=1 THEN 'do something here when PortA.1 changes
    as shown below... it is also correct to test for a "0" by testing for "NOT" and it is also correct to test for a "1" as shown...

    Code:
    IF NOT PortA.0 then '(this code tests for a "0" or "LOW")
    
    IF PortA.1 THEN ' (this code tests for a "1" or "HIGH"
    I hope this helps...

    PS... if you can not see the schematic image above, click on it.
    Last edited by Heckler; - 16th October 2011 at 17:33.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  6. #6
    Join Date
    Jan 2010
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: Watching for pin High

    Thank you for the help. So far it is still not working(I have not tried everything suggested though). I will try to add the pull down resistors as well. I already have the working PCB but I should be able to drill and add them with some creative soldering.

    I didn't really intend this board to be an input board but figured I would use it for testing while I was ironing everything (output) else out.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts