accessing ports pins using an index variable


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Code:
    for i = 0 to 255
        IF porta <> 0 then exitloop
    pause 1
    next i
    exitloop:
    if any pin on porta is set, exit the loop

  2. #2
    xnihilo's Avatar
    xnihilo Guest


    Did you find this post helpful? Yes | No

    Smile accessing byte's bits

    Okays, thanks.
    However it does not exactly meet my needs:

    Not all pins will be triggered when they go from High to Low.

    My program gets an IR signal on a sensor connected to a PORTA pin and this triggers an INT. In the int routine, I check what pin received the IR signal and then I poll the pin to get a signal that is encoded.
    On pin RA5, I set no WPU because the default state will be 0V and once 5V gets to this pin, it triggers an INT.

    (I'm developping software and hardware for a lasergame).

    Anyway thank you very much for your help.

  3. #3


    Did you find this post helpful? Yes | No

    Default

    You could bitwise xor the read of port A with a saved previous state. Then changed bits would be set. Then you could use a SELECT CASE to do actions if a specific bit is set


    casetest = portA ^ prev_state

    prev_case = portA

    SELECT CASE casetest
    CASE casetest.BIT0
    ( do stuff for bit 0 changes)
    CASE casetest.BIT1
    (do stuff for bit 1 changes)
    etc, etc.
    Tim Barr

  4. #4
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    If you just need to know which pin changed, you can
    use something like -

    DeltaPortA = OldPortA ^ PortA
    If DeltaPortA > 0 THEN
    OldPortA = PortA
    BitThatChanged = NCD DeltaPortA
    EndIF

    BitThatChanged will have a number from 1-8, corresponding to the bit that changed.
    If more than one changed, the highest-order bit that changed will be in the variable.


    If you need to know ONLY if it went from LOW to HIGH, you can use

    DeltaPortA = OldPortA ^ PortA
    If DeltaPortA > 0 Then
    OldPortA = PortA
    DeltaLowToHigh = DeltaPortA & PortA
    BitThatChangedH = NCD DeltaLowToHigh
    EndIF

    BitThatChangedH will contain the number (1-8) of the highest order bit that changed from low to high.

    You can use the same technique for figuring out which one went from HIGH to LOW.
    Charles Linquist

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  2. How to index Port variable
    By jpadin1 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th February 2010, 10:36
  3. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  4. Accessing pins with a variable
    By Desterline in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 20th August 2004, 16:03
  5. Accessing ports ( Melanie )
    By anj in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 7th March 2004, 20:52

Members who have read this thread : 0

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