Trying to learn spin
Beavis3215
Posts: 229
in Propeller 1
I cant seem to figure out conditional branching. Using outa[9..4] tied to leds, and ina[23..21] tied to pushbuttons, I am trying to make a 3 button calculator just for educational purposes.
PUB PBCount
dira[9..4]~~
dira[23..21] := %000
outa[9..4] := %000000
repeat while flag == 0
if ina[21] == 1
outa[9..4] := outa[9..4] + 1
waitcnt(clkfreq/2 + cnt)
if ina[22] == 1
flag := 1
a := outa[9..4]
outa[9..4] := %000000
repeat
if ina[21] == 1
outa[9..4] := outa[9..4] + 1
waitcnt(clkfreq/2 + cnt)
if ina[23] == 1
b := a + outa[9..4]
outa[9..4] := b
This code does not do what I want.
What I want it to do is by pressing singly or holding the first button, it increments outa. This enters the first operand.
After depressing the second button it saves outa in variable a, and clears outa. This is supposed to be like the "+" button.
Then the first button is used to increment outa again. This enters the second operand.
After the third button is presses it adds a to outa for the result. This is like the "=" button.
I would like it to clear and start again by pressing the first button again, which will start entering the first operand again.
I am aware that there will be an overflow problem with bigger numbers, I am just trying to learn the structure of the indents and conditions to make
this possible in spin so that I can figure it out.
Please help
Thanks
PUB PBCount
dira[9..4]~~
dira[23..21] := %000
outa[9..4] := %000000
repeat while flag == 0
if ina[21] == 1
outa[9..4] := outa[9..4] + 1
waitcnt(clkfreq/2 + cnt)
if ina[22] == 1
flag := 1
a := outa[9..4]
outa[9..4] := %000000
repeat
if ina[21] == 1
outa[9..4] := outa[9..4] + 1
waitcnt(clkfreq/2 + cnt)
if ina[23] == 1
b := a + outa[9..4]
outa[9..4] := b
This code does not do what I want.
What I want it to do is by pressing singly or holding the first button, it increments outa. This enters the first operand.
After depressing the second button it saves outa in variable a, and clears outa. This is supposed to be like the "+" button.
Then the first button is used to increment outa again. This enters the second operand.
After the third button is presses it adds a to outa for the result. This is like the "=" button.
I would like it to clear and start again by pressing the first button again, which will start entering the first operand again.
I am aware that there will be an overflow problem with bigger numbers, I am just trying to learn the structure of the indents and conditions to make
this possible in spin so that I can figure it out.
Please help
Thanks
Comments
<pre><code>PUB PBCount dira[9..4]~~</code></pre>
Which will in turn will look like this
thanks
I don't see a pullup resistor on the button.
The side of the button which connects with the Propeller should have a 10k resistor connecting it to 3.3V. As it is now, the pin looks like it's floating which will give very unpredictable results.
Your code doesn't have much of a chance of working with the present hardware. I'll compare your code with the flowchart to see if the Spin is correct.