Notes About Working with Various Arduino & Netduino Microcontroller Boards

Saturday, November 3, 2012

Basic Example Using a Floating Point Co-Processor with the Arduino

The uM-FPU v 3.1 math co-processor can add significant calculating power to the Arduino. The FPU library is available from the Arduino Playground.

Using SPI, the Arduino and co-processor communicate over 3 wires. The complete wiring diagram is has a few more connections, but the setup isn't bad.  

The manufacturer, Micromega Corp., has good uM-FPU documentation on their site. There are also a couple code examples included with the FPU library. 

The sample code below is meant to be a very bare-bones example showing the minimum code needed to work with the co-processor. This example calculates the square root of 2 and displays the result in the serial console with 9 decimal places.

#include <SPI.h>
#include <Fpu.h>
#include <FpuSerial.h>

void setup()
{
  Serial.begin(9600);
  SPI.begin();
  Fpu.begin();

  // Check to make sure uM-FPU is connected
  if (Fpu.sync() != SYNC_CHAR) {
    Serial.print("uM-FPU not detected");
    while(1) ; // stop if FPU not detected
  }
}

void loop() {
  /*  SELECTA, 1 means select register 1 as "accumulator" *workspace)
   *  FSETI, 2 means load 2 into the accumulator as a float
   *  SQRT means calculate square root of value in accumulator & save result 
   *       in the accumulator.
   *  Each Fpu.write can hasve up to a total of 6 commands & arguments combined. 
   */
  Fpu.write(SELECTA, 1, FSETI, 2, SQRT);
  Serial.print("Sqrt of 2 = ");
  /*  Print result to the serial monitor.
   *  129 means format up to 12 charaters toal, 9 to the right of decimal point.
   */
  FpuSerial.printFloatln(129);  
  while(1);  // stop here
}

No comments:

Post a Comment