Shop OBEX P1 Docs P2 Docs Learn Events
Handling 32 bit numbers — Parallax Forums

Handling 32 bit numbers

tdoddstdodds Posts: 12
edited 2010-05-18 00:51 in General Discussion
Hi,
I have a project where I am gathering and reporting data from several ModBus devices using RS485.·I have the·hardware working just fine, but these·devices report some of their values in 32-bit signed-integer or 32-bit floating-point. Is there a subroutine that I can·adopt to convert these values into ASCII strings (hopefully in SX-B)? In my site search I found a routine for 16 bit numbers (BIN16DEC) and that works just fine for the 16bit registers, but offhand I can't·visualize·a way to expand it·to·handle·these large numbers (a one in the 31st position has to report as ASCII 1,073,741,824 !) and it has to also handle the sign bit.
Thanks for the help.

Comments

  • ZootZoot Posts: 2,227
    edited 2010-05-17 13:20
    If you go to sxlist.com, there are two routines that might help under the "binary" section:

    32bit to BCD packed and ASCII 10 digits
    and
    24bit signed to ASCII 8/9 digit decimal

    I can't remember if the former handles the sign, if it doesn't, you should be able to crib the relevant code from the 24 bit routine.

    www.sxlist.com/techref/ubicom/lib/math/radix/index_sx.htm

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • tdoddstdodds Posts: 12
    edited 2010-05-18 00:51
    Tracy Allen at Berkeley has a great writeup on the subject and code for the Basic Stamp. I ported the divide-by-ten subroutine from his document to my SX28 board and it works perfectly. Below is the SX/B code:

    DEVICE sx28,oscxt2, turbo,stackx,optionx,BOR26
    FREQ 8_000_000
    ArrayByte VAR byte (12)
    send VAR byte
    TmpB1 VAR Byte
    temp VAR Byte
    TmpW1 VAR Word
    WX VAR Word
    WY VAR Word
    RM VAR Byte
    TX_BYTE SUB 1,2 'transmit byte
    Bin32DEC SUB 1,4 ''Create ASCII string from double word

    Start:
    Bin32DEC 0,0,0,128 'ENTER 32 BIT NUMBER -Param1 (leftmost) is least significant byte
    For J = 0 to 9
    ArrayByte(J) = ArrayByte(J) + 48
    Tx_Byte ArrayByte(J)
    Next
    Pause 2000
    Goto Start

    TX_BYTE:
    SEROUT Send, N9600, __PARAM1 ' send the byte
    return
    Bin32DEC:
    WX = __WPARAM34 'Most significant word
    WY = __WPARAM12 'Least significant word
    for I = 9 to 0 Step -1
    TmpW1 = WX//10 'high remainder
    temp = TmpW1_LSB
    WX = WX/10 'high word quotient
    TmpW1 = WY//10 'remainder calc
    TMPB1 = TmpW1_LSB
    RM = temp * 6
    RM = RM//10
    RM = RM + TmpB1
    WY = WY/10 'Low word quotient
    TmpW1 = RM/10 'WY = (WY/10)+(RM/10)+(temp*6553)+(Temp*6/10)
    WY = TmpW1 + WY
    TmpW1 = temp*6553
    WY = WY + TmpW1
    TmpB1 = Temp*6
    TmpB1 = TmpB1/10
    WY = WY + TmpB1
    RM = RM//10 'final remainder
    Arraybyte(I) = RM
    next
    return
Sign In or Register to comment.