Shop OBEX P1 Docs P2 Docs Learn Events
Word Sized constant in Byte array — Parallax Forums

Word Sized constant in Byte array

DynamoBenDynamoBen Posts: 366
edited 2009-05-18 07:11 in Propeller 1
I have several word sized constants that need to be place into a byte array which is subsequently sent via serial. For the life of me I can't figure out how to place both bytes into the array. I've tried bytemove, wordmove, constant.byte[noparse][[/noparse]x], but none of these seem to·work.

Can this be done? If so·how?

Comments

  • mctriviamctrivia Posts: 3,772
    edited 2009-05-17 04:22
    just use x>>8 to get the top half of the word.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My new unsecure propmod both 1x1 and full size arriving soon.

    Need to upload large images or movies for use in the forum. you can do so at uploader.propmodule.com for free.
  • DynamoBenDynamoBen Posts: 366
    edited 2009-05-17 04:35
    Works perfect, thank you for the quick reply!
  • mctriviamctrivia Posts: 3,772
    edited 2009-05-17 04:40
    glad I could help.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My new unsecure propmod both 1x1 and full size arriving soon.

    Need to upload large images or movies for use in the forum. you can do so at uploader.propmodule.com for free.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-05-17 04:50
    DynamoBen,

    Here is another "trick" that avoids using any shifting or math...

    This demonstration uses the lights on the Propeller DEMO board.
    CON
    _clkmode          = xtal1 + pll16x                      
    _xinfreq          = 5_000_000
    
    PUB  MainProg
         L0 := %01100001_00110001_00011001_00001101
    {                                       
                    │        │        │        │
               W1_highbyte   │   W0_highbyte   │                              
                             │                 │
                        W1_lowbyte        W0_lowbyte
    }
         dira[noparse][[/noparse]16..23]~~
         repeat
           outa[noparse][[/noparse]16..23] := W0_lowbyte
           waitcnt(clkfreq*1+cnt)                           'Wait 1 second
           outa[noparse][[/noparse]16..23] := W0_highbyte
           waitcnt(clkfreq*1+cnt)                           'Wait 1 second
           outa[noparse][[/noparse]16..23] := W1_lowbyte
           waitcnt(clkfreq*1+cnt)                           'Wait 1 second
           outa[noparse][[/noparse]16..23] := W1_highbyte
           waitcnt(clkfreq*1+cnt)                           'Wait 1 second
    DAT
    L0            long
    W0            word
    W0_lowbyte    byte      0
    W0_highbyte   byte      0
    W1            word
    W1_lowbyte    byte      0
    W1_highbyte   byte      0
    



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • DynamoBenDynamoBen Posts: 366
    edited 2009-05-17 05:31
    Very interesting thanks Beau I may be able to use that.
  • Mike HuseltonMike Huselton Posts: 746
    edited 2009-05-17 08:53
    This also provides an alternative scheme for coding data structures. I'll take any coding "tricks" that I can.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    JMH
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-05-17 15:33
    Just an additional note:
    It isn't necessary to setup an entire variable array, you can just create one instance of a LONG defined in the DAT section like I have done...
    DAT
    L0            long
    W0            word
    W0_lowbyte    byte      0
    W0_highbyte   byte      0
    W1            word
    W1_lowbyte    byte      0
    W1_highbyte   byte      0
    
    

    ...·From there, if you need to determon what each byte is of a particular LONG, you can do something like ...
    L0 := SomeOtherLongVariable
     
    ' W1_highbyte, W1_lowbyte, W0_highbyte, W0_lowbyte now hold the ordered bytes that are
    ' associated to SomeOtherLongVariable
    

    ... You can also use this method in reverse ...

    W0_lowbyte  := %11001100
    W0_highbyte := %10101010
    W1_lowbyte  := %00110011
    W1_highbyte := %01010101
     
    ' Causes L0 to equal... %01010101_00110011_10101010_11001100
    

    · ... If you only wanted to affect the upper or lower WORD, then something like this would also work ...

    W0 := %10111101_11100111
     
    ' This way, W0_highbyte equals %10111101 and W0_lowbyte equals 11100111  
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 5/17/2009 3:46:58 PM GMT
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-05-18 07:11
    But that only works in DAT section, so if you want to write something for the ObjectExchange, please don't do it that way - or at least tell in the Header that this object can only be used as a singleton.

    The way it should also work with variables is:
    CON 
       word0 = $aaaa
       word1 = $1111
     
    VAR
       myVar byte[noparse][[/noparse]10]
     
    PUB main
     
       word[noparse][[/noparse] @myVar     ]:=word0
       word[noparse][[/noparse] @myVar + 2 ]:=word1
    
    

    You only have to take care of the alignment of the byte array.
Sign In or Register to comment.