Shop OBEX P1 Docs P2 Docs Learn Events
uM-fpu confusion — Parallax Forums

uM-fpu confusion

Tom JTom J Posts: 1
edited 2004-10-20 22:35 in BASIC Stamp
Hi all... I am having on heck of a time geting a number into the floating point unit.
this works
fA = lat1
· fHigh = $4524··· '2628.813
· fLow· = $4D03
· GOSUB Load_Float
· GOSUB Fset
the problem is that i have the numbers in variables but i cant get it to process them
lat1_bit(0) = 2628
lat1_bit(1) = 8133
fHigh = lat1_bit(0)
fLow = lat1_bit(1)
GOSUB load_float
GOSUB fset

I have tried using all the "load_float*" support routines that make sense.
I am looking for a way to convert the variable to the correct hex code or properly process the variable with the load routine.
Thank you
Tom Jaeger

Comments

  • camtcamt Posts: 45
    edited 2004-10-20 22:35
    Hi Tom,
    I assume the numbers you are trying to convert are from a serial source of some kind, and are the left side and right side of a floating point string. To convert to floating point you need to convert the right side to a fraction, then add it to the left side. Alternatively, the uM-FPU can convert an ASCII string directly. Three examples are provided that should solve your problem. Example 1 is the original method you asked about, Examples 2 and 3 use strings.
    -Cam

    '
    ' Example 1
    ' If the integer and fraction part of a number are converted by SERIN
    ' and stored as seperate numbers (as in the original question),
    ' the device must send a fixed number of digits on the right
    ' side of the decimal point for this method to work. In other words,
    ' the device must send trailing zeroes (e.g. 123.5000, 123.0500, 123.0050).
    ' If the device truncates zeroes (e.g. 123.5, 123.05, 123.005), the SERIN
    ' format conversion will yield 5 for the right side in all cases,
    ' since leading zeroes are ignored by SERIN. If the format has 4 digits
    ' to the right of the decimal point, the following code will convert the
    ' number and store it in register 1 on the uM-FPU.
    '

    val VAR Word(2)

    SERIN 0, 16884, [noparse][[/noparse]DEC val(0), SKIP 1, DEC val(1)] 'read left/right values

    fA = 1 'select uM-FPU register 1
    fLow = val(1) 'load number from right side to uM-FPU
    GOSUB Load_FloatWord 'convert to floating point
    GOSUB Fset
    fLow = 10000 'load 10000
    GOSUB Load_FloatWord 'convert to floating point
    GOSUB Fdivide 'divide right side by 10000 to get fraction
    fLow = val(0) 'load number from left side to uM-FPU
    GOSUB Load_FloatWord 'convert to floating point
    GOSUB Fadd 'add to fraction
    '...

    '
    ' Example 2
    ' This example reads the string (e.g. 2628.8133) directly from the serial
    ' port and sends it to the uM-FPU for conversion. It terminates the
    ' conversion when a comma is read from the serial port. This example
    ' conserves variable space since no extra variables are required.
    '

    GOSUB Load_SerialString 'load
    fA = 1
    GOSUB Fset
    '...

    Load_SerialString:
    fOpcode = $F9 'convert ASCII to float
    GOSUB Sys_Command
    DO
    SERIN 0, 16884, [noparse][[/noparse]fChar] 'read character from serial port
    IF fChar = "," THEN EXIT 'check for comma terminator
    SHIFTOUT FpuData, FpuClock, MSBFIRST, [noparse][[/noparse]fChar] ' send character to FPU
    LOOP
    SHIFTOUT FpuData, FpuClock, MSBFIRST, [noparse][[/noparse]0] ' send zero terminator
    RETURN

    '
    ' Example 3
    ' This example reads the string into RAM, then sends it to the uM-FPU for
    ' conversion. It requires a 10 character string buffer.
    '

    serStr VAR Byte(10)
    cnt VAR Byte

    SERIN 0, 16884, [noparse][[/noparse]STR serStr\10\","] 'read string
    GOSUB Load_RamString
    fA = 1
    '...

    Load_RamString:
    fOpcode = $F9 'convert ASCII to float
    GOSUB Sys_Command
    FOR cnt = 0 TO 10
    IF serStr(cnt) = 0 THEN EXIT 'check for comma terminator
    SHIFTOUT FpuData, FpuClock, MSBFIRST, [noparse][[/noparse]serStr(cnt)] ' send character to FPU
    NEXT
    SHIFTOUT FpuData, FpuClock, MSBFIRST, [noparse][[/noparse]0] ' send zero terminator
    RETURN
Sign In or Register to comment.