converting uM-FPU floating point values to int
Ray
Posts: 3
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?
Any suggestions?
Comments
intValue Get the integer value of this Float32.
regards peter
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
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
·
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
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