Shop OBEX P1 Docs P2 Docs Learn Events
Am I being really obtuse? — Parallax Forums

Am I being really obtuse?

HughHugh Posts: 362
edited 2015-01-06 08:55 in Propeller 1
I'm having one of those occasions where the apparently obvious doesn't work as I expected. Would someone please be kind enough to point out the problem that I just can't see for staring at it so long?

All I want to do is to set one pin high, the other low.

This code does not work (indicator LEDs stay unlit)
[B]dira[/B][16]~~     ' Set pin 16 to output
[B]outa[/B][16]~      ' Set pin 16 low

[B]dira[/B][17]~~     ' Set pin 17 to output
[B]outa[/B][17]~~    ' Set pin 17 high

This code does work, with the indicator LEDs toggling between lit / unlit at half-second intervals
[B]dira[/B][16]~~     ' Set pin 16 to output
[B]outa[/B][16]~      ' Set pin 16 low

[B]dira[/B][17]~~     ' Set pin 17 to output
[B]outa[/B][17]~~    ' Set pin 17 high

[B]repeat[/B] 
  [B]outa[/B][17] := ![B]outa[/B][17]     ' Toggle pin 17
  [B]outa[/B][16] := ![B]outa[/B][16]     ' Toggle pin 16
  [B]waitcnt[/B]([B]cnt[/B]+4000000)

I have checked this so many times.

Why?!

Comments

  • ErNaErNa Posts: 1,752
    edited 2015-01-06 02:41
    Looks like the LED is in the wrong direction. The working code toggles the outputs, so the state you do NOT expect to shine is shining ;-) At least, that is what it looks like from the distance!

    Yes, but as frida showed: we didn't look to what is not coded ;-)
  • fridafrida Posts: 155
    edited 2015-01-06 03:00
    dira[16]~~     ' Set pin 16 to output
    outa[16]~      ' Set pin 16 low
    
    dira[17]~~     ' Set pin 17 to output
    outa[17]~~    ' Set pin 17 high
    repeat
    

    You need a repeat at the end, else close the propeller down.
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2015-01-06 04:00
    Hugh wrote: »
    I'm having one of those occasions where the apparently obvious doesn't work as I expected. Would someone please be kind enough to point out the problem that I just can't see for staring at it so long?

    All I want to do is to set one pin high, the other low.

    This code does not work (indicator LEDs stay unlit)
    [B]dira[/B][16]~~     ' Set pin 16 to output
    [B]outa[/B][16]~      ' Set pin 16 low
    
    [B]dira[/B][17]~~     ' Set pin 17 to output
    [B]outa[/B][17]~~    ' Set pin 17 high
    

    This code does work, with the indicator LEDs toggling between lit / unlit at half-second intervals
    [B]dira[/B][16]~~     ' Set pin 16 to output
    [B]outa[/B][16]~      ' Set pin 16 low
    
    [B]dira[/B][17]~~     ' Set pin 17 to output
    [B]outa[/B][17]~~    ' Set pin 17 high
    
    [B]repeat[/B] 
      [B]outa[/B][17] := ![B]outa[/B][17]     ' Toggle pin 17
      [B]outa[/B][16] := ![B]outa[/B][16]     ' Toggle pin 16
      [B]waitcnt[/B]([B]cnt[/B]+4000000)
    

    I have checked this so many times.

    Why?!

    The toggle operation does not need an assignment, just use the right side of your code that is in your repeat (which btw is correct)

    !outa[16] will by itself toggle pin 16
  • HughHugh Posts: 362
    edited 2015-01-06 04:29
    Brilliant!

    Thank you
  • ErNaErNa Posts: 1,752
    edited 2015-01-06 04:37
    but then you have to code:
    repeat 
      !outa[17]     ' Toggle pin 17
      !outa[16]     ' Toggle pin 16
      waitcnt(cnt+4000000+ aLittleTime)
    
    to have the toggle frequency not changed ( if the compiler didn't optimize it)
    or:
    ToggleTime : = cnt
    repeat 
      !outa[17]     ' Toggle pin 17
      !outa[16]     ' Toggle pin 16
      waitcnt( ToggleTime )
      ToggleTime +=4000000
    

    You also could code:
    ToggleTime : = cnt
    repeat 
      !outa[16..17]     ' Toggle pin 16 to 17
      waitcnt( ToggleTime )
      ToggleTime +=4000000
    

    At least: I hope it will do ;-)
  • Dave HeinDave Hein Posts: 6,347
    edited 2015-01-06 08:55
    You need to increment ToggleTime before doing the waitcnt or the first loop will wait till the counter wraps, which is 53.7 seconds. You could also do the increment on the same line as the waitcnt, so the code should look like:
    ToggleTime : = cnt
    repeat 
      !outa[16..17]     ' Toggle pin 16 to 17
      waitcnt(ToggleTime += 4000000)
    
Sign In or Register to comment.