Shop OBEX P1 Docs P2 Docs Learn Events
Bit Shift - easy one for you :) — Parallax Forums

Bit Shift - easy one for you :)

GeeksGoneBadGeeksGoneBad Posts: 100
edited 2012-08-29 03:40 in Propeller 1
I have this bit shift thing that I'm using - it works and I don't have any problems but I want to make sure I'm doing things the best I can :) bottom line I want to cycle through a simple bit pattern (%1000) twice
  dira[0..3] := %1111
  
  repeat 2
    outa[0..3] := %1000     
    waitcnt(clkfreq/8 + cnt)
     repeat 3
        outa[0..3] >>= 1 
        waitcnt(clkfreq/8 + cnt)
        

Thanks for any tips to make this any better :)

Jamie

Comments

  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-08-28 19:01
    Not better, but different, alternatives...

    rotating a variable past the output window...
    [FONT=courier new][SIZE=1]PUB slide | x
      dira[16..19] := % 1111
      x := % 10001000
      repeat 8
        outa[16..19] := (x ->= 1)   
        waitcnt(clkfreq/8 + cnt)[/SIZE][/FONT]
    


    Using the decode operator, |< ...
    [FONT=courier new][SIZE=1]PUB slide | x
      dira[16..19] := % 1111
      repeat x from 7 to 0
        outa[16..19] := |< (x//4)
        waitcnt(clkfreq/8 + cnt)[/SIZE][/FONT]
    
  • AribaAriba Posts: 2,690
    edited 2012-08-28 22:15
    A bit shorter:
    dira[0..3] := %1111
    
      pattern := %1000_1000
      repeat 8 '2*4
        outa[0..3] := pattern
        waitcnt(clkfreq/8 + cnt)
        pattern >>= 1
    

    Andy
  • JonnyMacJonnyMac Posts: 9,194
    edited 2012-08-28 23:14
    The neat thing about Andy's approach is that it can be extended -- I just ran this on a QS board;
    dira[19..16] := %1111
    
      pattern := %1000_1000_1000_1000_1000_1000_1000_1000
      repeat 48 '12 * 4
        outa[19..16] := pattern
        waitcnt((clkfreq >> 3) + cnt)
        pattern ->= 1
    
  • GeeksGoneBadGeeksGoneBad Posts: 100
    edited 2012-08-29 03:40
    Awesome - thanks for the ideas guys

    The books can tell you what things do, and I can figure out how to do something, but I'm just never sure if there are better ways :)

    Jamie
Sign In or Register to comment.