Shop OBEX P1 Docs P2 Docs Learn Events
Array of bits to shiftout — Parallax Forums

Array of bits to shiftout

pohopoho Posts: 3
edited 2009-02-12 02:20 in BASIC Stamp
Hi Everyone,

Im pretty new to the whole basic stamp thing, but have been programming in various laguages for many years (unfortunately none of which were basic). Anyways, im working on restoring a pinball machine which has ~45 lights on it and ~23 switches, as well as some 50v solenoids, all to be controlled by a BS2sx chip. I have made a controller board of 74hc4094's for my lights which i have gotten working using a for-loop. The place where im storing the lights status (0 or 1) is an array of bits. Heres what i want to do: use shiftout to dump that array of bits to my light controller board, however it doesnt seem to be working. Here is my for loop to get a better idea:

pinStatus VAR Bit(MAX_PINS)


  FOR loopCounter = 0 TO (MAX_PINS - 1)
    currentPin = (MAX_PINS - 1) - loopCounter
    IF (pinStatus(currentPin) = 0) THEN
      SERIAL = IsOff
    ELSE
      SERIAL = IsOn
    ENDIF
    CLOCK = 1
    CLOCK = 0
  NEXT
PULSOUT Strobe,1



EDIT: i should mention here that CLOCK, SERIAL, Strobe are PIN vars (although they are consts in the below example) and MAX_PINS is a const currently defined as 45 (which i should probably move up to 48)

Ideally i want to replace it with something like this:
SHIFTOUT 1, 2, MSBPRE, [noparse][[/noparse]pinStatus]
      PULSOUT Strobe,1




which im hoping will be well faster, however its the passing of pinStatus to shifout which is giving me grief. Ive tried a few different combinations such as:
SHIFTOUT 1, 2, MSBPRE, [noparse][[/noparse]pinStatus]
SHIFTOUT 1, 2, MSBPRE, [noparse][[/noparse]pinStatus(0)]
SHIFTOUT 1, 2, MSBPRE, [noparse][[/noparse]pinStatus(0)\16] (to get only the first 16 statuses)
and even
SHIFTOUT 1, 2, MSBPRE, [noparse][[/noparse]pinStatus(0)\16, pinStatus(0)>>16\16, pinStatus(0)>>32\16]

Can anyone help?!? I can get it working if i just use 65535 instead of the pinStatus crazyness, so i know my controller board is fine.

on a side note i have a usb->serial adapter (chipset HL-340), however it only detects the stamp *some* of the time, although echo and the other thing come back positive - any ideas?

Cheers

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-11 15:13
    SHIFTOUT works best if you use bytes or words as the underlying data unit. Bit arrays are overlaid on bytes and words anyway. If you have three successive words (16 bits each) that are also used as a word array (3), byte array (6), and bit array (48), you would do your SHIFTOUT like:

    SHIFTOUT 1, 2, MSBPRE, [noparse][[/noparse] theWords1, theWords2, theWords3\13 ]

    or

    SHIFTOUT 1, 2, MSBPRE, [noparse][[/noparse] theWords(0), theWords(1), theWords(2)\13 ]

    The \13 would only be needed if you still want to use only 45 bits.
  • pohopoho Posts: 3
    edited 2009-02-11 21:44
    Yeah thats pretty much what i thought, however using words would make it a pain to set individual bits within each of those words. As i will be doing potentially quite a few of these operations, id want to keep my code to a minimum.

    I might just stick with the loop for now - it is probably the fastest, and in a game application, speed is everything!
  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-11 22:27
    You can still set individual bits by using the ".bit(x)" suffix which, when applied to the first word of a word array, turns it into a bit array. If you declare "array var word(3)", you can then say "array.bit(40)" which will get you the 8th bit of the 3rd word. You can also declare a bit array as an alias to "array" and then use the bit array name as an equivalent to "array.bit(x)". See the section in the PBasic Manual on aliases.
  • pohopoho Posts: 3
    edited 2009-02-12 02:20
    That looks good, i think ill use an alias. just one more question though, how do you type an alias. Judging by the alias documentation it seems to type itself based on what you give it.

    lightStatusWords VAR Word(3)
    lightStatusBits VAR ???
    
    



    or will i have to write a chunk of code to insert each bit into the 'bit' array

    also, the above suggested code doesnt work if i use
    lightStatusWords VAR Word(3)
    lightStatus.Bit(4) = 1
    
    



    it whinges on the 'bit' saying 'expected a variable modifier.

    Post Edited (poho) : 2/12/2009 9:59:14 AM GMT
Sign In or Register to comment.