This project serves as a template for work with the Freescale FRDM-KL46 board. Support will be added to all on-board peripherals, sensors and I/O.

Dependencies:   FRDM_MMA8451Q MAG3110 TSI mbed

Fork of FRDM-KL46-Template by Mike Maas

Project Information:

Theory

This project has been created to serve as a template for those who wish to use the Freescale Freedom FRDM-KL46Z board. Existing drivers within mbed have been brought together and board specific configurations for certain inputs and outputs have included.

Libraries

TODOs

Hardware Information:

FRDM-KL46Z Information

Freescale Kinetis L-Series Microcontroller Information

Freescale Sensor Information

MMA8451Q

MAG3110

Revision:
4:6cb640167538
Parent:
3:1e85b49a3e18
Child:
6:aaf347dfd538
--- a/main.cpp	Thu Dec 19 20:05:17 2013 +0000
+++ b/main.cpp	Tue Dec 31 17:16:57 2013 +0000
@@ -52,7 +52,6 @@
 // Include support for USB Serial console
 Serial pc(USBTX, USBRX);
 
-
 //////////////////////////////////////////////////////////////////////
 // Include support for on-board green and red LEDs
 #define LED_ON 0
@@ -91,6 +90,7 @@
     pc.baud( 230400 );
     pc.printf("Hello World\r\n");
 
+
     // Turn on pull up resistors on pushbutton inputs
     sw1.mode(PullUp);
     sw3.mode(PullUp);
@@ -105,7 +105,7 @@
 
     wait(1);
 
-    
+
     // Get some values
     printf("DR_STATUS %X\r\n", mag.readReg( MAG_DR_STATUS ));
     printf("WHO_AM_I %X\r\n", mag.readReg( MAG_WHO_AM_I ));
@@ -120,19 +120,22 @@
     printf("CTRL_REG2 %X\r\n", mag.readReg( MAG_CTRL_REG2 ));
 
 
-    // Calibrate the magnetomoter
-    calXY();
+    printf("Press and release SW1, rotate the board 360 degrees and then press and release SW1 to complete the calibration process.\r\n");
+
+    mag.calXY(PTC3, 0);
+
+    printf("Calibration complete.\r\n");
+
     
-
     // Loop: Blink LEDs, report pushbutton status, mag data, accel data
     while(1) {
-        
+
         // Get Magnetometer data
         int xVal = mag.readVal(MAG_OUT_X_MSB);
         int yVal = mag.readVal(MAG_OUT_Y_MSB);
-        float heading = mag.getHeading();        
-        
-        // Do something with heading - display direction 
+        float heading = mag.getHeading();
+
+        // Do something with heading - display direction
         printf("Heading: ");
         if (abs(heading) <= 22.5) printf("N ");
         if (abs(heading) >= 157.5) printf("S ");
@@ -144,8 +147,8 @@
         if (heading < -112.5 && heading > -157.5) printf("SW ");
         if (heading < 0) heading += 360.0;
         printf("%f\r\n", heading);
-        
-        
+
+
         greenLED = LED_ON;
         redLED = LED_OFF;
 
@@ -163,38 +166,7 @@
 
 void calXY() //magnetometer calibration: finding max and min of X, Y axis
 {
-    int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY;
-
-
-    printf("Press and release SW1 to Initiate Mag Calibration\r\n");
-    // Wait for SW1 press and release
-    while( sw1 == 1 ) {
-    }
-    while( sw1 == 0 ) {
-    }
-
-    printf("Rotate the board 360 degrees on a level plane then press and release SW1\r\n");
-
-    tempXmax = tempXmin = mag.readVal(MAG_OUT_X_MSB);
-    tempYmax = tempYmin = mag.readVal(MAG_OUT_Y_MSB);
-
-    while(sw1 == 1) {
-
-        newX = mag.readVal(MAG_OUT_X_MSB);
-        newY = mag.readVal(MAG_OUT_Y_MSB);
-        if (newX > tempXmax) tempXmax = newX;
-        if (newX < tempXmin) tempXmin = newX;
-        if (newY > tempYmax) tempYmax = newY;
-        if (newY < tempYmin) tempYmin = newY;
-    }
-
-    mag.setCalibration( tempXmin, tempXmax, tempYmin, tempYmax );
-
-    // Wait for release
-    while( sw1 == 0 ) {
-    }
-    
-    printf("Calibration complete.\r\n");
+ 
 
 }