Shop OBEX P1 Docs P2 Docs Learn Events
combine 2 bytes into 1 word — Parallax Forums

combine 2 bytes into 1 word

archer1424archer1424 Posts: 7
edited 2013-04-17 07:24 in General Discussion
Hi all,


I'm am reading in coordinate data from a pcap screen (serially) and I am having a simple problem. The x and y coordinate data comes in as 2 bytes each, so I need to combine the two bytes together so I can use the single binary value. My first attempt used a two element byte sized array. I placed the first byte in array[0] and the second in array[1]. when I print the result to the terminal I only get the first byte with many leading 0s. Is this the correct approach, or am I missing something simple?



Thanks
Dan

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-17 07:00
    You didn't say what microcontroller you're using or what programming language. In both PBasic for the Stamps and Spin for the Propeller, there are ways to access the two bytes of a word variable. If you declare your variable "foo" as a word in PBasic with "foo VAR word", you can set the least significant byte with "foo.byte0 = x" and the most significant byte with "foo.byte1 = y". Look at pages 89-91 of the Basic Stamp Reference Manual for details. You'd do something similar in Spin using ".byte[ 0]" and ".byte[ 1]".
  • archer1424archer1424 Posts: 7
    edited 2013-04-17 07:24
    Mike,

    propeller- spin

    I apologize for my terrible forum etiquette, now I know. The .byte[n] worked perfectly!


    Thanks
    Dan
Sign In or Register to comment.