Shop OBEX P1 Docs P2 Docs Learn Events
HELP: Question regarding: IncrementUntilCondition.spin — Parallax Forums

HELP: Question regarding: IncrementUntilCondition.spin

danstockdanstock Posts: 2
edited 2013-11-25 05:41 in Propeller 1
I am new to .spin programming. I am using the QuickStart board and uploading the examples but having some mixed results using the IncrementUntilCondition.spin . The program seems to continually count(flash) changing the conditional value to anything >than 7. If I use a value less than 8, the program seems to work as expected. Using a value of 8 or higher, it continues to flash the outputs. What am I missing?? see the code below.
''
'' File: IncrementUntilCondition.spin
PUB BlinkLeds
dira[16..18]~~
repeat until ++outa[16..18]==8
waitcnt(clkfreq/2+ cnt)
repeat

Comments

  • Cluso99Cluso99 Posts: 18,069
    edited 2013-11-24 17:27
    danstock: Welcome. You would be better reposting this in the Propeller 1 thread and use the code tags to include your code (it retains the indentation required).

    the value outa[16..18] is 3 bits for values of 0-7. It will never get to 8.
  • MagIO2MagIO2 Posts: 2,243
    edited 2013-11-25 05:41
    Welcome to the forum!

    Cluso99 already explained the problem, so what is the fix? I would opt for
    repeat until ++outa[16..18]==0
      waitcnt( clkfreq/2+cnt)
    

    When going into the loop outa[16..18] is 0. As it is pre-incremented, it will be 1 when comparison takes place.
    When outa[16..18] is 7 the pre-increment will make it 0 again.

    Another solution would be
    repeat 8
      outa[16..18]++
      waitcnt(clkfreq/2+cnt)
    

    repeat 8 will make the LEDs being off after the loop, repeat 7 would leave them on.
    Please be aware, both rely on the fact that the going in situation is that those three outa bits are really 0, otherwise you won't see the full cycle. (same is true for your initial code)

    If you don't want to rely on this fact, you'd better use
    repeat i from 0 to 8
      outa[16..18]:=i
      waitcnt...
    

    Again 8 turns the LEDs off in the last cycle, 7 leaves both LEDs on.

    And what you really should try is using outa[18..16] instead! Nice to know those little differences in SPIN.

    These nice examples really help in understanding you propeller & SPIN & PASM much better.
Sign In or Register to comment.