Help with outa[ ]
mynet43
Posts: 644
I solved a problem with outa[ ]. But I don't know why my original solution didn't work.
Here's the sample code:
Can someone help me understand why the first program doesn't work and the second one does work?
It looks to me like they should both be doing the same thing.
Thank you for your help.
Jim
Here's the sample code:
CON LIGHT_RELAY1 = 24 VAR long light_type ' type of bulb being used DAT light_case long 0, %0100, %0011, %0100, %0111 ' truth table logic for each light case PUB outa_test light_type := 4 light_on_1 ' this program does NOT work, it sets the outa to zero light_on_2 ' this program works, it sets the outa to one PRI light_on_1 outa[LIGHT_RELAY1] := (light_case[light_type] & 4) >> 2 PRI light_on_2 | ix1 ix1 := (light_case[light_type] & 4) >> 2 outa[LIGHT_RELAY1] := ix1
Can someone help me understand why the first program doesn't work and the second one does work?
It looks to me like they should both be doing the same thing.
Thank you for your help.
Jim
Comments
Second, when the program ends the main cog stops.
My apologies for making greatly oversimplified sample code.
In fact, I did initialize the dira[ ] for this pin. And the code is part of a large program that does not exit after this call.
My whole point was to ask why one line of code didn't work:
outa[LIGHT_RELAY1] := (light_case[light_type] & 4) >> 2
Here's is a better example of code that should work.
If you call routine outa_test_1, it does not set the pin.
If you call routine outa_test_2, it DOES set the pin.
I think they should be equivalent, but one works, the other doesn't.
Thanks again for your help.
Jim
Sometimes obvious is best:
Once you get things working, then start on code optimization. My friend, Peter, constantly reminds me: don't optimize early. Get it working, first.
Thanks for asking. The real code is controlling three solid state relays to turn on different combinations of high powered spotlights, up to 28V @ 16A.
The earlier example was just trying to simplify the question.
Here's the actual routine in question, before and after the code fix:
Before code. This one did NOT work for LIGHT_RELAY_1:
After. This one works fine for all of them:
Still looking for the answer...
Thanks,
Jim
The logic error is probably elsewhere in the code base...
Maybe post all your code?
I guess I answered my own question.
I'll go back and look again and post if I can't find the problem.
Now I'm really curious.
Thanks!
Jim
This works because your output is a single bit, hence only the LSB of the expression is used.
Are you saying the outa[ ] := var depends only on the state of bit 0 of var?
I didn't know that and it doesn't appear to be in the manual.
So if I set outa[24] := 6, it will set P24 to zero. Is that correct?
If so, it seems like the manual should state that.
Please let me know.
Thanks!
Jim
Only if there is ONE bit specified in the outa parameter field -- as is the case in your program.
Yes.
Page 177, just below the middle of the page (manual 1.2):
Here, DIRA bits 8 through 12 are set to high (like before) but OUTA bits 8, 9, 10, 11 and 12 are set equal to 1, 1, 0, 0, and 1, respectively, making P8, P9 and P12 output high and P10 and P11 output low.
I was referring only to the single pin, outa[24] case.
The reference you gave in the manual is for the multiple pin case.
Is there something in the manual that refers to what you said about the single pin case? Or do you just know it from experience, which is fine?
I just want to be sure this is a general case.
Thank you for your help.
Jim
That statement in the manual explains how the bits work -- from the LSB end -- and applies to any number of bits which can in fact be from 1 to 32. You're going to be very unhappy if you expect every possible combination to be spelled out for you.
Sorry, I wasn't trying to pin you down. I appreciate your help and support.
I'm very happy to be doing what I love to do.
Thanks again,
Jim