Access pin using index


+ Reply to Thread
Results 1 to 13 of 13
  1. #1
    xnihilo's Avatar
    xnihilo Guest

    Question Access pin using index

    I know I already opened a post for this but after reading it again some month later, I could still not find the answer.

    In the first step I need to know what pin triggered an interrupt, so I'm using:

    Code:
    if (porta.0 == 0) THEN
        pin = 0             'set the number of the pin for the score managment (update_scores)
        GOTO got_pin        'bypass 
    ENDIF
    if (porta.1 == 0) THEN
        pin = 1
        GOTO got_pin        'bypass 
    ENDIF
    if (porta.2 == 0) THEN
        pin = 2
        GOTO got_pin        'bypass 
    ENDIF
    if (porta.3 == 0) THEN
        pin = 3
        GOTO got_pin        'bypass 
    ENDIF
    if (porta.4 == 0) THEN
        pin = 4
        GOTO got_pin        'bypass 
    ENDIF
    'don't check A.5 which is an output
    got_pin:
    In the second step I have to read the pin that changed, logicaly it would be:

    Code:
    WHILE (PORTA.0(pin) = 0)
    ...
    But id does not work.
    I don't want to use the same code for all 5 porta pins I'm using.
    Isn't there a way to access pins using an index variable?

  2. #2
    Join Date
    Mar 2005
    Location
    Denmark
    Posts
    31

    Default

    I am not really sure that I understand your question.

    I assume that you have an change on PORTA interrupt and that you want to know which of PORTA pins that triggered your interrupt.

    Maybe you could as the first thing in your interrupt handler read the lower 5 bits of PORTA to see there status.

    If I remember it right (don't have my manual here at work)
    You can isolate the lower 5 bits and put them into a variable

    Code:
    youvar = PORTA & $1f    ' put lower 5 bits of PORTA into youvar
    Then if PORTA.3 is low and the others are high you will get the value 23 in youvar

    Hope I remember it right
    *Let's do it*

  3. #3
    Join Date
    Aug 2005
    Posts
    57

    Default

    I believe this document was off this forum.It describes indexing port bits.
    Attached Files Attached Files

  4. #4
    xnihilo's Avatar
    xnihilo Guest

    Default

    Quote Originally Posted by Sharky View Post
    I am not really sure that I understand your question.

    I assume that you have an change on PORTA interrupt and that you want to know which of PORTA pins that triggered your interrupt.

    Maybe you could as the first thing in your interrupt handler read the lower 5 bits of PORTA to see there status.

    If I remember it right (don't have my manual here at work)
    You can isolate the lower 5 bits and put them into a variable

    Code:
    youvar = PORTA & $1f    ' put lower 5 bits of PORTA into youvar
    Then if PORTA.3 is low and the others are high you will get the value 23 in youvar

    Hope I remember it right
    No, I know what pin triggered the int. I jut can write a routine that will read the incoming data on the specific pin as I don't know at the time I write he routine what pin it will be so I have to use an index but there is no way I can make it work.

  5. #5
    sinoteq's Avatar
    sinoteq Guest

    Default Using index and pins

    Hi
    Indexing pins is not a totally safe thing to do with PBP. This is a "Extra feature" and works really great with some commands and not at all with others. I have had some really strange results using indexed pins and Pulsin. It would not be a big surprise if While was a no-no.

    One other thing, please forgive me if I am wrong, are you sure you are using Interupt? When I look at your code. Unless it is in the interupt handler I would say you are polling the pin to get the status and generate a "software interupt". IF this is the case there are more efficient ways to do that is much faster and will save a ton of codespace.

    if (porta.0 == 0) THEN
    pin = 0 'set the number of the pin for the score managment (update_scores)
    GOTO got_pin 'bypass
    ENDIF

  6. #6
    sheepdog's Avatar
    sheepdog Guest

    Default

    Will it work via a pointer
    SYMBOL PORT_PIN = PORTA

    while PORT_PIN.0[pin] = 0
    Worth a try I guess, unable to test.

    or a loop of
    mybyte = portA
    while mybyte.0(pin) = 0

    By the way re: "You are right, bit shuffling and time wasting"
    MOVE?BB PORTA, _B0
    AND?BCB _B1, 00Fh, _B1
    NCD?BB _B1, _B0
    LABEL?L L00002

    END
    thats the size of the code in assembly using ncd added the "and" to remove msb
    Last edited by sheepdog; - 9th October 2008 at 20:48. Reason: code shuffle

  7. #7
    Join Date
    Oct 2004
    Posts
    19

    Default

    Couldn't we use LOOKUP for this??

    my_var = port A

    Lookup my_var,[0,1,2,3,4,]pin_pressed



    wouldn't pin_presed now hold the pin that was triggered?
    Victor

  8. #8
    xnihilo's Avatar
    xnihilo Guest

    Default

    Quote Originally Posted by sinoteq View Post
    Hi
    Indexing pins is not a totally safe thing to do with PBP. This is a "Extra feature" and works really great with some commands and not at all with others. I have had some really strange results using indexed pins and Pulsin. It would not be a big surprise if While was a no-no.

    One other thing, please forgive me if I am wrong, are you sure you are using Interupt? When I look at your code. Unless it is in the interupt handler I would say you are polling the pin to get the status and generate a "software interupt". IF this is the case there are more efficient ways to do that is much faster and will save a ton of codespace.

    if (porta.0 == 0) THEN
    pin = 0 'set the number of the pin for the score managment (update_scores)
    GOTO got_pin 'bypass
    ENDIF
    Yes, I am using interrupts on PORTA.

  9. #9
    xnihilo's Avatar
    xnihilo Guest

    Default

    Quote Originally Posted by victorf57 View Post
    Couldn't we use LOOKUP for this??

    my_var = port A

    Lookup my_var,[0,1,2,3,4,]pin_pressed



    wouldn't pin_presed now hold the pin that was triggered?
    Victor
    From what is written in the manual:

    "LOOKUP B0,[“Hello!”],B1 ' Get character number B0 from string to variable B1"

    So if you lookup portA then you will have a value equal to the bit rank of the bits that are set. I only need to find the first bit set when reading the PORTA register from LSB to MSB.
    With LOOKUP, if the bits 0 and 2 are set, portA will be = 5, that is not the pin_pressed.
    (It will will return the value of the 5th constant).

    The only way I found till now is to use redundant code like:
    if porta.0 = 0 then
    pin = 0
    Goto bypass
    endif
    if porta.1 = 0 the
    pin = 1
    Goto bypass
    endif
    ...etcetera

    That's not nice but it works

  10. #10
    skimask's Avatar
    skimask Guest

    Default

    Quote Originally Posted by xnihilo View Post
    That's not nice but it works
    This may not be any nicer, and I'm not even sure if it'll work...but...

    Code:
    temp var byte
    pin var byte
    
    pin = 0 : temp = porta     'initial pin to zero and save porta to temp
    loop: pin = pin + 1 : if (temp.0[pin] <> 0) AND (pin <> 4) then loop
    got_pin:    'when you get here, if pin = 5 then porta.0 thru porta.3 <> 0

  11. #11
    xnihilo's Avatar
    xnihilo Guest

    Default

    Quote Originally Posted by skimask View Post
    This may not be any nicer, and I'm not even sure if it'll work...but...

    Code:
    temp var byte
    pin var byte
    
    pin = 0 : temp = porta     'initial pin to zero and save porta to temp
    loop: pin = pin + 1 : if (temp.0[pin] <> 0) AND (pin <> 4) then loop
    got_pin:    'when you get here, if pin = 5 then porta.0 thru porta.3 <> 0
    This seems to be the way to index the pin registers.
    In fact before checking the replies on the forum (and your reply) I've tried the tutorial from BRUCE where he is using:
    SYMBOL TRIGGERED_PIN = PORTA

    Then

    IF (TRIGGERED_PIN.0[index] == 0) THEN
    ...

    and it worked fine I'm very happy.

  12. #12
    sheepdog's Avatar
    sheepdog Guest

    Default

    You have substituted arrogance for intelligence
    Time enough
    nostromo signing off

  13. #13
    skimask's Avatar
    skimask Guest

    Default

    Quote Originally Posted by sheepdog View Post
    You have substituted arrogance for intelligence
    Time enough
    nostromo signing off
    Even I disagree with that statement...(who would've thunk that eh? )

    I think he substituted something that you think MIGHT have worked for him with something that DID work for him.

Similar Threads

  1. Is this a K Type sensor?
    By jessey in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 21st November 2009, 14:55
  2. DS1820 with 16f688
    By jessey in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 23rd May 2009, 06:07
  3. Advice-scrutiny for my serial controller
    By kevlar129bp in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 13th December 2008, 18:11
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 18:27
  5. Another RTC, DS1287
    By DavidK in forum Code Examples
    Replies: 0
    Last Post: - 12th December 2006, 18:07

Posting Permissions

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