PDA

View Full Version : Access pin using index



xnihilo
- 9th October 2008, 14:42
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:



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:



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?

Sharky
- 9th October 2008, 16:40
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



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 :o

arniepj
- 9th October 2008, 17:02
I believe this document was off this forum.It describes indexing port bits.

xnihilo
- 9th October 2008, 17:21
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



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 :o

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.

sinoteq
- 9th October 2008, 17:38
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

sheepdog
- 9th October 2008, 19:06
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

victorf57
- 10th October 2008, 01:34
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

xnihilo
- 13th October 2008, 07:32
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.

xnihilo
- 13th October 2008, 07:42
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

skimask
- 13th October 2008, 08:22
That's not nice but it works
This may not be any nicer, and I'm not even sure if it'll work...but...



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

xnihilo
- 13th October 2008, 22:13
This may not be any nicer, and I'm not even sure if it'll work...but...



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.

sheepdog
- 16th October 2008, 00:43
You have substituted arrogance for intelligence
Time enough
nostromo signing off

skimask
- 16th October 2008, 02:34
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.