Shop OBEX P1 Docs P2 Docs Learn Events
2 byte result — Parallax Forums

2 byte result

Farmer ScottFarmer Scott Posts: 30
edited 2008-05-29 00:21 in Propeller 1
I think I know the answer to this one, but was hoping someone could help me and verify...

I want to send a two byte I2C result as a return code from a public function, using the Basic I2C Driver from the object exchange...

Is this valid to assign both bytes to result:

Result[noparse][[/noparse]0] := I2C.Read(SCL, NAK)
Result[noparse][[/noparse]1] := I2C.Read(SCL, ACK)

Thanks in advance...

Scott

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-05-28 21:26
    You are close but not quite right, indirect data (which arrays are an example of) is accessed via BYTE, WORD, or LONG. So for your example, say you have declared Result as type WORD, to access the least significant byte you would use BYTE[noparse][[/noparse]@Result][noparse][[/noparse]0] (the second bracket in this example is optional, but for symmetry when accessing multiple elements·you should include it). To access the most significant byte you use BYTE[noparse][[/noparse]@Result][noparse][[/noparse]1]. Which one goes first depends on the endian-ness of the device you're interfacing with. If it's little endian you assign to BYTE[noparse][[/noparse]@Result][noparse][[/noparse]0] then BYTE[noparse][[/noparse]@Result][noparse][[/noparse]1]. If it's big endian you assign to BYTE[noparse][[/noparse]@Result][noparse][[/noparse]1] then BYTE[noparse][[/noparse]@Result][noparse][[/noparse]0].

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

    Parallax, Inc.
  • AribaAriba Posts: 2,687
    edited 2008-05-28 21:49
    Another solution:
    result := I2C.Read(SCL, NAK) + I2C.Read(SCL, ACK)<<8

    'result' is always a long, so you can pack up to 4 bytes in it. Here the first byte is in bits7..0 and the scond in bits 15..8.
    To separate theme again:
    first := result & 255
    second := result >> 8

    Andy
  • Farmer ScottFarmer Scott Posts: 30
    edited 2008-05-29 00:21
    Paul & Andy,

    Thanks to both of you.· Andy's method looks a little cleaner, so I'm gonna give it a shot and see how things go, but appreciate both responses...

    Thanks,

    Scott
Sign In or Register to comment.