Spin: assigning a LONG into a BYTE
Basearsa
Posts: 5
Update: The code as supplied here actually does produce a blinking light.
This is my first forum post. I recently bought a Propeller, and have been having fun making LEDs blink.
Today I attempted something like the following, and was surprised that my LED did not turn on:
I eventually learned that my repeat block was never finishing. Changing my byte array to a long array resulted in expected output, as did replacing "True" with a number such as 1.
What is happening here? Is the propeller halting execution? It makes sense that a True being 0xffffffff cannot fit into a byte. I tried wrapping my repeat loop in a method and using the "\method" syntax to trap any implicit ABORT that might be happening, but that did not work. Perhaps I am not using the abort facilities correctly.
I suppose in this case I'll chose some other value to represent a TRUE and FALSE here, and then remember not to do a direct comparison with the real TRUE/FALSE constants later. Is there a more recommended approach to storing boolean values in a BYTE?
This is my first forum post. I recently bought a Propeller, and have been having fun making LEDs blink.
Today I attempted something like the following, and was surprised that my LED did not turn on:
VAR byte _elements[10] PUB Main repeat i from 0 to 9 _elements[i] := True dira[0]~~ outa[0]~~ repeat
I eventually learned that my repeat block was never finishing. Changing my byte array to a long array resulted in expected output, as did replacing "True" with a number such as 1.
What is happening here? Is the propeller halting execution? It makes sense that a True being 0xffffffff cannot fit into a byte. I tried wrapping my repeat loop in a method and using the "\method" syntax to trap any implicit ABORT that might be happening, but that did not work. Perhaps I am not using the abort facilities correctly.
I suppose in this case I'll chose some other value to represent a TRUE and FALSE here, and then remember not to do a direct comparison with the real TRUE/FALSE constants later. Is there a more recommended approach to storing boolean values in a BYTE?
Comments
I always use 0 for false and 1 for true. Why is -1 true anyway? I'm sure there is a good reason, but it seems kind of strange.
All bits are set. False is $00000000, True is $FFFFFFFF
In Spin, any non-zero value will evaluate as true.
In normal language "not true" is the same as "false" and "not false" is the same as "true".
Let's say you only have one bit you have that "false" is represented by 0. Then for "true" we have "not false" or ~0 which is 1.
When you have 32 bits of 0s for false then true becomes !0 which is 32 bits of 1s. Or $FFFFFFFF has hexadecimal.
It just so happens that $FFFFFFFF in decimal is -1.
Not so strange after all.
Problems can arise when comparing values against true. That's because Spin treats any non-zero value as true in conditional statements. So things like 3 are also true even if the don't equal the defined value of true.
With a variable i declared, the example works fine. I have only LEDs at pin 16..23, so I have changed the pin number.
Andy
When I attempt to assign TRUE to a byte value to my repeat loop never exits. Truncation is what I was expecting to happen.
Code?
The code I provided was a simplification of what I'm trying to do. Indeed, when I plug in this exact example into the IDE I get the expected behavior.
It seems that when I write TRUE to an element of a BYTE array a LONG value is written to memory starting from the address of the BYTE.
Here is an example which I have tested:
Main.spin
BoundedBuffer.spin
The code as it is written does not produce a lit LED. When I modify the VAR block in BoundedBuffer.spin such that _elements is declared after _index the light blinks. Am I correct that I must be overwriting memory?