Shop OBEX P1 Docs P2 Docs Learn Events
converting uM-FPU floating point values to int — Parallax Forums

converting uM-FPU floating point values to int

RayRay Posts: 3
edited 2005-07-01 01:16 in General Discussion
As an avid Javelin user I decided to increase computing power by adding the uM-FPU coprocessor. The uM-FPU wasn't difficult to use and was well documented. I have been successful at printing the 32 bit floating point result (a whole number between zero and 300). However, I have been unable to convert the result into an "int" for further processing.

Any suggestions?

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-06-30 05:33
    There is a method to do that
    intValue Get the integer value of this Float32.

    regards peter

  • camtcamt Posts: 45
    edited 2005-06-30 14:43
    Peter's answer is correct for the Float32 compatible class, but there is a new class available for uM-FPU V2. See the following web page for details on uM-FPU Javelin support.

    http://www.micromegacorp.com/javelin.html

    The new class is compatible with code generated by the uM-FPU IDE (currently available in beta, the general release is due in the next couple of days). With the new class you would use the readWord method.

    See the document:
    Using uM-FPU V2 with the Javelin Stamp (Jun 21, 2005), page 9 and 13

    Regards,
    Cam Thompson
    Micromega Corporation
  • RayRay Posts: 3
    edited 2005-06-30 19:40
    Hi Cam
    Thanks for the prompt reply. I'm trying to use the new class and the readWord method, but clearly I'm missing something. The code should input 30; divide by 2; and output 15. When I use the floatFormat I get the correct answer of 15, but when I·try readWord I get 0.
    Thanks for the help.
    Ray

    Here is the code:

    import com.micromegacorp.math.v2_spi.*;
    import stamp.core.*;
    public class fpuTest1a {
    · final static int fpuRegister1 = 1;
    · public static void main() {
    ··· int testValue = 30;
    ··· // reset the uM-FPU and print version string
    ··· if (!Fpu.reset()) System.out.println("uM-FPU not responding.");
    ··· else System.out.println(Fpu.version());
    ····· // send testValue to fpuRegister1 and convert to floating point
    ····· Fpu.startWrite();
    ····· Fpu.write(fpuRegister1, Fpu.LOADWORD);
    ····· Fpu.writeWord(testValue);
    ····· Fpu.write(Fpu.FSET);
    ····· Fpu.write(Fpu.LOADBYTE, 2, Fpu.FDIV); // divide testValue by 2
    ····· Fpu.write(Fpu.XOP, Fpu.READWORD);
    ····· int testReadWord = Fpu.read();
    ····· Fpu.stop();
    ····· System.out.println(Fpu.floatFormat(fpuRegister1, 51));
    ····· System.out.println(testReadWord);

    · } // end main
    } // end class

    // results printed in the debug window
    // uM-FPU V2.0
    //· 15.0
    // 0
    ·
  • camtcamt Posts: 45
    edited 2005-06-30 21:09
    Hi Ray,
    A couple of things.
    1) READWORD returns the lower 16 bits of a register, it doesn't convert from floating point first. If you want the integer value of a floating point number, you need to first convert it with the FIX instruction.
    2) READWORD returns 16 bits, so you need to use the readWord() method (the read() method only reads an 8-bit byte).

    I've included a modified version of your code with //***** comments where I made changes.
    I also inserted a wait() before the READWORD is sent. (See wait() description on page 8, and Reading data description on page 13 of Using the uM-FPU with Javelin doc.

    Regards,
    Cam


    import com.micromegacorp.math.v2_spi.*;
    import stamp.core.*;
    public class test {
    final static int fpuRegister1 = 1;
    public static void main() {
    int testValue = 30;
    // reset the uM-FPU and print version string
    if (!Fpu.reset()) System.out.println("uM-FPU not responding.");
    else System.out.println(Fpu.version());
    // send testValue to fpuRegister1 and convert to floating point
    Fpu.startWrite();
    Fpu.write(fpuRegister1, Fpu.LOADWORD);
    Fpu.writeWord(testValue);
    Fpu.write(Fpu.FSET);
    Fpu.write(Fpu.LOADBYTE, 2, Fpu.FDIV); // divide testValue by 2
    Fpu.write(Fpu.FIX, Fpu.SELECTA); //***** convert FP to Int, result in reg0
    Fpu.wait(); //***** wait should be done before any read instruction
    Fpu.startWrite(); //***** startWrite needed after wait
    Fpu.write(Fpu.XOP, Fpu.READWORD);
    //***** int testReadWord = Fpu.read(); //***** need readWord after READWORD
    int testReadWord = Fpu.readWord();
    //***** Fpu.stop(); //***** stop already done by readWord
    System.out.println(Fpu.floatFormat(fpuRegister1, 51));
    System.out.println(testReadWord);

    } // end main
    } // end class
  • RayRay Posts: 3
    edited 2005-07-01 01:16
    Hi Cam
    Thanks for the suggestions, they did the job. The program runs now, I don't think that I could have figured it out without your help.
    Thanks
    Ray
Sign In or Register to comment.