Shop OBEX P1 Docs P2 Docs Learn Events
Sending a 32 bit value out a serial port — Parallax Forums

Sending a 32 bit value out a serial port

pogerttpogertt Posts: 33
edited 2009-02-18 19:56 in BASIC Stamp
I have an application that requires a value greater than 65535 to be output in a serial string.
46 ** 4000· =· 184000· or··· h2······ hCECO
If the upper word is 0, the result is less than 65536
if it is 1, then make HU =6553 and save the left over 6 for later.
If it is 2, then make HU =(65536 *2 = 131072) \ 10 for 13107 and save the 2 for later.
hCECO =52928· then / by 10 for 5292 and save the 8.
13107 + 5292 =18399

The left over 2 +8 =10 so if this is over 9, add one to 18399 for 18400, then subtract 10 from the 2+8 variable.

output the variable representing the 18400, followed by the variable representing the saved remainders, and you have now sent a 32 bit value out the serial port using a controler that likes 16 bit numbers.


Pogertt

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-02-18 16:31
    Assuming you have the value stored somewhere in two word variables then it is a simple task to send out the 32-bit value since the serial port only deals with bytes anyway. Even a 16-bit value must be broken up into individual bytes. So assuming your two 16-bit values are in word variables named highval and lowval, you might do something like this:
    [color=#000000]SEROUT pin, baud, [noparse][[/noparse]lowval.lowbyte, lowval.highbyte, highval.lowbyte, highval.highbyte][/color]
    
    

    I hope this helps.· Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-02-18 19:22
    I think Pogertt is after a decimal display of a double precision variable. Here is a program that displays any unsigned 32 bit number as up to 10 decimal digits. It works by successive division by 10 to strip off the remainders, similar to Pogertt's method.

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    ' demo shows conversion of 32 bit binary double precision variable to decimal display
    ' Tracy Allen, www.emesystems.com
    ' Enter 8 hex digits at the keyboard, including leading zeros if necessary.
    z1 VAR WORD   ' two words form double prec (32 bit) variable z1:z0
    z0 VAR WORD
    rm1 VAR NIB     ' remainders, for calculation
    rm0 VAR BYTE
    flag VAR BIT       ' for leading zero suression
    nub VAR NIB(10)   ' this array stores 10 possible digis
    idx VAR NIB         ' index
    
    DO
      DEBUG CR, "Please enter 8 hex digits (e.g. 0FDB9753): "
      DEBUGIN HEX4 z1, HEX4 z0
      DEBUG CR, "Decimal value = "
      GOSUB showDubDec10zBS2
    LOOP
    
    
    
    showDubDec10zBS2:    ' enter here with z1:z0
      FOR idx=0 TO 9    ' 10 bytes hold 10 digits, peal off remainders of each divide by 10
        rm1=z1 // 10   ' high remainder
        z1=z1 / 10      ' high word of quotient
        rm0=((rm1*6//10) + (z0//10))    ' remainder calc, value from 0 to 18
        z0=(rm1*6553) + (rm1*6/10) + (z0/10) + (rm0/10) 'low word of quotient
        rm0=rm0 // 10     ' final remainder
        nub(idx)=rm0   ' store up digits from lsd to msd
      NEXT    ' idx next digit
      ' -- printout routine, digits stored in nib array
      flag=0   ' flag allows leading zero suppression in display
      FOR idx=9 TO 0      ' print digits, most significant first, suppress leading zeros
        rm0 = nub(idx)      ' from memory msd first
        IF rm0>0 OR idx=0 THEN flag=1
        IF flag THEN DEBUG DEC1 rm0 ' print the ascii code, suppress leading zeros
      NEXT
      RETURN
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-02-18 19:54
    Tracy,

    I have been using your·math·routines for calculating DDS values (32 bits +) .

    Many thanks·for such wondeful tools.

    But being lazy and so far not fully understnding the "magic" , do you have a sample·code for decimal to hex conversion - I need more that 5 digits decimal precision.

    Thanks in advance.
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-02-18 19:56
    Whoops,
    I better look at the code you just published and see if I can modify it for hex.
    Sorry
Sign In or Register to comment.