Shop OBEX P1 Docs P2 Docs Learn Events
binary math - add three 8 bit variables — Parallax Forums

binary math - add three 8 bit variables

gtslabsgtslabs Posts: 40
edited 2007-03-28 00:18 in BASIC Stamp
How can I divide a 24 bit value into 3 8 bit variables so I can read and write them to 24 bit registers?
Is there any example code how to do this since the word is the largest at 16 bits?

Would this fall under the "big endian" and "little endian" format?
Thanks
Steve

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-27 19:13
    Where are you getting the value? Typically, you are getting this value from somewhere external to the Stamp and it is already in the form of 3 x 8-bit values and you don't have to do anything other than keeping in that form.

    "big endian" vs. "little endian" refers to whether the most significant byte comes first or the least significan byte comes first respectively.
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-03-27 19:22
    Where are your 24-bit "registers"?
  • gtslabsgtslabs Posts: 40
    edited 2007-03-27 20:12
    I am using the AD7730 chip and need to send its filter register the hex value of 200110. This is 2,097,424 in decimal and 00100000.00000001.00010000 in a 24 bit binary number with the "dots" eparating every 8 positons.

    I have been using the SHIFTIN and SHIFTOUT commands but not figured how to send it the 3 sets of 8 yet.



    I have this VB code to convert decimal to 24 bit binary that I was thinking of using to put on the stamp.

    Public Function dec24binFunc(ByVal x As Double) As String
    ··· Dim p As Double
    ··· bits = ""
    ··· p = 1
    ··· i = 0
    ··· Do Until i >= 23
    ······· i = i + 1
    ······· If x And p Then bits = "1" + bits Else bits = "0" + bits
    ······· p = p * 2
    ······· ' This puts a dot for every 8 bits for better reading
    ······· If (i Mod 8) = 0 Then bits = "." + bits
    ··· Loop
    ··· If i > p Then bits = "1" + bits Else bits = "0" + bits
    ··· dec24binFunc = bits
    End Function
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-27 20:29
    You'd simply use SHIFTOUT to send 3 constants: %00100000,%00000001,%00010000 or $20,$01,$10. These would be assumed to be 8 bit values by the Stamp.
  • gtslabsgtslabs Posts: 40
    edited 2007-03-27 20:49
    Thanks Mike, I think that worked.

    How would I read·the·3 8-bit·numbers using the SHIFTIN command?
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-03-27 21:01
    LowByte VAR BYTE
    MidByte VAR BYTE
    HighByte VAR BYTE

    SHIFTIN PortNum, [noparse][[/noparse]LowByte/8, MidByte/8, HighByte/8]
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-28 00:00
    Allan,

    You cannot use LOWBYTE and HIGHBYTE as variable names. For SHIFTIN you will need 3 parameters following the command besides the data. The slashes should be backslash, not forward slash, though you don’t need them for 8 bits as that is the default. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-03-28 00:18
    Oops, right, xxx.LowByte is a keyword in PBasic. D'oh!
Sign In or Register to comment.