Shop OBEX P1 Docs P2 Docs Learn Events
BS1 shifting — Parallax Forums

BS1 shifting

nick bernardnick bernard Posts: 329
edited 2004-10-08 21:24 in BASIC Stamp
for the BS1,

i know its not the prettiest thing in the world but it works.
but if anyone has any ideas on how to make this code better i'd really appreciate it.
rox on
nick bernard
'not the entire script, just some variable definitions and subroutines
'variables
SYMBOL· d0····· =BIT0
SYMBOL· d1····· =BIT1
SYMBOL· d2····· =BIT2
SYMBOL· d3····· =BIT3
SYMBOL· d4····· =BIT4
SYMBOL· d5····· =BIT5
SYMBOL· d6····· =BIT6
SYMBOL· d7····· =BIT7
SYMBOL· d8····· =BIT8
SYMBOL· d9····· =BIT9
SYMBOL· d10···· =BIT10
SYMBOL· d11···· =BIT11
SYMBOL· d12···· =BIT12
SYMBOL· d13···· =BIT13
SYMBOL· d14···· =BIT14
SYMBOL· d15···· =BIT15
SYMBOL· door··· =B0


'subroutines
SHIFT_IN:
·· PULSOUT SR_LOAD,1
·· d0 = SR_INPUT
·· PULSOUT SR_CLK,1
·· d1 = SR_INPUT
·· PULSOUT SR_CLK,1
·· d2 = SR_INPUT
·· PULSOUT SR_CLK,1
·· d3 = SR_INPUT
·· PULSOUT SR_CLK,1
·· d4 = SR_INPUT
·· PULSOUT SR_CLK,1
·· d5 = SR_INPUT
·· PULSOUT SR_CLK,1
·· d6 = SR_INPUT
·· PULSOUT SR_CLK,1
·· d7 = SR_INPUT
RETURN

SHIFT_OUT:
·· GOSUB SO_BIT
·· GOSUB SO_BIT
·· GOSUB SO_BIT
·· GOSUB SO_BIT
·· GOSUB SO_BIT
·· GOSUB SO_BIT
·· GOSUB SO_BIT
·· GOSUB SO_BIT
·· PULSOUT SR_LATCH,1
RETURN
SO_BIT:
· IF d0 = 1 THEN LED_ON
· LOW 7
· GOTO SHIFT
LED_ON:
· HIGH 7
SHIFT:
· PULSOUT SR_OCLK,1
· door = door/2
RETURN

Comments

  • AlWilliamsAWCAlWilliamsAWC Posts: 135
    edited 2004-10-08 16:29
    This code is from our PAK-I manual. It uses BS2 syntax, but should adapt easily.·It might be helpful to you:

    ' iobyte is the byte to shift in or out
    datapin con 15
    datainput var in15
    clkpin con 14
    i var byte
    iobyte var byte
    shiftoutput: ' sends iobyte
    output datapin
    for i=0 to 7
    · ' Set data pin to 0 or 1
    · low datapin
    · if (iobyte & $80) <> $80 then so0
    · high datapin
    so0:
    · iobyte=iobyte*2 ' shift byte left
    · high clkpin
    · low clkpin ' could use pulsout
    next
    return
    shiftinput:
    input datapin
    iobyte=0
    for i=0 to 7
    · iobyte=iobyte*2 ' shift left
    · iobyte=iobyte | datainput
    · high clkpin
    · low clkpin ' could use pulsout
    next
    return
    Of course, you could rearrange this to shift right pretty easily.

    Regards,

    Al Williams
    AWC
    Add Floating Point Math to your Stamp
    http://www.awce.com/pak12.htm
    ·
  • nick bernardnick bernard Posts: 329
    edited 2004-10-08 19:34
    thx al,
    i considered a nested shifting function. but i was hoping for a way to adress the bits of the byte like values of an array. oh me

    rox on
    nick B
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-10-08 21:24
    Here's a bit of code from an ancient program (I wrote in '94) that will do a shift-out on the BS1.· I was using this to talk to a 74HC595:

    Shift_Out:
    · FOR shift = 1 TO 8··········' shift 8 bits
    ··· SData = Bit7··············'·put MSB on·data pin
    ····PULSOUT Clk, 1············' clock the bit
    ····temp = temp * 2···········' get next bit
    · NEXT
    ··RETURN

    This code does an MSBFIRST shift -- you can change the Bit7 reference to Bit0, and divide temp by 2 instead of multiplying.· It's probably obvious, but the variable "temp" is aliased to B0.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
Sign In or Register to comment.