Shop OBEX P1 Docs P2 Docs Learn Events
how to take 2 bytes and convert to a long — Parallax Forums

how to take 2 bytes and convert to a long

mike goettlingmike goettling Posts: 31
edited 2015-04-02 09:26 in Propeller 1
got tunnel vison.


using fullduplexserial program. i need to recive 2 bytes and convert to a·long for data use.
the bytes recived are high byte and low byte of a location pointer.
i need to take the two bytes and create a·long then divde it· by 2
then it is a new pointer location



serial = "fullduplexserial"
long scrprt
byte cursorh
byte cursorl

······ cursorh :=·serial.rx············ get high bit of pointer
······ cursorl :=·· serial.rx··········· get low bit of pointer

·from here i need to take cursorh and cursol and put them in scrprt.
this is where i am stuck. how do i get two bytes into a long.
once they are in the scrprt then i can divde by 2

······· scrprt := scrprt /2

Comments

  • Martin HebelMartin Hebel Posts: 1,239
    edited 2006-12-24 03:40
    I can't think of a fancy way right now with Spin, but the common way to combine 2 bytes into a word (or long) is:

    scrptr := cursorh * 256 + cursorl

    -Martin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Martin Hebel
    StampPlot - Graphical Data Acquisition and Control
    AppBee -·2.4GHz Wireless Adapters & transceivers·for the BASIC Stamp & Other controllers·
  • mike goettlingmike goettling Posts: 31
    edited 2006-12-24 03:53
    i will try that.
    does not need to be fancy just work
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-12-24 03:55
    A pretty fast method would be

    scrprt := cursorh << 7 + cursorl >> 1

    this should produce the final result you are looking for.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • James LongJames Long Posts: 1,181
    edited 2006-12-24 04:02
    Paul,

    I'm confused about this instruction.

    7+ ??? wouldn't replace the top bit of the cursorh variable?

    Or have I just had my dumb pill today.

    James L
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-12-24 04:28
    I was rolling the divide by two into the equation:

    scrprt := cursorh << 8 + cursorl
    scrprt >>= 1

    is the same as

    scrprt := cursorh << 7 + cursorl >> 1

    only faster the way spin does it's execution.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • william chanwilliam chan Posts: 1,326
    edited 2006-12-24 07:22
    I think the divide by 2 (rotate to the right) is unnecessary.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.fd.com.my
    www.mercedes.com.my
  • JoeldarJoeldar Posts: 17
    edited 2015-04-01 06:01
    I found this thread when looking for the best way to combine the MSbyte and LSbytes from a register. The propeller manual says the MSBs are dropped as the bits are shifted left using the << command. Why wouldn't shifting the bits of a byte 8 left effectively make it zero? Is the example assuming the MSByte is somehow changed to a long before the shift occurs?
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-04-01 08:41
    Why wouldn't shifting the bits of a byte 8 left effectively make it zero?


    No. Internally, everything is stored as a long. If the destination value was a byte, then you'd have a problem, With the destination being a long, you're fine.

    I would do it like this:
    longvar := (msbbyte << 8) | lsbbyte
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-04-01 08:52
    I'd do it this way, assuming the top 16 bits of scrpt are always zero. (Of course, there's probably no good reason that scrpt couldn't be a word, instead of a long.)
    byte[@scrpt][1] := serial.rx
    byte[@scrpt][0] := serial.rx
    scrpt >>= 1
    

    This way would also work, and is probably quicker:
    scrpt := (serial.rx << 7) | (serial.rx >> 1)
    


    -Phil
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-04-01 10:00
    I've been frustrated by bit shifts and bit rotations several times. I find if I limit the shift and rotate operations to longs, the results end up as expected. Shifting (and particularly rotating) bits in bytes or words can be unpredictable (at least they were to me).

    All local variables are longs. I suggest using a local variable (or global long) when performing bit manipulation operations.
  • AribaAriba Posts: 2,690
    edited 2015-04-01 10:45
    There is also a very readable way in Spin:
      longvar.byte[0] := lowbyte
      longvar.byte[1] := hibyte
      ~~longvar
    
    The last line is only needed if the two bytes build a signed number. The ~~ operator expands the sign bit to the full 32bit range.

    Andy
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-04-01 10:58
    Ariba wrote: »
    There is also a very readable way in Spin:

    I don't know if you saw post #8 or not. The thread has taken a detour.
  • ratronicratronic Posts: 1,451
    edited 2015-04-01 13:12
    You are receiving a 16 bit word. If it is a signed +/- number then you could do something like this to receive the word

    and convert it to a signed(+/-) 32 bit long that is divided by 2.
    scrprt := (serial.Rx << 24) | (serial.Rx << 16)
    scrprt ~>= 17
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-04-01 13:14
    Since the OP stated that the resulting long is a data pointer, it's unlikely to be negative.

    -Phil
  • ratronicratronic Posts: 1,451
    edited 2015-04-01 13:17
    That is what happens when you don't read the entire thread!
  • AribaAriba Posts: 2,690
    edited 2015-04-01 14:06
    That is what happens when you revive an old thread and change the topic.

    Perhaps in 9 years someone exhume this thread again and needs a respond to the original question, just for signed numbers. In that case my respond was not totally useless...

    Andy
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-04-01 14:16
    You're right. I didn't even notice the date on the first post!

    -Phil
  • kwinnkwinn Posts: 8,697
    edited 2015-04-01 18:40
    ..................................


    This way would also work, and is probably quicker:
    scrpt := (serial.rx << 7) | (serial.rx >> 1)
    


    -Phil

    Nice one, and yes, I did notice the date of the original post.
  • JoeldarJoeldar Posts: 17
    edited 2015-04-02 09:26
    Sorry for pulling out an old thread, I should have just pasted the text in question.

    I appreciate the help, I put the suggested code:

    TempRaw := (TempMSB << 8) | TempLSB
    TempRaw := TempRaw >> 1

    I believe that combines the two registers and shifts out the fault code in the LSBit.

    Unfortunately I'm back to getting no predictable operation from the Max31865 board. I think I may need to get another part to ensure I'm not beating my head against the wall with a defective part.

    Thanks

    Joel
Sign In or Register to comment.