Shop OBEX P1 Docs P2 Docs Learn Events
Convert WORD variable to 16 bit array? — Parallax Forums

Convert WORD variable to 16 bit array?

FalconFalcon Posts: 191
edited 2011-08-10 06:11 in BASIC Stamp
Hello,

I'm using the "two 74HC165 Shift Register" circuit from Stampworks V 2.1 to save some input pins. See below:

'
[ I/O Definitions ]
Clock PIN 0 ' shift clock (74HC165.2)
SerData PIN 1 ' serial data (74HC165.7)
Load PIN 2 ' output latch (74HC165.1)
'
[ Constants ]
DelayTime CON 100
'
[ Variables ]
xInputs VAR Word ' external inputs
'
[ Initialization ]
Reset:
HIGH Load ' make output and high
DEBUG CLS,
"XInputs FEDCBA9876543210", CR,
"

", CR,
"Status ................"
'
[ Program Code ]
Main:
DO
GOSUB Get_165x2 ' get inputs
DEBUG CRSRXY, 10, 2, BIN16 xInputs ' display current status
PAUSE DelayTime ' pad the loop a bit
LOOP
'
[ Subroutines ]
Get_165x2:
PULSOUT Load, 5 ' load inputs
SHIFTIN SerData, Clock, MSBPRE, [xInputs\16] ' shift them in
RETURN

My code needs to have the 16 bits of data in the xInputs WORD variable converted into a 16 bit array so each bit can be individually compared to a previous state. How can the WORD variable be converted to a 16 bit array?

falcon

Comments

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-08-08 09:55
    That's built in to the PBASIC syntax.

    Using a certain bit...
    IF xinputs.bit12 = old12 THEN ....
    

    Or using an indexed array...
    IF xinputs.bit0(12) = old(12) THEN ....
    
  • FalconFalcon Posts: 191
    edited 2011-08-09 22:11
    I was able to use the "IF xinputs.bit12 = old12 THEN ...." statement to get my code working. However, I had to remove the loop that read the individual array bits and insert multiple statements throughout my code to "read" individual bits.

    But just to know for future reference, is there a way to take a Byte or WORD variable and convert it to an 8- or 16-bit array?

    falcon
  • Mike GMike G Posts: 2,702
    edited 2011-08-10 06:11
    But just to know for future reference, is there a way to take a Byte or WORD variable and convert it to an 8- or 16-bit array?
    Bytes and words are an array of bits as shown by Tracy Allen. See Aliases and Modifiers in the BASIC Stamp Manual (pdf) page 93. Click the Help drop down in the Stamp Editor and select BASIC Stamp Manual (pdf).
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    
    aWord   VAR   Word
    aBit    VAR   Byte
    
    aWord = %1010101000001111
    
    FOR aBit = 0 TO 15
      DEBUG ?aWord.BIT0(aBit)
    NEXT
    
Sign In or Register to comment.